Passed
Push — master ( 316db8...97e0d0 )
by Greg
05:28
created

I18N::htmlAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees;
21
22
use Collator;
23
use Exception;
24
use Fisharebest\Localization\Locale;
25
use Fisharebest\Localization\Locale\LocaleEnUs;
26
use Fisharebest\Localization\Locale\LocaleInterface;
27
use Fisharebest\Localization\Translation;
28
use Fisharebest\Localization\Translator;
29
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
30
use Fisharebest\Webtrees\Module\ModuleLanguageInterface;
31
use Fisharebest\Webtrees\Services\ModuleService;
32
use Illuminate\Support\Collection;
33
34
use function array_merge;
35
use function class_exists;
36
use function html_entity_decode;
37
use function in_array;
38
use function mb_strtolower;
39
use function mb_strtoupper;
40
use function mb_substr;
41
use function ord;
42
use function sprintf;
43
use function str_replace;
44
use function strcmp;
45
use function strip_tags;
46
use function strlen;
47
use function strpos;
48
use function strtr;
49
50
/**
51
 * Internationalization (i18n) and localization (l10n).
52
 */
53
class 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
     * Initialise the translation adapter with a locale setting.
284
     *
285
     * @param string $code
286
     * @param bool   $setup
287
     *
288
     * @return void
289
     */
290
    public static function init(string $code, bool $setup = false): void
291
    {
292
        self::$locale = Locale::create($code);
293
294
        // Load the translation file
295
        $translation_file = __DIR__ . '/../resources/lang/' . self::$locale->languageTag() . '/messages.php';
296
297
        try {
298
            $translation  = new Translation($translation_file);
299
            $translations = $translation->asArray();
300
        } catch (Exception $ex) {
301
            // The translations files are created during the build process, and are
302
            // not included in the source code.
303
            // Assuming we are using dev code, and build (or rebuild) the files.
304
            $po_file      = Webtrees::ROOT_DIR . 'resources/lang/' . self::$locale->languageTag() . '/messages.po';
305
            $translation  = new Translation($po_file);
306
            $translations = $translation->asArray();
307
            file_put_contents($translation_file, '<?php return ' . var_export($translations, true) . ';');
308
        }
309
310
        // Add translations from custom modules (but not during setup, as we have no database/modules)
311
        if (!$setup) {
312
            $translations = app(ModuleService::class)
313
                ->findByInterface(ModuleCustomInterface::class)
314
                ->reduce(static function (array $carry, ModuleCustomInterface $item): array {
315
                    return array_merge($carry, $item->customTranslations(self::$locale->languageTag()));
316
                }, $translations);
317
        }
318
319
        // Create a translator
320
        self::$translator = new Translator($translations, self::$locale->pluralRule());
321
322
        /* I18N: This punctuation is used to separate lists of items */
323
        self::$list_separator = self::translate(', ');
324
325
        // Create a collator
326
        try {
327
            if (class_exists('Collator')) {
328
                // Symfony provides a very incomplete polyfill - which cannot be used.
329
                self::$collator = new Collator(self::$locale->code());
330
                // Ignore upper/lower case differences
331
                self::$collator->setStrength(Collator::SECONDARY);
332
            }
333
        } catch (Exception $ex) {
334
            // PHP-INTL is not installed?  We'll use a fallback later.
335
            self::$collator = null;
336
        }
337
    }
338
339
    /**
340
     * Translate a string, and then substitute placeholders
341
     * echo I18N::translate('Hello World!');
342
     * echo I18N::translate('The %s sat on the mat', 'cat');
343
     *
344
     * @param string $message
345
     * @param string ...$args
346
     *
347
     * @return string
348
     */
349
    public static function translate(string $message, ...$args): string
350
    {
351
        $message = self::$translator->translate($message);
352
353
        return sprintf($message, ...$args);
354
    }
355
356
    /**
357
     * @return string
358
     */
359
    public static function languageTag(): string
360
    {
361
        return self::$locale->languageTag();
362
    }
363
364
    /**
365
     * @return LocaleInterface
366
     */
367
    public static function locale(): LocaleInterface
