xref: /webtrees/app/I18N.php (revision d35568b467207589ea9059739da0bf1f7e785a0d)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use Closure;
23use Collator;
24use Exception;
25use Fisharebest\Localization\Locale;
26use Fisharebest\Localization\Locale\LocaleEnUs;
27use Fisharebest\Localization\Locale\LocaleInterface;
28use Fisharebest\Localization\Translation;
29use Fisharebest\Localization\Translator;
30use Fisharebest\Webtrees\Module\ModuleCustomInterface;
31use Fisharebest\Webtrees\Module\ModuleLanguageInterface;
32use Fisharebest\Webtrees\Services\ModuleService;
33
34use function array_merge;
35use function class_exists;
36use function html_entity_decode;
37use function in_array;
38use function mb_strtolower;
39use function mb_strtoupper;
40use function mb_substr;
41use function ord;
42use function sprintf;
43use function str_contains;
44use function str_replace;
45use function strcmp;
46use function strip_tags;
47use function strlen;
48use function strtr;
49use function var_export;
50
51/**
52 * Internationalization (i18n) and localization (l10n).
53 */
54class I18N
55{
56    // MO files use special characters for plurals and context.
57    public const PLURAL  = "\x00";
58    public const CONTEXT = "\x04";
59
60    // Digits are always rendered LTR, even in RTL text.
61    private const DIGITS = '0123456789٠١٢٣٤٥٦٧٨٩۰۱۲۳۴۵۶۷۸۹';
62
63    // These locales need special handling for the dotless letter I.
64    private const DOTLESS_I_LOCALES = [
65        'az',
66        'tr',
67    ];
68
69    private const DOTLESS_I_TOLOWER = [
70        'I' => 'ı',
71        'İ' => 'i',
72    ];
73
74    private const DOTLESS_I_TOUPPER = [
75        'ı' => 'I',
76        'i' => 'İ',
77    ];
78
79    // The ranges of characters used by each script.
80    private const SCRIPT_CHARACTER_RANGES = [
81        [
82            'Latn',
83            0x0041,
84            0x005A,
85        ],
86        [
87            'Latn',
88            0x0061,
89            0x007A,
90        ],
91        [
92            'Latn',
93            0x0100,
94            0x02AF,
95        ],
96        [
97            'Grek',
98            0x0370,
99            0x03FF,
100        ],
101        [
102            'Cyrl',
103            0x0400,
104            0x052F,
105        ],
106        [
107            'Hebr',
108            0x0590,
109            0x05FF,
110        ],
111        [
112            'Arab',
113            0x0600,
114            0x06FF,
115        ],
116        [
117            'Arab',
118            0x0750,
119            0x077F,
120        ],
121        [
122            'Arab',
123            0x08A0,
124            0x08FF,
125        ],
126        [
127            'Deva',
128            0x0900,
129            0x097F,
130        ],
131        [
132            'Taml',
133            0x0B80,
134            0x0BFF,
135        ],
136        [
137            'Sinh',
138            0x0D80,
139            0x0DFF,
140        ],
141        [
142            'Thai',
143            0x0E00,
144            0x0E7F,
145        ],
146        [
147            'Geor',
148            0x10A0,
149            0x10FF,
150        ],
151        [
152            'Grek',
153            0x1F00,
154            0x1FFF,
155        ],
156        [
157            'Deva',
158            0xA8E0,
159            0xA8FF,
160        ],
161        [
162            'Hans',
163            0x3000,
164            0x303F,
165        ],
166        // Mixed CJK, not just Hans
167        [
168            'Hans',
169            0x3400,
170            0xFAFF,
171        ],
172        // Mixed CJK, not just Hans
173        [
174            'Hans',
175            0x20000,
176            0x2FA1F,
177        ],
178        // Mixed CJK, not just Hans
179    ];
180
181    // Characters that are displayed in mirror form in RTL text.
182    private const MIRROR_CHARACTERS = [
183        '('  => ')',
184        ')'  => '(',
185        '['  => ']',
186        ']'  => '[',
187        '{'  => '}',
188        '}'  => '{',
189        '<'  => '>',
190        '>'  => '<',
191        '‹ ' => '›',
192        '› ' => '‹',
193        '«'  => '»',
194        '»'  => '«',
195        '﴾ ' => '﴿',
196        '﴿ ' => '﴾',
197        '“ ' => '”',
198        '” ' => '“',
199        '‘ ' => '’',
200        '’ ' => '‘',
201    ];
202
203    // Punctuation used to separate list items, typically a comma
204    public static string $list_separator;
205
206    private static ModuleLanguageInterface $language;
207
208    private static LocaleInterface $locale;
209
210    private static Translator $translator;
211
212    private static ?Collator $collator = null;
213
214    /**
215     * The preferred locales for this site, or a default list if no preference.
216     *
217     * @return array<LocaleInterface>
218     */
219    public static function activeLocales(): array
220    {
221        $locales = Registry::container()->get(ModuleService::class)
222            ->findByInterface(ModuleLanguageInterface::class, false, true)
223            ->map(static function (ModuleLanguageInterface $module): LocaleInterface {
224                return $module->locale();
225            });
226
227        if ($locales->isEmpty()) {
228            return [new LocaleEnUs()];
229        }
230
231        return $locales->all();
232    }
233
234    /**
235     * What format is used to display dates in the current locale?
236     *
237     * @return string
238     */
239    public static function dateFormat(): string
240    {
241        /* I18N: This is the format string for full dates. See https://php.net/date for codes */
242        return self::$translator->translate('%j %F %Y');
243    }
244
245    /**
246     * Convert the digits 0-9 into the local script
247     * Used for years, etc., where we do not want thousands-separators, decimals, etc.
248     *
249     * @param string|int $n
250     *
251     * @return string
252     */
253    public static function digits(string|int $n): string
254    {
255        return self::$locale->digits((string) $n);
256    }
257
258    /**
259     * What is the direction of the current locale
260     *
261     * @return string "ltr" or "rtl"
262     */
263    public static function direction(): string
264    {
265        return self::$locale->direction();
266    }
267
268    /**
269     * Initialise the translation adapter with a locale setting.
270     *
271     * @param string $code
272     * @param bool   $setup
273     *
274     * @return void
275     */
276    public static function init(string $code, bool $setup = false): void
277    {
278        self::$locale = Locale::create($code);
279
280        // Load the translation file
281        $translation_file = __DIR__ . '/../resources/lang/' . self::$locale->languageTag() . '/messages.php';
282
283        try {
284            $translation  = new Translation($translation_file);
285            $translations = $translation->asArray();
286        } catch (Exception) {
287            // The translations files are created during the build process, and are
288            // not included in the source code.
289            // Assuming we are using dev code, and build (or rebuild) the files.
290            $po_file      = Webtrees::ROOT_DIR . 'resources/lang/' . self::$locale->languageTag() . '/messages.po';
291            $translation  = new Translation($po_file);
292            $translations = $translation->asArray();
293            file_put_contents($translation_file, "<?php\n\nreturn " . var_export($translations, true) . ";\n");
294        }
295
296        // Add translations from custom modules (but not during setup, as we have no database/modules)
297        if (!$setup) {
298            $module_service = Registry::container()->get(ModuleService::class);
299
300            $translations = $module_service
301                ->findByInterface(ModuleCustomInterface::class)
302                ->reduce(static function (array $carry, ModuleCustomInterface $item): array {
303                    return array_merge($carry, $item->customTranslations(self::$locale->languageTag()));
304                }, $translations);
305
306            self::$language = $module_service
307                ->findByInterface(ModuleLanguageInterface::class, true)
308                ->first(fn (ModuleLanguageInterface $module): bool => $module->locale()->languageTag() === $code);
309        }
310
311        // Create a translator
312        self::$translator = new Translator($translations, self::$locale->pluralRule());
313
314        /* I18N: This punctuation is used to separate lists of items */
315        self::$list_separator = self::translate(', ');
316
317        // Create a collator
318        try {
319            // Symfony provides a very incomplete polyfill - which cannot be used.
320            if (class_exists('Collator')) {
321                // Need phonebook collation rules for German Ä, Ö and Ü.
322                if (str_contains(self::$locale->code(), '@')) {
323                    self::$collator = new Collator(self::$locale->code() . ';collation=phonebook');
324                } else {
325                    self::$collator = new Collator(self::$locale->code() . '@collation=phonebook');
326                }
327                // Ignore upper/lower case differences
328                self::$collator->setStrength(Collator::SECONDARY);
329            }
330        } catch (Exception) {
331            // PHP-INTL is not installed?  We'll use a fallback later.
332        }
333    }
334
335    /**
336     * Translate a string, and then substitute placeholders
337     * echo I18N::translate('Hello World!');
338     * echo I18N::translate('The %s sat on the mat', 'cat');
339     *
340     * @param string $message
341     * @param string ...$args
342     *
343     * @return string
344     */
345    public static function translate(string $message, ...$args): string
346    {
347        $message = self::$translator->translate($message);
348
349        return sprintf($message, ...$args);
350    }
351
352    /**
353     * @return string
354     */
355    public static function languageTag(): string
356    {
357        return self::$locale->languageTag();
358    }
359
360    /**
361     * @return LocaleInterface
362     */
363    public static function locale(): LocaleInterface
364    {
365        return self::$locale;
366    }
367
368    /**
369     * @return ModuleLanguageInterface
370     */
371    public static function language(): ModuleLanguageInterface
372    {
373        return self::$language;
374    }
375
376    /**
377     * Translate a number into the local representation.
378     * e.g. 12345.67 becomes
379     * en: 12,345.67
380     * fr: 12 345,67
381     * de: 12.345,67
382     *
383     * @param float $n
384     * @param int   $precision
385     *
386     * @return string
387     */
388    public static function number(float $n, int $precision = 0): string
389    {
390        return self::$locale->number(round($n, $precision));
391    }
392
393    /**
394     * Translate a fraction into a percentage.
395     * e.g. 0.123 becomes
396     * en: 12.3%
397     * fr: 12,3 %
398     * de: 12,3%
399     *
400     * @param float $n
401     * @param int   $precision
402     *
403     * @return string
404     */
405    public static function percentage(float $n, int $precision = 0): string
406    {
407        return self::$locale->percent(round($n, $precision + 2));
408    }
409
410    /**
411     * Translate a plural string
412     * echo self::plural('There is an error', 'There are errors', $num_errors);
413     * echo self::plural('There is one error', 'There are %s errors', $num_errors);
414     * echo self::plural('There is %1$s %2$s cat', 'There are %1$s %2$s cats', $num, $num, $colour);
415     *
416     * @param string $singular
417     * @param string $plural
418     * @param int    $count
419     * @param string ...$args
420     *
421     * @return string
422     */
423    public static function plural(string $singular, string $plural, int $count, ...$args): string
424    {
425        $message = self::$translator->translatePlural($singular, $plural, $count);
426
427        return sprintf($message, ...$args);
428    }
429
430    /**
431     * UTF8 version of PHP::strrev()
432     * Reverse RTL text for third-party libraries such as GD2 and googlechart.
433     * These do not support UTF8 text direction, so we must mimic it for them.
434     * Numbers are always rendered LTR, even in RTL text.
435     * The visual direction of characters such as parentheses should be reversed.
436     *
437     * @param string $text Text to be reversed
438     *
439     * @return string
440     */
441    public static function reverseText(string $text): string
442    {
443        // Remove HTML markup - we can't display it and it is LTR.
444        $text = strip_tags($text);
445        // Remove HTML entities.
446        $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
447
448        // LTR text doesn't need reversing
449        if (self::scriptDirection(self::textScript($text)) === 'ltr') {
450            return $text;
451        }
452
453        // Mirrored characters
454        $text = strtr($text, self::MIRROR_CHARACTERS);
455
456        $reversed = '';
457        $digits   = '';
458        while ($text !== '') {
459            $letter = mb_substr($text, 0, 1);
460            $text   = mb_substr($text, 1);
461            if (str_contains(self::DIGITS, $letter)) {
462                $digits .= $letter;
463            } else {
464                $reversed = $letter . $digits . $reversed;
465                $digits   = '';
466            }
467        }
468
469        return $digits . $reversed;
470    }
471
472    /**
473     * Return the direction (ltr or rtl) for a given script
474     * The PHP/intl library does not provde this information, so we need
475     * our own lookup table.
476     *
477     * @param string $script
478     *
479     * @return string
480     */
481    public static function scriptDirection(string $script): string
482    {
483        switch ($script) {
484            case 'Arab':
485            case 'Hebr':
486            case 'Mong':
487            case 'Thaa':
488                return 'rtl';
489            default:
490                return 'ltr';
491        }
492    }
493
494    /**
495     * Identify the script used for a piece of text
496     *
497     * @param string $string
498     *
499     * @return string
500     */
501    public static function textScript(string $string): string
502    {
503        $string = strip_tags($string); // otherwise HTML tags show up as latin
504        $string = html_entity_decode($string, ENT_QUOTES, 'UTF-8'); // otherwise HTML entities show up as latin
505        $string = str_replace([
506            Individual::NOMEN_NESCIO,
507            Individual::PRAENOMEN_NESCIO,
508        ], '', $string);
509        $pos    = 0;
510        $strlen = strlen($string);
511        while ($pos < $strlen) {
512            // get the Unicode Code Point for the character at position $pos
513            $byte1 = ord($string[$pos]);
514            if ($byte1 < 0x80) {
515                $code_point = $byte1;
516                $chrlen     = 1;
517            } elseif ($byte1 < 0xC0) {
518                // Invalid continuation character
519                return 'Latn';
520            } elseif ($byte1 < 0xE0) {
521                $code_point = (($byte1 & 0x1F) << 6) + (ord($string[$pos + 1]) & 0x3F);
522                $chrlen     = 2;
523            } elseif ($byte1 < 0xF0) {
524                $code_point = (($byte1 & 0x0F) << 12) + ((ord($string[$pos + 1]) & 0x3F) << 6) + (ord($string[$pos + 2]) & 0x3F);
525                $chrlen     = 3;
526            } elseif ($byte1 < 0xF8) {
527                $code_point = (($byte1 & 0x07) << 24) + ((ord($string[$pos + 1]) & 0x3F) << 12) + ((ord($string[$pos + 2]) & 0x3F) << 6) + (ord($string[$pos + 3]) & 0x3F);
528                $chrlen     = 3;
529            } else {
530                // Invalid UTF
531                return 'Latn';
532            }
533
534            foreach (self::SCRIPT_CHARACTER_RANGES as $range) {
535                if ($code_point >= $range[1] && $code_point <= $range[2]) {
536                    return $range[0];
537                }
538            }
539            // Not a recognised script. Maybe punctuation, spacing, etc. Keep looking.
540            $pos += $chrlen;
541        }
542
543        return 'Latn';
544    }
545
546    /**
547     * A closure which will compare strings using local collation rules.
548     *
549     * @return Closure(string,string):int
550     */
551    public static function comparator(): Closure
552    {
553        $collator = self::$collator;
554
555        if ($collator instanceof Collator) {
556            return static fn (string $x, string $y): int => (int) $collator->compare($x, $y);
557        }
558
559        return static fn (string $x, string $y): int => strcmp(self::strtolower($x), self::strtolower($y));
560    }
561
562
563
564    /**
565     * Convert a string to lower case.
566     *
567     * @param string $string
568     *
569     * @return string
570     */
571    public static function strtolower(string $string): string
572    {
573        if (in_array(self::$locale->language()->code(), self::DOTLESS_I_LOCALES, true)) {
574            $string = strtr($string, self::DOTLESS_I_TOLOWER);
575        }
576
577        return mb_strtolower($string);
578    }
579
580    /**
581     * Convert a string to upper case.
582     *
583     * @param string $string
584     *
585     * @return string
586     */
587    public static function strtoupper(string $string): string
588    {
589        if (in_array(self::$locale->language()->code(), self::DOTLESS_I_LOCALES, true)) {
590            $string = strtr($string, self::DOTLESS_I_TOUPPER);
591        }
592
593        return mb_strtoupper($string);
594    }
595
596    /**
597     * What format is used to display dates in the current locale?
598     *
599     * @return string
600     */
601    public static function timeFormat(): string
602    {
603        /* I18N: This is the format string for the time-of-day. See https://php.net/date for codes */
604        return self::$translator->translate('%H:%i:%s');
605    }
606
607    /**
608     * Context sensitive version of translate.
609     * echo I18N::translateContext('NOMINATIVE', 'January');
610     * echo I18N::translateContext('GENITIVE', 'January');
611     *
612     * @param string $context
613     * @param string $message
614     * @param string ...$args
615     *
616     * @return string
617     */
618    public static function translateContext(string $context, string $message, ...$args): string
619    {
620        $message = self::$translator->translateContext($context, $message);
621
622        return sprintf($message, ...$args);
623    }
624}
625