xref: /webtrees/app/I18N.php (revision b458aac1fdf186c83111dd46e022cfdd0efd8e62)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg 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/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
2237646143SGreg Roachuse Closure;
23991b93ddSGreg Roachuse Collator;
24f1af7e1cSGreg Roachuse Exception;
25c999a340SGreg Roachuse Fisharebest\Localization\Locale;
261e71bdc0SGreg Roachuse Fisharebest\Localization\Locale\LocaleEnUs;
2715834aaeSGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface;
283bdc890bSGreg Roachuse Fisharebest\Localization\Translation;
293bdc890bSGreg Roachuse Fisharebest\Localization\Translator;
30d37db671SGreg Roachuse Fisharebest\Webtrees\Module\ModuleCustomInterface;
3102086832SGreg Roachuse Fisharebest\Webtrees\Module\ModuleLanguageInterface;
32d37db671SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
333976b470SGreg Roach
344f194b97SGreg Roachuse function array_merge;
35d68ee7a8SGreg Roachuse function class_exists;
36d68ee7a8SGreg Roachuse function html_entity_decode;
37d68ee7a8SGreg Roachuse function in_array;
38d68ee7a8SGreg Roachuse function mb_strtolower;
39d68ee7a8SGreg Roachuse function mb_strtoupper;
40d68ee7a8SGreg Roachuse function mb_substr;
41d68ee7a8SGreg Roachuse function ord;
42d68ee7a8SGreg Roachuse function sprintf;
43dec352c1SGreg Roachuse function str_contains;
44d68ee7a8SGreg Roachuse function str_replace;
45d68ee7a8SGreg Roachuse function strcmp;
46d68ee7a8SGreg Roachuse function strip_tags;
47d68ee7a8SGreg Roachuse function strlen;
48d68ee7a8SGreg Roachuse function strtr;
49b0fcccb0SGreg Roachuse function var_export;
50a25f0a04SGreg Roach
51a25f0a04SGreg Roach/**
5276692c8bSGreg Roach * Internationalization (i18n) and localization (l10n).
53a25f0a04SGreg Roach */
54c1010edaSGreg Roachclass I18N
55c1010edaSGreg Roach{
56d37db671SGreg Roach    // MO files use special characters for plurals and context.
574f194b97SGreg Roach    public const PLURAL  = "\x00";
584f194b97SGreg Roach    public const CONTEXT = "\x04";
596fcafd02SGreg Roach
606fcafd02SGreg Roach    // Digits are always rendered LTR, even in RTL text.
6116d6367aSGreg Roach    private const DIGITS = '0123456789٠١٢٣٤٥٦٧٨٩۰۱۲۳۴۵۶۷۸۹';
626fcafd02SGreg Roach
636fcafd02SGreg Roach    // These locales need special handling for the dotless letter I.
6416d6367aSGreg Roach    private const DOTLESS_I_LOCALES = [
65c1010edaSGreg Roach        'az',
66c1010edaSGreg Roach        'tr',
67c1010edaSGreg Roach    ];
686fcafd02SGreg Roach
6916d6367aSGreg Roach    private const DOTLESS_I_TOLOWER = [
70c1010edaSGreg Roach        'I' => 'ı',
71c1010edaSGreg Roach        'İ' => 'i',
72c1010edaSGreg Roach    ];
73006094b9SGreg Roach
7416d6367aSGreg Roach    private const DOTLESS_I_TOUPPER = [
75c1010edaSGreg Roach        'ı' => 'I',
76c1010edaSGreg Roach        'i' => 'İ',
77c1010edaSGreg Roach    ];
78a25f0a04SGreg Roach
796fcafd02SGreg Roach    // The ranges of characters used by each script.
8016d6367aSGreg Roach    private const SCRIPT_CHARACTER_RANGES = [
81c1010edaSGreg Roach        [
82c1010edaSGreg Roach            'Latn',
83c1010edaSGreg Roach            0x0041,
84c1010edaSGreg Roach            0x005A,
85c1010edaSGreg Roach        ],
86c1010edaSGreg Roach        [
87c1010edaSGreg Roach            'Latn',
88c1010edaSGreg Roach            0x0061,
89c1010edaSGreg Roach            0x007A,
90c1010edaSGreg Roach        ],
91c1010edaSGreg Roach        [
92c1010edaSGreg Roach            'Latn',
93c1010edaSGreg Roach            0x0100,
94c1010edaSGreg Roach            0x02AF,
95c1010edaSGreg Roach        ],
96c1010edaSGreg Roach        [
97c1010edaSGreg Roach            'Grek',
98c1010edaSGreg Roach            0x0370,
99c1010edaSGreg Roach            0x03FF,
100c1010edaSGreg Roach        ],
101c1010edaSGreg Roach        [
102c1010edaSGreg Roach            'Cyrl',
103c1010edaSGreg Roach            0x0400,
104c1010edaSGreg Roach            0x052F,
105c1010edaSGreg Roach        ],
106c1010edaSGreg Roach        [
107c1010edaSGreg Roach            'Hebr',
108c1010edaSGreg Roach            0x0590,
109c1010edaSGreg Roach            0x05FF,
110c1010edaSGreg Roach        ],
111c1010edaSGreg Roach        [
112c1010edaSGreg Roach            'Arab',
113c1010edaSGreg Roach            0x0600,
114c1010edaSGreg Roach            0x06FF,
115c1010edaSGreg Roach        ],
116c1010edaSGreg Roach        [
117c1010edaSGreg Roach            'Arab',
118c1010edaSGreg Roach            0x0750,
119c1010edaSGreg Roach            0x077F,
120c1010edaSGreg Roach        ],
121c1010edaSGreg Roach        [
122c1010edaSGreg Roach            'Arab',
123c1010edaSGreg Roach            0x08A0,
124c1010edaSGreg Roach            0x08FF,
125c1010edaSGreg Roach        ],
126c1010edaSGreg Roach        [
127c1010edaSGreg Roach            'Deva',
128c1010edaSGreg Roach            0x0900,
129c1010edaSGreg Roach            0x097F,
130c1010edaSGreg Roach        ],
131c1010edaSGreg Roach        [
132c1010edaSGreg Roach            'Taml',
133c1010edaSGreg Roach            0x0B80,
134c1010edaSGreg Roach            0x0BFF,
135c1010edaSGreg Roach        ],
136c1010edaSGreg Roach        [
137c1010edaSGreg Roach            'Sinh',
138c1010edaSGreg Roach            0x0D80,
139c1010edaSGreg Roach            0x0DFF,
140c1010edaSGreg Roach        ],
141c1010edaSGreg Roach        [
142c1010edaSGreg Roach            'Thai',
143c1010edaSGreg Roach            0x0E00,
144c1010edaSGreg Roach            0x0E7F,
145c1010edaSGreg Roach        ],
146c1010edaSGreg Roach        [
147c1010edaSGreg Roach            'Geor',
148c1010edaSGreg Roach            0x10A0,
149c1010edaSGreg Roach            0x10FF,
150c1010edaSGreg Roach        ],
151c1010edaSGreg Roach        [
152c1010edaSGreg Roach            'Grek',
153c1010edaSGreg Roach            0x1F00,
154c1010edaSGreg Roach            0x1FFF,
155c1010edaSGreg Roach        ],
156c1010edaSGreg Roach        [
157c1010edaSGreg Roach            'Deva',
158c1010edaSGreg Roach            0xA8E0,
159c1010edaSGreg Roach            0xA8FF,
160c1010edaSGreg Roach        ],
161c1010edaSGreg Roach        [
162c1010edaSGreg Roach            'Hans',
163c1010edaSGreg Roach            0x3000,
164c1010edaSGreg Roach            0x303F,
165c1010edaSGreg Roach        ],
166c1010edaSGreg Roach        // Mixed CJK, not just Hans
167c1010edaSGreg Roach        [
168c1010edaSGreg Roach            'Hans',
169c1010edaSGreg Roach            0x3400,
170c1010edaSGreg Roach            0xFAFF,
171c1010edaSGreg Roach        ],
172c1010edaSGreg Roach        // Mixed CJK, not just Hans
173c1010edaSGreg Roach        [
174c1010edaSGreg Roach            'Hans',
175c1010edaSGreg Roach            0x20000,
176c1010edaSGreg Roach            0x2FA1F,
177c1010edaSGreg Roach        ],
178c1010edaSGreg Roach        // Mixed CJK, not just Hans
17913abd6f3SGreg Roach    ];
1806fcafd02SGreg Roach
1816fcafd02SGreg Roach    // Characters that are displayed in mirror form in RTL text.
18216d6367aSGreg Roach    private const MIRROR_CHARACTERS = [
183a25f0a04SGreg Roach        '('  => ')',
184a25f0a04SGreg Roach        ')'  => '(',
185a25f0a04SGreg Roach        '['  => ']',
186a25f0a04SGreg Roach        ']'  => '[',
187a25f0a04SGreg Roach        '{'  => '}',
188a25f0a04SGreg Roach        '}'  => '{',
189a25f0a04SGreg Roach        '<'  => '>',
190a25f0a04SGreg Roach        '>'  => '<',
191a25f0a04SGreg Roach        '‹ ' => '›',
192a25f0a04SGreg Roach        '› ' => '‹',
193a25f0a04SGreg Roach        '«'  => '»',
194a25f0a04SGreg Roach        '»'  => '«',
195a25f0a04SGreg Roach        '﴾ ' => '﴿',
196a25f0a04SGreg Roach        '﴿ ' => '﴾',
197a25f0a04SGreg Roach        '“ ' => '”',
198a25f0a04SGreg Roach        '” ' => '“',
199a25f0a04SGreg Roach        '‘ ' => '’',
200a25f0a04SGreg Roach        '’ ' => '‘',
20113abd6f3SGreg Roach    ];
202a25f0a04SGreg Roach
2036fcafd02SGreg Roach    // Punctuation used to separate list items, typically a comma
2046fcafd02SGreg Roach    public static string $list_separator;
205006094b9SGreg Roach
206*b458aac1SGreg Roach    private static ModuleLanguageInterface $language;
2076fcafd02SGreg Roach
2086fcafd02SGreg Roach    private static LocaleInterface $locale;
2096fcafd02SGreg Roach
2106fcafd02SGreg Roach    private static Translator $translator;
2116fcafd02SGreg Roach
212a90d1d44SGreg Roach    private static ?Collator $collator = null;
213006094b9SGreg Roach
214a25f0a04SGreg Roach    /**
21502086832SGreg Roach     * The preferred locales for this site, or a default list if no preference.
216dfeee0a8SGreg Roach     *
217ac701fbdSGreg Roach     * @return array<LocaleInterface>
218dfeee0a8SGreg Roach     */
2198f53f488SRico Sonntag    public static function activeLocales(): array
220c1010edaSGreg Roach    {
22102086832SGreg Roach        $locales = app(ModuleService::class)
222d6137952SGreg Roach            ->findByInterface(ModuleLanguageInterface::class, false, true)
2230b5fd0a6SGreg Roach            ->map(static function (ModuleLanguageInterface $module): LocaleInterface {
22402086832SGreg Roach                return $module->locale();
22502086832SGreg Roach            });
226dfeee0a8SGreg Roach
22702086832SGreg Roach        if ($locales->isEmpty()) {
22802086832SGreg Roach            return [new LocaleEnUs()];
229dfeee0a8SGreg Roach        }
230dfeee0a8SGreg Roach
23102086832SGreg Roach        return $locales->all();
232dfeee0a8SGreg Roach    }
233dfeee0a8SGreg Roach
234dfeee0a8SGreg Roach    /**
235dfeee0a8SGreg Roach     * Which MySQL collation should be used for this locale?
236dfeee0a8SGreg Roach     *
237dfeee0a8SGreg Roach     * @return string
238dfeee0a8SGreg Roach     */
239e364afe4SGreg Roach    public static function collation(): string
240c1010edaSGreg Roach    {
241dfeee0a8SGreg Roach        $collation = self::$locale->collation();
242dfeee0a8SGreg Roach        switch ($collation) {
243dfeee0a8SGreg Roach            case 'croatian_ci':
244dfeee0a8SGreg Roach            case 'german2_ci':
245dfeee0a8SGreg Roach            case 'vietnamese_ci':
246dfeee0a8SGreg Roach                // Only available in MySQL 5.6
247dfeee0a8SGreg Roach                return 'utf8_unicode_ci';
248dfeee0a8SGreg Roach            default:
249dfeee0a8SGreg Roach                return 'utf8_' . $collation;
250dfeee0a8SGreg Roach        }
251dfeee0a8SGreg Roach    }
252dfeee0a8SGreg Roach
253dfeee0a8SGreg Roach    /**
254dfeee0a8SGreg Roach     * What format is used to display dates in the current locale?
255dfeee0a8SGreg Roach     *
256dfeee0a8SGreg Roach     * @return string
257dfeee0a8SGreg Roach     */
2588f53f488SRico Sonntag    public static function dateFormat(): string
259c1010edaSGreg Roach    {
260ad3143ccSGreg Roach        /* I18N: This is the format string for full dates. See https://php.net/date for codes */
261bbb76c12SGreg Roach        return self::$translator->translate('%j %F %Y');
262dfeee0a8SGreg Roach    }
263dfeee0a8SGreg Roach
264dfeee0a8SGreg Roach    /**
265dfeee0a8SGreg Roach     * Convert the digits 0-9 into the local script
266dfeee0a8SGreg Roach     * Used for years, etc., where we do not want thousands-separators, decimals, etc.
267dfeee0a8SGreg Roach     *
26855664801SGreg Roach     * @param string|int $n
269dfeee0a8SGreg Roach     *
270dfeee0a8SGreg Roach     * @return string
271dfeee0a8SGreg Roach     */
272ac71572dSGreg Roach    public static function digits(string|int $n): string
273c1010edaSGreg Roach    {
27455664801SGreg Roach        return self::$locale->digits((string) $n);
275dfeee0a8SGreg Roach    }
276dfeee0a8SGreg Roach
277dfeee0a8SGreg Roach    /**
278dfeee0a8SGreg Roach     * What is the direction of the current locale
279dfeee0a8SGreg Roach     *
280dfeee0a8SGreg Roach     * @return string "ltr" or "rtl"
281dfeee0a8SGreg Roach     */
2828f53f488SRico Sonntag    public static function direction(): string
283c1010edaSGreg Roach    {
284dfeee0a8SGreg Roach        return self::$locale->direction();
285dfeee0a8SGreg Roach    }
286dfeee0a8SGreg Roach
287dfeee0a8SGreg Roach    /**
288a25f0a04SGreg Roach     * Initialise the translation adapter with a locale setting.
289a25f0a04SGreg Roach     *
290150f35adSGreg Roach     * @param string $code
291150f35adSGreg Roach     * @param bool   $setup
292a25f0a04SGreg Roach     *
293150f35adSGreg Roach     * @return void
294a25f0a04SGreg Roach     */
295150f35adSGreg Roach    public static function init(string $code, bool $setup = false): void
296c1010edaSGreg Roach    {
2973bdc890bSGreg Roach        self::$locale = Locale::create($code);
2983bdc890bSGreg Roach
2994f194b97SGreg Roach        // Load the translation file
300150f35adSGreg Roach        $translation_file = __DIR__ . '/../resources/lang/' . self::$locale->languageTag() . '/messages.php';
3014f194b97SGreg Roach
302f1af7e1cSGreg Roach        try {
303006094b9SGreg Roach            $translation  = new Translation($translation_file);
304006094b9SGreg Roach            $translations = $translation->asArray();
30528d026adSGreg Roach        } catch (Exception) {
306006094b9SGreg Roach            // The translations files are created during the build process, and are
307006094b9SGreg Roach            // not included in the source code.
308006094b9SGreg Roach            // Assuming we are using dev code, and build (or rebuild) the files.
309006094b9SGreg Roach            $po_file      = Webtrees::ROOT_DIR . 'resources/lang/' . self::$locale->languageTag() . '/messages.po';
310006094b9SGreg Roach            $translation  = new Translation($po_file);
311006094b9SGreg Roach            $translations = $translation->asArray();
312b0fcccb0SGreg Roach            file_put_contents($translation_file, "<?php\n\nreturn " . var_export($translations, true) . ";\n");
313a25f0a04SGreg Roach        }
314a25f0a04SGreg Roach
3154f194b97SGreg Roach        // Add translations from custom modules (but not during setup, as we have no database/modules)
316c116a5ccSGreg Roach        if (!$setup) {
3176fcafd02SGreg Roach            $module_service = app(ModuleService::class);
3186fcafd02SGreg Roach
3196fcafd02SGreg Roach            $translations = $module_service
3204f194b97SGreg Roach                ->findByInterface(ModuleCustomInterface::class)
32169253da9SGreg Roach                ->reduce(static function (array $carry, ModuleCustomInterface $item): array {
3224f194b97SGreg Roach                    return array_merge($carry, $item->customTranslations(self::$locale->languageTag()));
3234f194b97SGreg Roach                }, $translations);
3246fcafd02SGreg Roach
3256fcafd02SGreg Roach            self::$language = $module_service
326*b458aac1SGreg Roach                ->findByInterface(ModuleLanguageInterface::class, true)
3276fcafd02SGreg Roach                ->first(fn (ModuleLanguageInterface $module): bool => $module->locale()->languageTag() === $code);
328d37db671SGreg Roach        }
329d37db671SGreg Roach
3303bdc890bSGreg Roach        // Create a translator
3313bdc890bSGreg Roach        self::$translator = new Translator($translations, self::$locale->pluralRule());
332a25f0a04SGreg Roach
333bbb76c12SGreg Roach        /* I18N: This punctuation is used to separate lists of items */
334bbb76c12SGreg Roach        self::$list_separator = self::translate(', ');
335a25f0a04SGreg Roach
336991b93ddSGreg Roach        // Create a collator
337991b93ddSGreg Roach        try {
338c9ec599fSGreg Roach            // Symfony provides a very incomplete polyfill - which cannot be used.
339dff81305SGreg Roach            if (class_exists('Collator')) {
340dff81305SGreg Roach                // Need phonebook collation rules for German Ä, Ö and Ü.
341dff81305SGreg Roach                if (str_contains(self::$locale->code(), '@')) {
342dff81305SGreg Roach                    self::$collator = new Collator(self::$locale->code() . ';collation=phonebook');
343dff81305SGreg Roach                } else {
344dff81305SGreg Roach                    self::$collator = new Collator(self::$locale->code() . '@collation=phonebook');
345dff81305SGreg Roach                }
346991b93ddSGreg Roach                // Ignore upper/lower case differences
347991b93ddSGreg Roach                self::$collator->setStrength(Collator::SECONDARY);
348444a65ecSGreg Roach            }
34928d026adSGreg Roach        } catch (Exception) {
350991b93ddSGreg Roach            // PHP-INTL is not installed?  We'll use a fallback later.
351991b93ddSGreg Roach        }
352a25f0a04SGreg Roach    }
353a25f0a04SGreg Roach
354a25f0a04SGreg Roach    /**
355006094b9SGreg Roach     * Translate a string, and then substitute placeholders
356006094b9SGreg Roach     * echo I18N::translate('Hello World!');
357006094b9SGreg Roach     * echo I18N::translate('The %s sat on the mat', 'cat');
358006094b9SGreg Roach     *
359006094b9SGreg Roach     * @param string $message
360006094b9SGreg Roach     * @param string ...$args
361006094b9SGreg Roach     *
362006094b9SGreg Roach     * @return string
363006094b9SGreg Roach     */
364006094b9SGreg Roach    public static function translate(string $message, ...$args): string
365006094b9SGreg Roach    {
366006094b9SGreg Roach        $message = self::$translator->translate($message);
367006094b9SGreg Roach
368006094b9SGreg Roach        return sprintf($message, ...$args);
369006094b9SGreg Roach    }
370006094b9SGreg Roach
371006094b9SGreg Roach    /**
37290a2f718SGreg Roach     * @return string
37390a2f718SGreg Roach     */
37490a2f718SGreg Roach    public static function languageTag(): string
37590a2f718SGreg Roach    {
37690a2f718SGreg Roach        return self::$locale->languageTag();
37790a2f718SGreg Roach    }
37890a2f718SGreg Roach
37990a2f718SGreg Roach    /**
38065cf5706SGreg Roach     * @return LocaleInterface
38165cf5706SGreg Roach     */
38265cf5706SGreg Roach    public static function locale(): LocaleInterface
38365cf5706SGreg Roach    {
38465cf5706SGreg Roach        return self::$locale;
38565cf5706SGreg Roach    }
38665cf5706SGreg Roach
38765cf5706SGreg Roach    /**
3886fcafd02SGreg Roach     * @return ModuleLanguageInterface
3896fcafd02SGreg Roach     */
3906fcafd02SGreg Roach    public static function language(): ModuleLanguageInterface
3916fcafd02SGreg Roach    {
3926fcafd02SGreg Roach        return self::$language;
3936fcafd02SGreg Roach    }
3946fcafd02SGreg Roach
3956fcafd02SGreg Roach    /**
396dfeee0a8SGreg Roach     * Translate a number into the local representation.
397dfeee0a8SGreg Roach     * e.g. 12345.67 becomes
398dfeee0a8SGreg Roach     * en: 12,345.67
399dfeee0a8SGreg Roach     * fr: 12 345,67
400dfeee0a8SGreg Roach     * de: 12.345,67
401dfeee0a8SGreg Roach     *
402dfeee0a8SGreg Roach     * @param float $n
403cbc1590aSGreg Roach     * @param int   $precision
404a25f0a04SGreg Roach     *
405a25f0a04SGreg Roach     * @return string
406a25f0a04SGreg Roach     */
40755664801SGreg Roach    public static function number(float $n, int $precision = 0): string
408c1010edaSGreg Roach    {
409dfeee0a8SGreg Roach        return self::$locale->number(round($n, $precision));
410dfeee0a8SGreg Roach    }
411dfeee0a8SGreg Roach
412dfeee0a8SGreg Roach    /**
413dfeee0a8SGreg Roach     * Translate a fraction into a percentage.
414dfeee0a8SGreg Roach     * e.g. 0.123 becomes
415dfeee0a8SGreg Roach     * en: 12.3%
416dfeee0a8SGreg Roach     * fr: 12,3 %
417dfeee0a8SGreg Roach     * de: 12,3%
418dfeee0a8SGreg Roach     *
419dfeee0a8SGreg Roach     * @param float $n
420cbc1590aSGreg Roach     * @param int   $precision
421dfeee0a8SGreg Roach     *
422dfeee0a8SGreg Roach     * @return string
423dfeee0a8SGreg Roach     */
42455664801SGreg Roach    public static function percentage(float $n, int $precision = 0): string
425c1010edaSGreg Roach    {
426dfeee0a8SGreg Roach        return self::$locale->percent(round($n, $precision + 2));
427dfeee0a8SGreg Roach    }
428dfeee0a8SGreg Roach
429dfeee0a8SGreg Roach    /**
430dfeee0a8SGreg Roach     * Translate a plural string
431dfeee0a8SGreg Roach     * echo self::plural('There is an error', 'There are errors', $num_errors);
432dfeee0a8SGreg Roach     * echo self::plural('There is one error', 'There are %s errors', $num_errors);
433dfeee0a8SGreg Roach     * echo self::plural('There is %1$s %2$s cat', 'There are %1$s %2$s cats', $num, $num, $colour);
434dfeee0a8SGreg Roach     *
435924d091bSGreg Roach     * @param string $singular
436924d091bSGreg Roach     * @param string $plural
437924d091bSGreg Roach     * @param int    $count
438a515be7cSGreg Roach     * @param string ...$args
439e93111adSRico Sonntag     *
440dfeee0a8SGreg Roach     * @return string
441dfeee0a8SGreg Roach     */
442924d091bSGreg Roach    public static function plural(string $singular, string $plural, int $count, ...$args): string
443c1010edaSGreg Roach    {
444924d091bSGreg Roach        $message = self::$translator->translatePlural($singular, $plural, $count);
445dfeee0a8SGreg Roach
446924d091bSGreg Roach        return sprintf($message, ...$args);
447dfeee0a8SGreg Roach    }
448dfeee0a8SGreg Roach
449dfeee0a8SGreg Roach    /**
450dfeee0a8SGreg Roach     * UTF8 version of PHP::strrev()
451dfeee0a8SGreg Roach     * Reverse RTL text for third-party libraries such as GD2 and googlechart.
452dfeee0a8SGreg Roach     * These do not support UTF8 text direction, so we must mimic it for them.
453dfeee0a8SGreg Roach     * Numbers are always rendered LTR, even in RTL text.
454dfeee0a8SGreg Roach     * The visual direction of characters such as parentheses should be reversed.
455dfeee0a8SGreg Roach     *
456dfeee0a8SGreg Roach     * @param string $text Text to be reversed
457dfeee0a8SGreg Roach     *
458dfeee0a8SGreg Roach     * @return string
459dfeee0a8SGreg Roach     */
460e0c85f48SGreg Roach    public static function reverseText(string $text): string
461c1010edaSGreg Roach    {
462dfeee0a8SGreg Roach        // Remove HTML markup - we can't display it and it is LTR.
4639524b7b5SGreg Roach        $text = strip_tags($text);
4649524b7b5SGreg Roach        // Remove HTML entities.
4659524b7b5SGreg Roach        $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
466dfeee0a8SGreg Roach
467dfeee0a8SGreg Roach        // LTR text doesn't need reversing
468dfeee0a8SGreg Roach        if (self::scriptDirection(self::textScript($text)) === 'ltr') {
469dfeee0a8SGreg Roach            return $text;
470dfeee0a8SGreg Roach        }
471dfeee0a8SGreg Roach
472dfeee0a8SGreg Roach        // Mirrored characters
473991b93ddSGreg Roach        $text = strtr($text, self::MIRROR_CHARACTERS);
474dfeee0a8SGreg Roach
475dfeee0a8SGreg Roach        $reversed = '';
476dfeee0a8SGreg Roach        $digits   = '';
477e364afe4SGreg Roach        while ($text !== '') {
478dfeee0a8SGreg Roach            $letter = mb_substr($text, 0, 1);
479dfeee0a8SGreg Roach            $text   = mb_substr($text, 1);
480dec352c1SGreg Roach            if (str_contains(self::DIGITS, $letter)) {
481dfeee0a8SGreg Roach                $digits .= $letter;
482a25f0a04SGreg Roach            } else {
483dfeee0a8SGreg Roach                $reversed = $letter . $digits . $reversed;
484dfeee0a8SGreg Roach                $digits   = '';
485dfeee0a8SGreg Roach            }
486a25f0a04SGreg Roach        }
487a25f0a04SGreg Roach
488dfeee0a8SGreg Roach        return $digits . $reversed;
489a25f0a04SGreg Roach    }
490a25f0a04SGreg Roach
491a25f0a04SGreg Roach    /**
492a25f0a04SGreg Roach     * Return the direction (ltr or rtl) for a given script
493a25f0a04SGreg Roach     * The PHP/intl library does not provde this information, so we need
494a25f0a04SGreg Roach     * our own lookup table.
495a25f0a04SGreg Roach     *
496a25f0a04SGreg Roach     * @param string $script
497a25f0a04SGreg Roach     *
498a25f0a04SGreg Roach     * @return string
499a25f0a04SGreg Roach     */
500e0c85f48SGreg Roach    public static function scriptDirection(string $script): string
501c1010edaSGreg Roach    {
502a25f0a04SGreg Roach        switch ($script) {
503a25f0a04SGreg Roach            case 'Arab':
504a25f0a04SGreg Roach            case 'Hebr':
505a25f0a04SGreg Roach            case 'Mong':
506a25f0a04SGreg Roach            case 'Thaa':
507a25f0a04SGreg Roach                return 'rtl';
508a25f0a04SGreg Roach            default:
509a25f0a04SGreg Roach                return 'ltr';
510a25f0a04SGreg Roach        }
511a25f0a04SGreg Roach    }
512a25f0a04SGreg Roach
513a25f0a04SGreg Roach    /**
514dfeee0a8SGreg Roach     * Identify the script used for a piece of text
515dfeee0a8SGreg Roach     *
516d0bfc631SGreg Roach     * @param string $string
517dfeee0a8SGreg Roach     *
518dfeee0a8SGreg Roach     * @return string
519dfeee0a8SGreg Roach     */
520e0c85f48SGreg Roach    public static function textScript(string $string): string
521c1010edaSGreg Roach    {
522dfeee0a8SGreg Roach        $string = strip_tags($string); // otherwise HTML tags show up as latin
523dfeee0a8SGreg Roach        $string = html_entity_decode($string, ENT_QUOTES, 'UTF-8'); // otherwise HTML entities show up as latin
524c1010edaSGreg Roach        $string = str_replace([
5258fb4e87cSGreg Roach            Individual::NOMEN_NESCIO,
5268fb4e87cSGreg Roach            Individual::PRAENOMEN_NESCIO,
5278fb4e87cSGreg Roach        ], '', $string);
528dfeee0a8SGreg Roach        $pos    = 0;
529dfeee0a8SGreg Roach        $strlen = strlen($string);
530dfeee0a8SGreg Roach        while ($pos < $strlen) {
531dfeee0a8SGreg Roach            // get the Unicode Code Point for the character at position $pos
532dfeee0a8SGreg Roach            $byte1 = ord($string[$pos]);
533dfeee0a8SGreg Roach            if ($byte1 < 0x80) {
534dfeee0a8SGreg Roach                $code_point = $byte1;
535dfeee0a8SGreg Roach                $chrlen     = 1;
536dfeee0a8SGreg Roach            } elseif ($byte1 < 0xC0) {
537dfeee0a8SGreg Roach                // Invalid continuation character
538dfeee0a8SGreg Roach                return 'Latn';
539dfeee0a8SGreg Roach            } elseif ($byte1 < 0xE0) {
540dfeee0a8SGreg Roach                $code_point = (($byte1 & 0x1F) << 6) + (ord($string[$pos + 1]) & 0x3F);
541dfeee0a8SGreg Roach                $chrlen     = 2;
542dfeee0a8SGreg Roach            } elseif ($byte1 < 0xF0) {
543dfeee0a8SGreg Roach                $code_point = (($byte1 & 0x0F) << 12) + ((ord($string[$pos + 1]) & 0x3F) << 6) + (ord($string[$pos + 2]) & 0x3F);
544dfeee0a8SGreg Roach                $chrlen     = 3;
545dfeee0a8SGreg Roach            } elseif ($byte1 < 0xF8) {
546dfeee0a8SGreg Roach                $code_point = (($byte1 & 0x07) << 24) + ((ord($string[$pos + 1]) & 0x3F) << 12) + ((ord($string[$pos + 2]) & 0x3F) << 6) + (ord($string[$pos + 3]) & 0x3F);
547dfeee0a8SGreg Roach                $chrlen     = 3;
548dfeee0a8SGreg Roach            } else {
549dfeee0a8SGreg Roach                // Invalid UTF
550dfeee0a8SGreg Roach                return 'Latn';
551dfeee0a8SGreg Roach            }
552dfeee0a8SGreg Roach
553991b93ddSGreg Roach            foreach (self::SCRIPT_CHARACTER_RANGES as $range) {
554dfeee0a8SGreg Roach                if ($code_point >= $range[1] && $code_point <= $range[2]) {
555dfeee0a8SGreg Roach                    return $range[0];
556dfeee0a8SGreg Roach                }
557dfeee0a8SGreg Roach            }
558dfeee0a8SGreg Roach            // Not a recognised script. Maybe punctuation, spacing, etc. Keep looking.
559dfeee0a8SGreg Roach            $pos += $chrlen;
560dfeee0a8SGreg Roach        }
561dfeee0a8SGreg Roach
562dfeee0a8SGreg Roach        return 'Latn';
563dfeee0a8SGreg Roach    }
564dfeee0a8SGreg Roach
565dfeee0a8SGreg Roach    /**
56637646143SGreg Roach     * A closure which will compare strings using local collation rules.
567006094b9SGreg Roach     *
56837646143SGreg Roach     * @return Closure
569006094b9SGreg Roach     */
57037646143SGreg Roach    public static function comparator(): Closure
571006094b9SGreg Roach    {
57239bfe684SGreg Roach        $collator = self::$collator;
57339bfe684SGreg Roach
57439bfe684SGreg Roach        if ($collator instanceof Collator) {
57539bfe684SGreg Roach            return static fn (string $x, string $y): int => (int) $collator->compare($x, $y);
576006094b9SGreg Roach        }
577006094b9SGreg Roach
5786c3b7df0SGreg Roach        return static fn (string $x, string $y): int => strcmp(self::strtolower($x), self::strtolower($y));
579006094b9SGreg Roach    }
580006094b9SGreg Roach
58137646143SGreg Roach
58237646143SGreg Roach
583006094b9SGreg Roach    /**
584006094b9SGreg Roach     * Convert a string to lower case.
585006094b9SGreg Roach     *
586006094b9SGreg Roach     * @param string $string
587006094b9SGreg Roach     *
588006094b9SGreg Roach     * @return string
589006094b9SGreg Roach     */
590e0c85f48SGreg Roach    public static function strtolower(string $string): string
591006094b9SGreg Roach    {
592006094b9SGreg Roach        if (in_array(self::$locale->language()->code(), self::DOTLESS_I_LOCALES, true)) {
593006094b9SGreg Roach            $string = strtr($string, self::DOTLESS_I_TOLOWER);
594006094b9SGreg Roach        }
595006094b9SGreg Roach
596006094b9SGreg Roach        return mb_strtolower($string);
597006094b9SGreg Roach    }
598006094b9SGreg Roach
599006094b9SGreg Roach    /**
600006094b9SGreg Roach     * Convert a string to upper case.
601006094b9SGreg Roach     *
602006094b9SGreg Roach     * @param string $string
603006094b9SGreg Roach     *
604006094b9SGreg Roach     * @return string
605006094b9SGreg Roach     */
606e0c85f48SGreg Roach    public static function strtoupper(string $string): string
607006094b9SGreg Roach    {
608006094b9SGreg Roach        if (in_array(self::$locale->language()->code(), self::DOTLESS_I_LOCALES, true)) {
609006094b9SGreg Roach            $string = strtr($string, self::DOTLESS_I_TOUPPER);
610006094b9SGreg Roach        }
611006094b9SGreg Roach
612006094b9SGreg Roach        return mb_strtoupper($string);
613006094b9SGreg Roach    }
614006094b9SGreg Roach
615006094b9SGreg Roach    /**
616dfeee0a8SGreg Roach     * What format is used to display dates in the current locale?
617dfeee0a8SGreg Roach     *
618dfeee0a8SGreg Roach     * @return string
619dfeee0a8SGreg Roach     */
6208f53f488SRico Sonntag    public static function timeFormat(): string
621c1010edaSGreg Roach    {
622ad3143ccSGreg Roach        /* I18N: This is the format string for the time-of-day. See https://php.net/date for codes */
623bbb76c12SGreg Roach        return self::$translator->translate('%H:%i:%s');
624dfeee0a8SGreg Roach    }
625dfeee0a8SGreg Roach
626dfeee0a8SGreg Roach    /**
627dfeee0a8SGreg Roach     * Context sensitive version of translate.
628a4956c0eSGreg Roach     * echo I18N::translateContext('NOMINATIVE', 'January');
629a4956c0eSGreg Roach     * echo I18N::translateContext('GENITIVE', 'January');
630dfeee0a8SGreg Roach     *
631924d091bSGreg Roach     * @param string $context
632924d091bSGreg Roach     * @param string $message
633a515be7cSGreg Roach     * @param string ...$args
634c3283ed7SGreg Roach     *
635dfeee0a8SGreg Roach     * @return string
636dfeee0a8SGreg Roach     */
637924d091bSGreg Roach    public static function translateContext(string $context, string $message, ...$args): string
638c1010edaSGreg Roach    {
639924d091bSGreg Roach        $message = self::$translator->translateContext($context, $message);
640dfeee0a8SGreg Roach
641924d091bSGreg Roach        return sprintf($message, ...$args);
642a25f0a04SGreg Roach    }
643a25f0a04SGreg Roach}
644