368
    {
369
        return self::$locale;
370
    }
371
372
    /**
373
     * Translate a number into the local representation.
374
     * e.g. 12345.67 becomes
375
     * en: 12,345.67
376
     * fr: 12 345,67
377
     * de: 12.345,67
378
     *
379
     * @param float $n
380
     * @param int   $precision
381
     *
382
     * @return string
383
     */
384
    public static function number(float $n, int $precision = 0): string
385
    {
386
        return self::$locale->number(round($n, $precision));
387
    }
388
389
    /**
390
     * Translate a fraction into a percentage.
391
     * e.g. 0.123 becomes
392
     * en: 12.3%
393
     * fr: 12,3 %
394
     * de: 12,3%
395
     *
396
     * @param float $n
397
     * @param int   $precision
398
     *
399
     * @return string
400
     */
401
    public static function percentage(float $n, int $precision = 0): string
402
    {
403
        return self::$locale->percent(round($n, $precision + 2));
404
    }
405
406
    /**
407
     * Translate a plural string
408
     * echo self::plural('There is an error', 'There are errors', $num_errors);
409
     * echo self::plural('There is one error', 'There are %s errors', $num_errors);
410
     * echo self::plural('There is %1$s %2$s cat', 'There are %1$s %2$s cats', $num, $num, $colour);
411
     *
412
     * @param string $singular
413
     * @param string $plural
414
     * @param int    $count
415
     * @param string ...$args
416
     *
417
     * @return string
418
     */
419
    public static function plural(string $singular, string $plural, int $count, ...$args): string
420
    {
421
        $message = self::$translator->translatePlural($singular, $plural, $count);
422
423
        return sprintf($message, ...$args);
424
    }
425
426
    /**
427
     * UTF8 version of PHP::strrev()
428
     * Reverse RTL text for third-party libraries such as GD2 and googlechart.
429
     * These do not support UTF8 text direction, so we must mimic it for them.
430
     * Numbers are always rendered LTR, even in RTL text.
431
     * The visual direction of characters such as parentheses should be reversed.
432
     *
433
     * @param string $text Text to be reversed
434
     *
435
     * @return string
436
     */
437
    public static function reverseText($text): string
438
    {
439
        // Remove HTML markup - we can't display it and it is LTR.
440
        $text = strip_tags($text);
441
        // Remove HTML entities.
442
        $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
443
444
        // LTR text doesn't need reversing
445
        if (self::scriptDirection(self::textScript($text)) === 'ltr') {
446
            return $text;
447
        }
448
449
        // Mirrored characters
450
        $text = strtr($text, self::MIRROR_CHARACTERS);
451
452
        $reversed = '';
453
        $digits   = '';
454
        while ($text !== '') {
455
            $letter = mb_substr($text, 0, 1);
456
            $text   = mb_substr($text, 1);
457
            if (strpos(self::DIGITS, $letter) !== false) {
458
                $digits .= $letter;
459
            } else {
460
                $reversed = $letter . $digits . $reversed;
461
                $digits   = '';
462
            }
463
        }
464
465
        return $digits . $reversed;
466
    }
467
468
    /**
469
     * Return the direction (ltr or rtl) for a given script
470
     * The PHP/intl library does not provde this information, so we need
471
     * our own lookup table.
472
     *
473
     * @param string $script
474
     *
475
     * @return string
476
     */
477
    public static function scriptDirection($script): string
478
    {
479
        switch ($script) {
480
            case 'Arab':
481
            case 'Hebr':
482
            case 'Mong':
483
            case 'Thaa':
484
                return 'rtl';
485
            default:
486
                return 'ltr';
487
        }
488
    }
489
490
    /**
491
     * Identify the script used for a piece of text
492
     *
493
     * @param string $string
494
     *
495
     * @return string
496
     */
497
    public static function textScript($string): string
