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