xref: /webtrees/app/Module/ModuleLanguageTrait.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
111eb8581SGreg Roach<?php
23976b470SGreg Roach
311eb8581SGreg Roach/**
411eb8581SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
611eb8581SGreg Roach * This program is free software: you can redistribute it and/or modify
711eb8581SGreg Roach * it under the terms of the GNU General Public License as published by
811eb8581SGreg Roach * the Free Software Foundation, either version 3 of the License, or
911eb8581SGreg Roach * (at your option) any later version.
1011eb8581SGreg Roach * This program is distributed in the hope that it will be useful,
1111eb8581SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1211eb8581SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1311eb8581SGreg Roach * GNU General Public License for more details.
1411eb8581SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
1611eb8581SGreg Roach */
17fcfa147eSGreg Roach
1811eb8581SGreg Roachdeclare(strict_types=1);
1911eb8581SGreg Roach
2011eb8581SGreg Roachnamespace Fisharebest\Webtrees\Module;
2111eb8581SGreg Roach
224a9a6095SGreg Roachuse Fisharebest\ExtCalendar\CalendarInterface;
234a9a6095SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
2402086832SGreg Roachuse Fisharebest\Localization\Locale\LocaleEnUs;
2502086832SGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface;
263d8b2a8eSGreg Roachuse Fisharebest\Webtrees\I18N;
276fcafd02SGreg Roachuse Fisharebest\Webtrees\Relationship;
2852288ec7SGreg Roachuse Normalizer;
294a9a6095SGreg Roach
304a9a6095SGreg Roachuse function mb_substr;
3152288ec7SGreg Roachuse function normalizer_normalize;
3202086832SGreg Roach
3311eb8581SGreg Roach/**
3411eb8581SGreg Roach * Trait ModuleLanguageEventsTrait - default implementation of ModuleLanguageInterface.
3511eb8581SGreg Roach */
3611eb8581SGreg Roachtrait ModuleLanguageTrait
3711eb8581SGreg Roach{
3852288ec7SGreg Roach    /** @var array<string,string> */
3952288ec7SGreg Roach    private array $combining_diacritics = [
4052288ec7SGreg Roach        "\u{0300}" => '',
4152288ec7SGreg Roach        "\u{0301}" => '',
4252288ec7SGreg Roach        "\u{0302}" => '',
4352288ec7SGreg Roach        "\u{0303}" => '',
4452288ec7SGreg Roach        "\u{0304}" => '',
4552288ec7SGreg Roach        "\u{0305}" => '',
4652288ec7SGreg Roach        "\u{0306}" => '',
4752288ec7SGreg Roach        "\u{0307}" => '',
4852288ec7SGreg Roach        "\u{0308}" => '',
4952288ec7SGreg Roach        "\u{0309}" => '',
5052288ec7SGreg Roach        "\u{030A}" => '',
5152288ec7SGreg Roach        "\u{030B}" => '',
5252288ec7SGreg Roach        "\u{030C}" => '',
5352288ec7SGreg Roach        "\u{030D}" => '',
5452288ec7SGreg Roach        "\u{030E}" => '',
5552288ec7SGreg Roach        "\u{030F}" => '',
5652288ec7SGreg Roach        "\u{0310}" => '',
5752288ec7SGreg Roach        "\u{0311}" => '',
5852288ec7SGreg Roach        "\u{0312}" => '',
5952288ec7SGreg Roach        "\u{0313}" => '',
6052288ec7SGreg Roach        "\u{0314}" => '',
6152288ec7SGreg Roach        "\u{0315}" => '',
6252288ec7SGreg Roach        "\u{0316}" => '',
6352288ec7SGreg Roach        "\u{0317}" => '',
6452288ec7SGreg Roach        "\u{0318}" => '',
6552288ec7SGreg Roach        "\u{0319}" => '',
6652288ec7SGreg Roach        "\u{031A}" => '',
6752288ec7SGreg Roach        "\u{031B}" => '',
6852288ec7SGreg Roach        "\u{031C}" => '',
6952288ec7SGreg Roach        "\u{031D}" => '',
7052288ec7SGreg Roach        "\u{031E}" => '',
7152288ec7SGreg Roach        "\u{031F}" => '',
7252288ec7SGreg Roach        "\u{0320}" => '',
7352288ec7SGreg Roach        "\u{0321}" => '',
7452288ec7SGreg Roach        "\u{0322}" => '',
7552288ec7SGreg Roach        "\u{0323}" => '',
7652288ec7SGreg Roach        "\u{0324}" => '',
7752288ec7SGreg Roach        "\u{0325}" => '',
7852288ec7SGreg Roach        "\u{0326}" => '',
7952288ec7SGreg Roach        "\u{0327}" => '',
8052288ec7SGreg Roach        "\u{0328}" => '',
8152288ec7SGreg Roach        "\u{0329}" => '',
8252288ec7SGreg Roach        "\u{032A}" => '',
8352288ec7SGreg Roach        "\u{032B}" => '',
8452288ec7SGreg Roach        "\u{032C}" => '',
8552288ec7SGreg Roach        "\u{032D}" => '',
8652288ec7SGreg Roach        "\u{032E}" => '',
8752288ec7SGreg Roach        "\u{032F}" => '',
8852288ec7SGreg Roach        "\u{0330}" => '',
8952288ec7SGreg Roach        "\u{0331}" => '',
9052288ec7SGreg Roach        "\u{0332}" => '',
9152288ec7SGreg Roach        "\u{0333}" => '',
9252288ec7SGreg Roach        "\u{0334}" => '',
9352288ec7SGreg Roach        "\u{0335}" => '',
9452288ec7SGreg Roach        "\u{0336}" => '',
9552288ec7SGreg Roach        "\u{0337}" => '',
9652288ec7SGreg Roach        "\u{0338}" => '',
9752288ec7SGreg Roach        "\u{0339}" => '',
9852288ec7SGreg Roach        "\u{033A}" => '',
9952288ec7SGreg Roach        "\u{033B}" => '',
10052288ec7SGreg Roach        "\u{033C}" => '',
10152288ec7SGreg Roach        "\u{033D}" => '',
10252288ec7SGreg Roach        "\u{033E}" => '',
10352288ec7SGreg Roach        "\u{033F}" => '',
10452288ec7SGreg Roach        "\u{0340}" => '',
10552288ec7SGreg Roach        "\u{0341}" => '',
10652288ec7SGreg Roach        "\u{0342}" => '',
10752288ec7SGreg Roach        "\u{0343}" => '',
10852288ec7SGreg Roach        "\u{0344}" => '',
10952288ec7SGreg Roach        "\u{0345}" => '',
11052288ec7SGreg Roach        "\u{0346}" => '',
11152288ec7SGreg Roach        "\u{0347}" => '',
11252288ec7SGreg Roach        "\u{0348}" => '',
11352288ec7SGreg Roach        "\u{0349}" => '',
11452288ec7SGreg Roach        "\u{034A}" => '',
11552288ec7SGreg Roach        "\u{034B}" => '',
11652288ec7SGreg Roach        "\u{034C}" => '',
11752288ec7SGreg Roach        "\u{034D}" => '',
11852288ec7SGreg Roach        "\u{034E}" => '',
11952288ec7SGreg Roach        "\u{034F}" => '',
12052288ec7SGreg Roach        "\u{0350}" => '',
12152288ec7SGreg Roach        "\u{0351}" => '',
12252288ec7SGreg Roach        "\u{0352}" => '',
12352288ec7SGreg Roach        "\u{0353}" => '',
12452288ec7SGreg Roach        "\u{0354}" => '',
12552288ec7SGreg Roach        "\u{0355}" => '',
12652288ec7SGreg Roach        "\u{0356}" => '',
12752288ec7SGreg Roach        "\u{0357}" => '',
12852288ec7SGreg Roach        "\u{0358}" => '',
12952288ec7SGreg Roach        "\u{0359}" => '',
13052288ec7SGreg Roach        "\u{035A}" => '',
13152288ec7SGreg Roach        "\u{035B}" => '',
13252288ec7SGreg Roach        "\u{035C}" => '',
13352288ec7SGreg Roach        "\u{035D}" => '',
13452288ec7SGreg Roach        "\u{035E}" => '',
13552288ec7SGreg Roach        "\u{035F}" => '',
13652288ec7SGreg Roach        "\u{0360}" => '',
13752288ec7SGreg Roach        "\u{0361}" => '',
13852288ec7SGreg Roach        "\u{0362}" => '',
13952288ec7SGreg Roach        "\u{0363}" => '',
14052288ec7SGreg Roach        "\u{0364}" => '',
14152288ec7SGreg Roach        "\u{0365}" => '',
14252288ec7SGreg Roach        "\u{0366}" => '',
14352288ec7SGreg Roach        "\u{0367}" => '',
14452288ec7SGreg Roach        "\u{0368}" => '',
14552288ec7SGreg Roach        "\u{0369}" => '',
14652288ec7SGreg Roach        "\u{036A}" => '',
14752288ec7SGreg Roach        "\u{036B}" => '',
14852288ec7SGreg Roach        "\u{036C}" => '',
14952288ec7SGreg Roach        "\u{036D}" => '',
15052288ec7SGreg Roach        "\u{036E}" => '',
15152288ec7SGreg Roach        "\u{036F}" => '',
15252288ec7SGreg Roach    ];
15352288ec7SGreg Roach
1543d8b2a8eSGreg Roach    /**
1554a9a6095SGreg Roach     * Phone-book ordering of letters.
1564a9a6095SGreg Roach     *
1574a9a6095SGreg Roach     * @return array<int,string>
1584a9a6095SGreg Roach     */
1594a9a6095SGreg Roach    public function alphabet(): array
1604a9a6095SGreg Roach    {
1614a9a6095SGreg Roach        return ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
1624a9a6095SGreg Roach    }
1634a9a6095SGreg Roach
1644a9a6095SGreg Roach    /**
1654a9a6095SGreg Roach     * Default calendar used by this language.
1664a9a6095SGreg Roach     *
1674a9a6095SGreg Roach     * @return CalendarInterface
1684a9a6095SGreg Roach     */
1694a9a6095SGreg Roach    public function calendar(): CalendarInterface
1704a9a6095SGreg Roach    {
1714a9a6095SGreg Roach        return new GregorianCalendar();
1724a9a6095SGreg Roach    }
1734a9a6095SGreg Roach
1744a9a6095SGreg Roach    /**
1754a9a6095SGreg Roach     * One of: 'DMY', 'MDY', 'YMD'.
1764a9a6095SGreg Roach     *
1774a9a6095SGreg Roach     * @return string
1784a9a6095SGreg Roach     */
1794a9a6095SGreg Roach    public function dateOrder(): string
1804a9a6095SGreg Roach    {
1814a9a6095SGreg Roach        return 'DMY';
1824a9a6095SGreg Roach    }
1834a9a6095SGreg Roach
1844a9a6095SGreg Roach    /**
1854a9a6095SGreg Roach     * Some languages use digraphs and trigraphs.
1864a9a6095SGreg Roach     *
1874a9a6095SGreg Roach     * @param string $string
1884a9a6095SGreg Roach     *
1894a9a6095SGreg Roach     * @return string
1904a9a6095SGreg Roach     */
1914a9a6095SGreg Roach    public function initialLetter(string $string): string
1924a9a6095SGreg Roach    {
1934a9a6095SGreg Roach        return mb_substr($string, 0, 1);
1944a9a6095SGreg Roach    }
1954a9a6095SGreg Roach
1964a9a6095SGreg Roach    /**
19752288ec7SGreg Roach     * Ignore diacritics on letters - unless the language considers them a different letter.
1984a9a6095SGreg Roach     *
19952288ec7SGreg Roach     * @param string $text
20052288ec7SGreg Roach     *
20152288ec7SGreg Roach     * @return string
2024a9a6095SGreg Roach     */
20352288ec7SGreg Roach    public function normalize(string $text): string
2044a9a6095SGreg Roach    {
20552288ec7SGreg Roach        // Decompose any combined characters.
20652288ec7SGreg Roach        $text = normalizer_normalize($text, Normalizer::FORM_KD);
20752288ec7SGreg Roach
20852288ec7SGreg Roach        // Keep any significant diacritics.
20952288ec7SGreg Roach        $text = strtr($text, $this->normalizeExceptions());
21052288ec7SGreg Roach
21152288ec7SGreg Roach        // Remove other diacritics.
21252288ec7SGreg Roach        return strtr($text, $this->combining_diacritics);
21352288ec7SGreg Roach    }
21452288ec7SGreg Roach
21552288ec7SGreg Roach    /**
21652288ec7SGreg Roach     * Letters with diacritics that are considered distinct letters in this language.
21752288ec7SGreg Roach     *
21852288ec7SGreg Roach     * @return array<string,string>
21952288ec7SGreg Roach     */
22052288ec7SGreg Roach    protected function normalizeExceptions(): array
22152288ec7SGreg Roach    {
22252288ec7SGreg Roach        return [];
2234a9a6095SGreg Roach    }
2244a9a6095SGreg Roach
2254a9a6095SGreg Roach    /**
2263d8b2a8eSGreg Roach     * How should this module be identified in the control panel, etc.?
2273d8b2a8eSGreg Roach     *
2283d8b2a8eSGreg Roach     * @return string
2293d8b2a8eSGreg Roach     */
23002086832SGreg Roach    public function title(): string
23102086832SGreg Roach    {
23202086832SGreg Roach        return  $this->locale()->endonym();
23302086832SGreg Roach    }
23402086832SGreg Roach
23502086832SGreg Roach    public function description(): string
23602086832SGreg Roach    {
2373d8b2a8eSGreg Roach        return I18N::translate('Language') . ' — ' . $this->title() . ' — ' . $this->locale()->languageTag();
23802086832SGreg Roach    }
23902086832SGreg Roach
24002086832SGreg Roach    /**
24102086832SGreg Roach     * @return LocaleInterface
24202086832SGreg Roach     */
24302086832SGreg Roach    public function locale(): LocaleInterface
24402086832SGreg Roach    {
24502086832SGreg Roach        return new LocaleEnUs();
24602086832SGreg Roach    }
2476fcafd02SGreg Roach
2486fcafd02SGreg Roach    /**
2496fcafd02SGreg Roach     * @return array<Relationship>
2506fcafd02SGreg Roach     */
2516fcafd02SGreg Roach    public function relationships(): array
2526fcafd02SGreg Roach    {
2536fcafd02SGreg Roach        return [];
2546fcafd02SGreg Roach    }
25511eb8581SGreg Roach}
256