498
    {
499
        $string = strip_tags($string); // otherwise HTML tags show up as latin
500
        $string = html_entity_decode($string, ENT_QUOTES, 'UTF-8'); // otherwise HTML entities show up as latin
501
        $string = str_replace([
502
            '@N.N.',
503
            '@P.N.',
504
        ], '', $string); // otherwise unknown names show up as latin
505
        $pos    = 0;
506
        $strlen = strlen($string);
507
        while ($pos < $strlen) {
508
            // get the Unicode Code Point for the character at position $pos
509
            $byte1 = ord($string[$pos]);
510
            if ($byte1 < 0x80) {
511
                $code_point = $byte1;
512
                $chrlen     = 1;
513
            } elseif ($byte1 < 0xC0) {
514
                // Invalid continuation character
515
                return 'Latn';
516
            } elseif ($byte1 < 0xE0) {
517
                $code_point = (($byte1 & 0x1F) << 6) + (ord($string[$pos + 1]) & 0x3F);
518
                $chrlen     = 2;
519
            } elseif ($byte1 < 0xF0) {
520
                $code_point = (($byte1 & 0x0F) << 12) + ((ord($string[$pos + 1]) & 0x3F) << 6) + (ord($string[$pos + 2]) & 0x3F);
521
                $chrlen     = 3;
522
            } elseif ($byte1 < 0xF8) {
523
                $code_point = (($byte1 & 0x07) << 24) + ((ord($string[$pos + 1]) & 0x3F) << 12) + ((ord($string[$pos + 2]) & 0x3F) << 6) + (ord($string[$pos + 3]) & 0x3F);
524
                $chrlen     = 3;
525
            } else {
526
                // Invalid UTF
527
                return 'Latn';
528
            }
529
530
            foreach (self::SCRIPT_CHARACTER_RANGES as $range) {
531
                if ($code_point >= $range[1] && $code_point <= $range[2]) {
532
                    return $range[0];
533
                }
534
            }
535
            // Not a recognised script. Maybe punctuation, spacing, etc. Keep looking.
536
            $pos += $chrlen;
537
        }
538
539
        return 'Latn';
540
    }
541
542
    /**
543
     * Perform a case-insensitive comparison of two strings.
544
     *
545
     * @param string $string1
546
     * @param string $string2
547
     *
548
     * @return int
549
     */
550
    public static function strcasecmp($string1, $string2): int
551
    {
552
        if (self::$collator instanceof Collator) {
553
            return self::$collator->compare($string1, $string2);
554
        }
555
556
        return strcmp(self::strtolower($string1), self::strtolower($string2));
557
    }
558
559
    /**
560
     * Convert a string to lower case.
561
     *
562
     * @param string $string
563
     *
564
     * @return string
565
     */
566
    public static function strtolower($string): string
567
    {
568
        if (in_array(self::$locale->language()->code(), self::DOTLESS_I_LOCALES, true)) {
569
            $string = strtr($string, self::DOTLESS_I_TOLOWER);
570
        }
571
572
        return mb_strtolower($string);
573
    }
574
575
    /**
576
     * Convert a string to upper case.
577
     *
578
     * @param string $string
579
     *
580
     * @return string
581
     */
582
    public static function strtoupper($string): string
583
    {
584
        if (in_array(self::$locale->language()->code(), self::DOTLESS_I_LOCALES, true)) {
585
            $string = strtr($string, self::DOTLESS_I_TOUPPER);
586
        }
587
588
        return mb_strtoupper($string);
589
    }
590
591
    /**
592
     * What format is used to display dates in the current locale?
593
     *
594
     * @return string
595
     */
596
    public static function timeFormat(): string
597
    {
598
        /* I18N: This is the format string for the time-of-day. See http://php.net/date for codes */
599
        return self::$translator->translate('%H:%i:%s');
600
    }
601
602
    /**
603
     * Context sensitive version of translate.
604
     * echo I18N::translateContext('NOMINATIVE', 'January');
605
     * echo I18N::translateContext('GENITIVE', 'January');
606
     *
607
     * @param string $context
608
     * @param string $message
609
     * @param string ...$args
610
     *
611
     * @return string
612
     */
613
    public static function translateContext(string $context, string $message, ...$args): string
614
    {
615
        $message = self::$translator->translateContext($context, $message);
616
617
        return sprintf($message, ...$args);
618
    }
619
}
620