1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify 6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by 7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or 8a25f0a04SGreg Roach * (at your option) any later version. 9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful, 10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12a25f0a04SGreg Roach * GNU General Public License for more details. 13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License 14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 15a25f0a04SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees; 19a25f0a04SGreg Roach 20991b93ddSGreg Roachuse Collator; 21f1af7e1cSGreg Roachuse Exception; 22c999a340SGreg Roachuse Fisharebest\Localization\Locale; 231e71bdc0SGreg Roachuse Fisharebest\Localization\Locale\LocaleEnUs; 2415834aaeSGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface; 253bdc890bSGreg Roachuse Fisharebest\Localization\Translation; 263bdc890bSGreg Roachuse Fisharebest\Localization\Translator; 27d37db671SGreg Roachuse Fisharebest\Webtrees\Module\ModuleCustomInterface; 2802086832SGreg Roachuse Fisharebest\Webtrees\Module\ModuleLanguageInterface; 29d37db671SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 306cd97bf6SGreg Roachuse Illuminate\Support\Collection; 314f194b97SGreg Roachuse function array_merge; 32d68ee7a8SGreg Roachuse function class_exists; 334f194b97SGreg Roachuse function filemtime; 34d68ee7a8SGreg Roachuse function file_exists; 35d68ee7a8SGreg Roachuse function html_entity_decode; 36d68ee7a8SGreg Roachuse function in_array; 37d68ee7a8SGreg Roachuse function intdiv; 38d68ee7a8SGreg Roachuse function mb_strtolower; 39d68ee7a8SGreg Roachuse function mb_strtoupper; 40d68ee7a8SGreg Roachuse function mb_substr; 41d68ee7a8SGreg Roachuse function ord; 42d68ee7a8SGreg Roachuse function sprintf; 43d68ee7a8SGreg Roachuse function str_replace; 44d68ee7a8SGreg Roachuse function strcmp; 45d68ee7a8SGreg Roachuse function strip_tags; 46d68ee7a8SGreg Roachuse function strlen; 47d68ee7a8SGreg Roachuse function strpos; 48d68ee7a8SGreg Roachuse function strtr; 49a25f0a04SGreg Roach 50a25f0a04SGreg Roach/** 5176692c8bSGreg Roach * Internationalization (i18n) and localization (l10n). 52a25f0a04SGreg Roach */ 53c1010edaSGreg Roachclass I18N 54c1010edaSGreg Roach{ 55d37db671SGreg Roach // MO files use special characters for plurals and context. 564f194b97SGreg Roach public const PLURAL = "\x00"; 574f194b97SGreg Roach public const CONTEXT = "\x04"; 58d37db671SGreg Roach 5915834aaeSGreg Roach /** @var LocaleInterface The current locale (e.g. LocaleEnGb) */ 60c999a340SGreg Roach private static $locale; 61c999a340SGreg Roach 6276692c8bSGreg Roach /** @var Translator An object that performs translation */ 633bdc890bSGreg Roach private static $translator; 643bdc890bSGreg Roach 65c9ec599fSGreg Roach /** @var Collator|null From the php-intl library */ 66991b93ddSGreg Roach private static $collator; 67991b93ddSGreg Roach 68a25f0a04SGreg Roach // Digits are always rendered LTR, even in RTL text. 6916d6367aSGreg Roach private const DIGITS = '0123456789٠١٢٣٤٥٦٧٨٩۰۱۲۳۴۵۶۷۸۹'; 70a25f0a04SGreg Roach 71991b93ddSGreg Roach // These locales need special handling for the dotless letter I. 7216d6367aSGreg Roach private const DOTLESS_I_LOCALES = [ 73c1010edaSGreg Roach 'az', 74c1010edaSGreg Roach 'tr', 75c1010edaSGreg Roach ]; 7616d6367aSGreg Roach private const DOTLESS_I_TOLOWER = [ 77c1010edaSGreg Roach 'I' => 'ı', 78c1010edaSGreg Roach 'İ' => 'i', 79c1010edaSGreg Roach ]; 8016d6367aSGreg Roach private const DOTLESS_I_TOUPPER = [ 81c1010edaSGreg Roach 'ı' => 'I', 82c1010edaSGreg Roach 'i' => 'İ', 83c1010edaSGreg Roach ]; 84a25f0a04SGreg Roach 85991b93ddSGreg Roach // The ranges of characters used by each script. 8616d6367aSGreg Roach private const SCRIPT_CHARACTER_RANGES = [ 87c1010edaSGreg Roach [ 88c1010edaSGreg Roach 'Latn', 89c1010edaSGreg Roach 0x0041, 90c1010edaSGreg Roach 0x005A, 91c1010edaSGreg Roach ], 92c1010edaSGreg Roach [ 93c1010edaSGreg Roach 'Latn', 94c1010edaSGreg Roach 0x0061, 95c1010edaSGreg Roach 0x007A, 96c1010edaSGreg Roach ], 97c1010edaSGreg Roach [ 98c1010edaSGreg Roach 'Latn', 99c1010edaSGreg Roach 0x0100, 100c1010edaSGreg Roach 0x02AF, 101c1010edaSGreg Roach ], 102c1010edaSGreg Roach [ 103c1010edaSGreg Roach 'Grek', 104c1010edaSGreg Roach 0x0370, 105c1010edaSGreg Roach 0x03FF, 106c1010edaSGreg Roach ], 107c1010edaSGreg Roach [ 108c1010edaSGreg Roach 'Cyrl', 109c1010edaSGreg Roach 0x0400, 110c1010edaSGreg Roach 0x052F, 111c1010edaSGreg Roach ], 112c1010edaSGreg Roach [ 113c1010edaSGreg Roach 'Hebr', 114c1010edaSGreg Roach 0x0590, 115c1010edaSGreg Roach 0x05FF, 116c1010edaSGreg Roach ], 117c1010edaSGreg Roach [ 118c1010edaSGreg Roach 'Arab', 119c1010edaSGreg Roach 0x0600, 120c1010edaSGreg Roach 0x06FF, 121c1010edaSGreg Roach ], 122c1010edaSGreg Roach [ 123c1010edaSGreg Roach 'Arab', 124c1010edaSGreg Roach 0x0750, 125c1010edaSGreg Roach 0x077F, 126c1010edaSGreg Roach ], 127c1010edaSGreg Roach [ 128c1010edaSGreg Roach 'Arab', 129c1010edaSGreg Roach 0x08A0, 130c1010edaSGreg Roach 0x08FF, 131c1010edaSGreg Roach ], 132c1010edaSGreg Roach [ 133c1010edaSGreg Roach 'Deva', 134c1010edaSGreg Roach 0x0900, 135c1010edaSGreg Roach 0x097F, 136c1010edaSGreg Roach ], 137c1010edaSGreg Roach [ 138c1010edaSGreg Roach 'Taml', 139c1010edaSGreg Roach 0x0B80, 140c1010edaSGreg Roach 0x0BFF, 141c1010edaSGreg Roach ], 142c1010edaSGreg Roach [ 143c1010edaSGreg Roach 'Sinh', 144c1010edaSGreg Roach 0x0D80, 145c1010edaSGreg Roach 0x0DFF, 146c1010edaSGreg Roach ], 147c1010edaSGreg Roach [ 148c1010edaSGreg Roach 'Thai', 149c1010edaSGreg Roach 0x0E00, 150c1010edaSGreg Roach 0x0E7F, 151c1010edaSGreg Roach ], 152c1010edaSGreg Roach [ 153c1010edaSGreg Roach 'Geor', 154c1010edaSGreg Roach 0x10A0, 155c1010edaSGreg Roach 0x10FF, 156c1010edaSGreg Roach ], 157c1010edaSGreg Roach [ 158c1010edaSGreg Roach 'Grek', 159c1010edaSGreg Roach 0x1F00, 160c1010edaSGreg Roach 0x1FFF, 161c1010edaSGreg Roach ], 162c1010edaSGreg Roach [ 163c1010edaSGreg Roach 'Deva', 164c1010edaSGreg Roach 0xA8E0, 165c1010edaSGreg Roach 0xA8FF, 166c1010edaSGreg Roach ], 167c1010edaSGreg Roach [ 168c1010edaSGreg Roach 'Hans', 169c1010edaSGreg Roach 0x3000, 170c1010edaSGreg Roach 0x303F, 171c1010edaSGreg Roach ], 172c1010edaSGreg Roach // Mixed CJK, not just Hans 173c1010edaSGreg Roach [ 174c1010edaSGreg Roach 'Hans', 175c1010edaSGreg Roach 0x3400, 176c1010edaSGreg Roach 0xFAFF, 177c1010edaSGreg Roach ], 178c1010edaSGreg Roach // Mixed CJK, not just Hans 179c1010edaSGreg Roach [ 180c1010edaSGreg Roach 'Hans', 181c1010edaSGreg Roach 0x20000, 182c1010edaSGreg Roach 0x2FA1F, 183c1010edaSGreg Roach ], 184c1010edaSGreg Roach // Mixed CJK, not just Hans 18513abd6f3SGreg Roach ]; 186a25f0a04SGreg Roach 187991b93ddSGreg Roach // Characters that are displayed in mirror form in RTL text. 18816d6367aSGreg Roach private const MIRROR_CHARACTERS = [ 189a25f0a04SGreg Roach '(' => ')', 190a25f0a04SGreg Roach ')' => '(', 191a25f0a04SGreg Roach '[' => ']', 192a25f0a04SGreg Roach ']' => '[', 193a25f0a04SGreg Roach '{' => '}', 194a25f0a04SGreg Roach '}' => '{', 195a25f0a04SGreg Roach '<' => '>', 196a25f0a04SGreg Roach '>' => '<', 197a25f0a04SGreg Roach '‹ ' => '›', 198a25f0a04SGreg Roach '› ' => '‹', 199a25f0a04SGreg Roach '«' => '»', 200a25f0a04SGreg Roach '»' => '«', 201a25f0a04SGreg Roach '﴾ ' => '﴿', 202a25f0a04SGreg Roach '﴿ ' => '﴾', 203a25f0a04SGreg Roach '“ ' => '”', 204a25f0a04SGreg Roach '” ' => '“', 205a25f0a04SGreg Roach '‘ ' => '’', 206a25f0a04SGreg Roach '’ ' => '‘', 20713abd6f3SGreg Roach ]; 208a25f0a04SGreg Roach 209a25f0a04SGreg Roach /** @var string Punctuation used to separate list items, typically a comma */ 210a25f0a04SGreg Roach public static $list_separator; 211a25f0a04SGreg Roach 212a25f0a04SGreg Roach /** 21302086832SGreg Roach * The preferred locales for this site, or a default list if no preference. 214dfeee0a8SGreg Roach * 215dfeee0a8SGreg Roach * @return LocaleInterface[] 216dfeee0a8SGreg Roach */ 2178f53f488SRico Sonntag public static function activeLocales(): array 218c1010edaSGreg Roach { 21902086832SGreg Roach $locales = app(ModuleService::class) 220d6137952SGreg Roach ->findByInterface(ModuleLanguageInterface::class, false, true) 2210b5fd0a6SGreg Roach ->map(static function (ModuleLanguageInterface $module): LocaleInterface { 22202086832SGreg Roach return $module->locale(); 22302086832SGreg Roach }); 224dfeee0a8SGreg Roach 22502086832SGreg Roach if ($locales->isEmpty()) { 22602086832SGreg Roach return [new LocaleEnUs()]; 227dfeee0a8SGreg Roach } 228dfeee0a8SGreg Roach 22902086832SGreg Roach return $locales->all(); 230dfeee0a8SGreg Roach } 231dfeee0a8SGreg Roach 232dfeee0a8SGreg Roach /** 233dfeee0a8SGreg Roach * Which MySQL collation should be used for this locale? 234dfeee0a8SGreg Roach * 235dfeee0a8SGreg Roach * @return string 236dfeee0a8SGreg Roach */ 237e364afe4SGreg Roach public static function collation(): string 238c1010edaSGreg Roach { 239dfeee0a8SGreg Roach $collation = self::$locale->collation(); 240dfeee0a8SGreg Roach switch ($collation) { 241dfeee0a8SGreg Roach case 'croatian_ci': 242dfeee0a8SGreg Roach case 'german2_ci': 243dfeee0a8SGreg Roach case 'vietnamese_ci': 244dfeee0a8SGreg Roach // Only available in MySQL 5.6 245dfeee0a8SGreg Roach return 'utf8_unicode_ci'; 246dfeee0a8SGreg Roach default: 247dfeee0a8SGreg Roach return 'utf8_' . $collation; 248dfeee0a8SGreg Roach } 249dfeee0a8SGreg Roach } 250dfeee0a8SGreg Roach 251dfeee0a8SGreg Roach /** 252dfeee0a8SGreg Roach * What format is used to display dates in the current locale? 253dfeee0a8SGreg Roach * 254dfeee0a8SGreg Roach * @return string 255dfeee0a8SGreg Roach */ 2568f53f488SRico Sonntag public static function dateFormat(): string 257c1010edaSGreg Roach { 258bbb76c12SGreg Roach /* I18N: This is the format string for full dates. See http://php.net/date for codes */ 259bbb76c12SGreg Roach return self::$translator->translate('%j %F %Y'); 260dfeee0a8SGreg Roach } 261dfeee0a8SGreg Roach 262dfeee0a8SGreg Roach /** 263dfeee0a8SGreg Roach * Convert the digits 0-9 into the local script 264dfeee0a8SGreg Roach * Used for years, etc., where we do not want thousands-separators, decimals, etc. 265dfeee0a8SGreg Roach * 26655664801SGreg Roach * @param string|int $n 267dfeee0a8SGreg Roach * 268dfeee0a8SGreg Roach * @return string 269dfeee0a8SGreg Roach */ 2708f53f488SRico Sonntag public static function digits($n): string 271c1010edaSGreg Roach { 27255664801SGreg Roach return self::$locale->digits((string) $n); 273dfeee0a8SGreg Roach } 274dfeee0a8SGreg Roach 275dfeee0a8SGreg Roach /** 276dfeee0a8SGreg Roach * What is the direction of the current locale 277dfeee0a8SGreg Roach * 278dfeee0a8SGreg Roach * @return string "ltr" or "rtl" 279dfeee0a8SGreg Roach */ 2808f53f488SRico Sonntag public static function direction(): string 281c1010edaSGreg Roach { 282dfeee0a8SGreg Roach return self::$locale->direction(); 283dfeee0a8SGreg Roach } 284dfeee0a8SGreg Roach 285dfeee0a8SGreg Roach /** 2867231a557SGreg Roach * What is the first day of the week. 2877231a557SGreg Roach * 288cbc1590aSGreg Roach * @return int Sunday=0, Monday=1, etc. 2897231a557SGreg Roach */ 2908f53f488SRico Sonntag public static function firstDay(): int 291c1010edaSGreg Roach { 2927231a557SGreg Roach return self::$locale->territory()->firstDay(); 2937231a557SGreg Roach } 2947231a557SGreg Roach 2957231a557SGreg Roach /** 296dfeee0a8SGreg Roach * Generate i18n markup for the <html> tag, e.g. lang="ar" dir="rtl" 297dfeee0a8SGreg Roach * 298dfeee0a8SGreg Roach * @return string 299dfeee0a8SGreg Roach */ 3008f53f488SRico Sonntag public static function htmlAttributes(): string 301c1010edaSGreg Roach { 302dfeee0a8SGreg Roach return self::$locale->htmlAttributes(); 303dfeee0a8SGreg Roach } 304dfeee0a8SGreg Roach 305dfeee0a8SGreg Roach /** 306a25f0a04SGreg Roach * Initialise the translation adapter with a locale setting. 307a25f0a04SGreg Roach * 30815d603e7SGreg Roach * @param string $code Use this locale/language code, or choose one automatically 309e58a20ffSGreg Roach * @param Tree|null $tree 310c116a5ccSGreg Roach * @param bool $setup During setup, we cannot access the database. 311a25f0a04SGreg Roach * 312a25f0a04SGreg Roach * @return string $string 313a25f0a04SGreg Roach */ 314081ddc56SGreg Roach public static function init(string $code = '', Tree $tree = null, $setup = false): string 315c1010edaSGreg Roach { 31615d603e7SGreg Roach if ($code !== '') { 3173bdc890bSGreg Roach // Create the specified locale 3183bdc890bSGreg Roach self::$locale = Locale::create($code); 319*a0801ffbSGreg Roach } elseif (Session::has('language') && file_exists(WT_ROOT . 'resources/lang/' . Session::get('language') . '/messages.mo')) { 320e58a20ffSGreg Roach // Select a previously used locale 321*a0801ffbSGreg Roach self::$locale = Locale::create(Session::get('language')); 3223bdc890bSGreg Roach } else { 323e58a20ffSGreg Roach if ($tree instanceof Tree) { 324e58a20ffSGreg Roach $default_locale = Locale::create($tree->getPreference('LANGUAGE', 'en-US')); 325e58a20ffSGreg Roach } else { 32659f2f229SGreg Roach $default_locale = new LocaleEnUs(); 3273bdc890bSGreg Roach } 328e58a20ffSGreg Roach 329e58a20ffSGreg Roach // Negotiate with the browser. 330e58a20ffSGreg Roach // Search engines don't negotiate. They get the default locale of the tree. 331c116a5ccSGreg Roach if ($setup) { 332c116a5ccSGreg Roach $installed_locales = app(ModuleService::class)->setupLanguages() 3330b5fd0a6SGreg Roach ->map(static function (ModuleLanguageInterface $module): LocaleInterface { 334c116a5ccSGreg Roach return $module->locale(); 335c116a5ccSGreg Roach }); 336c116a5ccSGreg Roach } else { 337c116a5ccSGreg Roach $installed_locales = self::installedLocales(); 338c116a5ccSGreg Roach } 339c116a5ccSGreg Roach 340c116a5ccSGreg Roach self::$locale = Locale::httpAcceptLanguage($_SERVER, $installed_locales->all(), $default_locale); 3413bdc890bSGreg Roach } 3423bdc890bSGreg Roach 343f1af7e1cSGreg Roach $cache_dir = WT_DATA_DIR . 'cache/'; 344f1af7e1cSGreg Roach $cache_file = $cache_dir . 'language-' . self::$locale->languageTag() . '-cache.php'; 3453bdc890bSGreg Roach if (file_exists($cache_file)) { 3463bdc890bSGreg Roach $filemtime = filemtime($cache_file); 3473bdc890bSGreg Roach } else { 3483bdc890bSGreg Roach $filemtime = 0; 3493bdc890bSGreg Roach } 3503bdc890bSGreg Roach 3514f194b97SGreg Roach // Load the translation file 3524f194b97SGreg Roach $translation_file = WT_ROOT . 'resources/lang/' . self::$locale->languageTag() . '/messages.mo'; 353362b8464SGreg Roach 354d68ee7a8SGreg Roach if (!file_exists($translation_file)) { 355d68ee7a8SGreg Roach // Test and dev environments may not have the compiled translations 356d68ee7a8SGreg Roach $translations = []; 357d68ee7a8SGreg Roach } elseif (filemtime($translation_file) > $filemtime) { 3583bdc890bSGreg Roach $translation = new Translation($translation_file); 3594f194b97SGreg Roach $translations = $translation->asArray(); 3604f194b97SGreg Roach 361f1af7e1cSGreg Roach try { 362f1af7e1cSGreg Roach File::mkdir($cache_dir); 363f1af7e1cSGreg Roach file_put_contents($cache_file, '<?php return ' . var_export($translations, true) . ';'); 364f1af7e1cSGreg Roach } catch (Exception $ex) { 3657c2999b4SGreg Roach // During setup, we may not have been able to create it. 366c85fb0c4SGreg Roach } 3673bdc890bSGreg Roach } else { 3683bdc890bSGreg Roach $translations = include $cache_file; 369a25f0a04SGreg Roach } 370a25f0a04SGreg Roach 3714f194b97SGreg Roach // Add translations from custom modules (but not during setup, as we have no database/modules) 372c116a5ccSGreg Roach if (!$setup) { 3734f194b97SGreg Roach $translations = app(ModuleService::class) 3744f194b97SGreg Roach ->findByInterface(ModuleCustomInterface::class) 37569253da9SGreg Roach ->reduce(static function (array $carry, ModuleCustomInterface $item): array { 3764f194b97SGreg Roach return array_merge($carry, $item->customTranslations(self::$locale->languageTag())); 3774f194b97SGreg Roach }, $translations); 378d37db671SGreg Roach } 379d37db671SGreg Roach 3803bdc890bSGreg Roach // Create a translator 3813bdc890bSGreg Roach self::$translator = new Translator($translations, self::$locale->pluralRule()); 382a25f0a04SGreg Roach 383bbb76c12SGreg Roach /* I18N: This punctuation is used to separate lists of items */ 384bbb76c12SGreg Roach self::$list_separator = self::translate(', '); 385a25f0a04SGreg Roach 386991b93ddSGreg Roach // Create a collator 387991b93ddSGreg Roach try { 388444a65ecSGreg Roach if (class_exists('Collator')) { 389c9ec599fSGreg Roach // Symfony provides a very incomplete polyfill - which cannot be used. 390991b93ddSGreg Roach self::$collator = new Collator(self::$locale->code()); 391991b93ddSGreg Roach // Ignore upper/lower case differences 392991b93ddSGreg Roach self::$collator->setStrength(Collator::SECONDARY); 393444a65ecSGreg Roach } 394991b93ddSGreg Roach } catch (Exception $ex) { 395991b93ddSGreg Roach // PHP-INTL is not installed? We'll use a fallback later. 396c9ec599fSGreg Roach self::$collator = null; 397991b93ddSGreg Roach } 398991b93ddSGreg Roach 3995331c5eaSGreg Roach return self::$locale->languageTag(); 400a25f0a04SGreg Roach } 401a25f0a04SGreg Roach 402a25f0a04SGreg Roach /** 403c999a340SGreg Roach * All locales for which a translation file exists. 404c999a340SGreg Roach * 405c116a5ccSGreg Roach * @return Collection 40615834aaeSGreg Roach * @return LocaleInterface[] 407c999a340SGreg Roach */ 408c116a5ccSGreg Roach public static function installedLocales(): Collection 409c1010edaSGreg Roach { 41002086832SGreg Roach return app(ModuleService::class) 41102086832SGreg Roach ->findByInterface(ModuleLanguageInterface::class, true) 4120b5fd0a6SGreg Roach ->map(static function (ModuleLanguageInterface $module): LocaleInterface { 41302086832SGreg Roach return $module->locale(); 414c116a5ccSGreg Roach }); 415a25f0a04SGreg Roach } 416a25f0a04SGreg Roach 417a25f0a04SGreg Roach /** 418a25f0a04SGreg Roach * Return the endonym for a given language - as per http://cldr.unicode.org/ 419a25f0a04SGreg Roach * 420a25f0a04SGreg Roach * @param string $locale 421a25f0a04SGreg Roach * 422a25f0a04SGreg Roach * @return string 423a25f0a04SGreg Roach */ 42455664801SGreg Roach public static function languageName(string $locale): string 425c1010edaSGreg Roach { 426c999a340SGreg Roach return Locale::create($locale)->endonym(); 427a25f0a04SGreg Roach } 428a25f0a04SGreg Roach 429a25f0a04SGreg Roach /** 430a25f0a04SGreg Roach * Return the script used by a given language 431a25f0a04SGreg Roach * 432a25f0a04SGreg Roach * @param string $locale 433a25f0a04SGreg Roach * 434a25f0a04SGreg Roach * @return string 435a25f0a04SGreg Roach */ 43655664801SGreg Roach public static function languageScript(string $locale): string 437c1010edaSGreg Roach { 438c999a340SGreg Roach return Locale::create($locale)->script()->code(); 439a25f0a04SGreg Roach } 440a25f0a04SGreg Roach 441a25f0a04SGreg Roach /** 442dfeee0a8SGreg Roach * Translate a number into the local representation. 443dfeee0a8SGreg Roach * e.g. 12345.67 becomes 444dfeee0a8SGreg Roach * en: 12,345.67 445dfeee0a8SGreg Roach * fr: 12 345,67 446dfeee0a8SGreg Roach * de: 12.345,67 447dfeee0a8SGreg Roach * 448dfeee0a8SGreg Roach * @param float $n 449cbc1590aSGreg Roach * @param int $precision 450a25f0a04SGreg Roach * 451a25f0a04SGreg Roach * @return string 452a25f0a04SGreg Roach */ 45355664801SGreg Roach public static function number(float $n, int $precision = 0): string 454c1010edaSGreg Roach { 455dfeee0a8SGreg Roach return self::$locale->number(round($n, $precision)); 456dfeee0a8SGreg Roach } 457dfeee0a8SGreg Roach 458dfeee0a8SGreg Roach /** 459dfeee0a8SGreg Roach * Translate a fraction into a percentage. 460dfeee0a8SGreg Roach * e.g. 0.123 becomes 461dfeee0a8SGreg Roach * en: 12.3% 462dfeee0a8SGreg Roach * fr: 12,3 % 463dfeee0a8SGreg Roach * de: 12,3% 464dfeee0a8SGreg Roach * 465dfeee0a8SGreg Roach * @param float $n 466cbc1590aSGreg Roach * @param int $precision 467dfeee0a8SGreg Roach * 468dfeee0a8SGreg Roach * @return string 469dfeee0a8SGreg Roach */ 47055664801SGreg Roach public static function percentage(float $n, int $precision = 0): string 471c1010edaSGreg Roach { 472dfeee0a8SGreg Roach return self::$locale->percent(round($n, $precision + 2)); 473dfeee0a8SGreg Roach } 474dfeee0a8SGreg Roach 475dfeee0a8SGreg Roach /** 476dfeee0a8SGreg Roach * Translate a plural string 477dfeee0a8SGreg Roach * echo self::plural('There is an error', 'There are errors', $num_errors); 478dfeee0a8SGreg Roach * echo self::plural('There is one error', 'There are %s errors', $num_errors); 479dfeee0a8SGreg Roach * echo self::plural('There is %1$s %2$s cat', 'There are %1$s %2$s cats', $num, $num, $colour); 480dfeee0a8SGreg Roach * 481924d091bSGreg Roach * @param string $singular 482924d091bSGreg Roach * @param string $plural 483924d091bSGreg Roach * @param int $count 484a515be7cSGreg Roach * @param string ...$args 485e93111adSRico Sonntag * 486dfeee0a8SGreg Roach * @return string 487dfeee0a8SGreg Roach */ 488924d091bSGreg Roach public static function plural(string $singular, string $plural, int $count, ...$args): string 489c1010edaSGreg Roach { 490924d091bSGreg Roach $message = self::$translator->translatePlural($singular, $plural, $count); 491dfeee0a8SGreg Roach 492924d091bSGreg Roach return sprintf($message, ...$args); 493dfeee0a8SGreg Roach } 494dfeee0a8SGreg Roach 495dfeee0a8SGreg Roach /** 496dfeee0a8SGreg Roach * UTF8 version of PHP::strrev() 497dfeee0a8SGreg Roach * Reverse RTL text for third-party libraries such as GD2 and googlechart. 498dfeee0a8SGreg Roach * These do not support UTF8 text direction, so we must mimic it for them. 499dfeee0a8SGreg Roach * Numbers are always rendered LTR, even in RTL text. 500dfeee0a8SGreg Roach * The visual direction of characters such as parentheses should be reversed. 501dfeee0a8SGreg Roach * 502dfeee0a8SGreg Roach * @param string $text Text to be reversed 503dfeee0a8SGreg Roach * 504dfeee0a8SGreg Roach * @return string 505dfeee0a8SGreg Roach */ 5068f53f488SRico Sonntag public static function reverseText($text): string 507c1010edaSGreg Roach { 508dfeee0a8SGreg Roach // Remove HTML markup - we can't display it and it is LTR. 5099524b7b5SGreg Roach $text = strip_tags($text); 5109524b7b5SGreg Roach // Remove HTML entities. 5119524b7b5SGreg Roach $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8'); 512dfeee0a8SGreg Roach 513dfeee0a8SGreg Roach // LTR text doesn't need reversing 514dfeee0a8SGreg Roach if (self::scriptDirection(self::textScript($text)) === 'ltr') { 515dfeee0a8SGreg Roach return $text; 516dfeee0a8SGreg Roach } 517dfeee0a8SGreg Roach 518dfeee0a8SGreg Roach // Mirrored characters 519991b93ddSGreg Roach $text = strtr($text, self::MIRROR_CHARACTERS); 520dfeee0a8SGreg Roach 521dfeee0a8SGreg Roach $reversed = ''; 522dfeee0a8SGreg Roach $digits = ''; 523e364afe4SGreg Roach while ($text !== '') { 524dfeee0a8SGreg Roach $letter = mb_substr($text, 0, 1); 525dfeee0a8SGreg Roach $text = mb_substr($text, 1); 526dfeee0a8SGreg Roach if (strpos(self::DIGITS, $letter) !== false) { 527dfeee0a8SGreg Roach $digits .= $letter; 528a25f0a04SGreg Roach } else { 529dfeee0a8SGreg Roach $reversed = $letter . $digits . $reversed; 530dfeee0a8SGreg Roach $digits = ''; 531dfeee0a8SGreg Roach } 532a25f0a04SGreg Roach } 533a25f0a04SGreg Roach 534dfeee0a8SGreg Roach return $digits . $reversed; 535a25f0a04SGreg Roach } 536a25f0a04SGreg Roach 537a25f0a04SGreg Roach /** 538a25f0a04SGreg Roach * Return the direction (ltr or rtl) for a given script 539a25f0a04SGreg Roach * The PHP/intl library does not provde this information, so we need 540a25f0a04SGreg Roach * our own lookup table. 541a25f0a04SGreg Roach * 542a25f0a04SGreg Roach * @param string $script 543a25f0a04SGreg Roach * 544a25f0a04SGreg Roach * @return string 545a25f0a04SGreg Roach */ 546e364afe4SGreg Roach public static function scriptDirection($script): string 547c1010edaSGreg Roach { 548a25f0a04SGreg Roach switch ($script) { 549a25f0a04SGreg Roach case 'Arab': 550a25f0a04SGreg Roach case 'Hebr': 551a25f0a04SGreg Roach case 'Mong': 552a25f0a04SGreg Roach case 'Thaa': 553a25f0a04SGreg Roach return 'rtl'; 554a25f0a04SGreg Roach default: 555a25f0a04SGreg Roach return 'ltr'; 556a25f0a04SGreg Roach } 557a25f0a04SGreg Roach } 558a25f0a04SGreg Roach 559a25f0a04SGreg Roach /** 560991b93ddSGreg Roach * Perform a case-insensitive comparison of two strings. 561a25f0a04SGreg Roach * 562a25f0a04SGreg Roach * @param string $string1 563a25f0a04SGreg Roach * @param string $string2 564a25f0a04SGreg Roach * 565cbc1590aSGreg Roach * @return int 566a25f0a04SGreg Roach */ 567e364afe4SGreg Roach public static function strcasecmp($string1, $string2): int 568c1010edaSGreg Roach { 569991b93ddSGreg Roach if (self::$collator instanceof Collator) { 570991b93ddSGreg Roach return self::$collator->compare($string1, $string2); 571a25f0a04SGreg Roach } 572e364afe4SGreg Roach 573e364afe4SGreg Roach return strcmp(self::strtolower($string1), self::strtolower($string2)); 574c9ec599fSGreg Roach } 575a25f0a04SGreg Roach 576a25f0a04SGreg Roach /** 577991b93ddSGreg Roach * Convert a string to lower case. 578a25f0a04SGreg Roach * 579dfeee0a8SGreg Roach * @param string $string 580a25f0a04SGreg Roach * 581a25f0a04SGreg Roach * @return string 582a25f0a04SGreg Roach */ 5838f53f488SRico Sonntag public static function strtolower($string): string 584c1010edaSGreg Roach { 58502086832SGreg Roach if (in_array(self::$locale->language()->code(), self::DOTLESS_I_LOCALES, true)) { 586991b93ddSGreg Roach $string = strtr($string, self::DOTLESS_I_TOLOWER); 587a25f0a04SGreg Roach } 5885ddad20bSGreg Roach 5895ddad20bSGreg Roach return mb_strtolower($string); 590a25f0a04SGreg Roach } 591a25f0a04SGreg Roach 592a25f0a04SGreg Roach /** 593991b93ddSGreg Roach * Convert a string to upper case. 594dfeee0a8SGreg Roach * 595dfeee0a8SGreg Roach * @param string $string 596a25f0a04SGreg Roach * 597a25f0a04SGreg Roach * @return string 598a25f0a04SGreg Roach */ 5998f53f488SRico Sonntag public static function strtoupper($string): string 600c1010edaSGreg Roach { 60102086832SGreg Roach if (in_array(self::$locale->language()->code(), self::DOTLESS_I_LOCALES, true)) { 602991b93ddSGreg Roach $string = strtr($string, self::DOTLESS_I_TOUPPER); 603a25f0a04SGreg Roach } 6045ddad20bSGreg Roach 6055ddad20bSGreg Roach return mb_strtoupper($string); 606a25f0a04SGreg Roach } 607a25f0a04SGreg Roach 608dfeee0a8SGreg Roach /** 609dfeee0a8SGreg Roach * Identify the script used for a piece of text 610dfeee0a8SGreg Roach * 611d0bfc631SGreg Roach * @param string $string 612dfeee0a8SGreg Roach * 613dfeee0a8SGreg Roach * @return string 614dfeee0a8SGreg Roach */ 6158f53f488SRico Sonntag public static function textScript($string): string 616c1010edaSGreg Roach { 617dfeee0a8SGreg Roach $string = strip_tags($string); // otherwise HTML tags show up as latin 618dfeee0a8SGreg Roach $string = html_entity_decode($string, ENT_QUOTES, 'UTF-8'); // otherwise HTML entities show up as latin 619c1010edaSGreg Roach $string = str_replace([ 620c1010edaSGreg Roach '@N.N.', 621c1010edaSGreg Roach '@P.N.', 622c1010edaSGreg Roach ], '', $string); // otherwise unknown names show up as latin 623dfeee0a8SGreg Roach $pos = 0; 624dfeee0a8SGreg Roach $strlen = strlen($string); 625dfeee0a8SGreg Roach while ($pos < $strlen) { 626dfeee0a8SGreg Roach // get the Unicode Code Point for the character at position $pos 627dfeee0a8SGreg Roach $byte1 = ord($string[$pos]); 628dfeee0a8SGreg Roach if ($byte1 < 0x80) { 629dfeee0a8SGreg Roach $code_point = $byte1; 630dfeee0a8SGreg Roach $chrlen = 1; 631dfeee0a8SGreg Roach } elseif ($byte1 < 0xC0) { 632dfeee0a8SGreg Roach // Invalid continuation character 633dfeee0a8SGreg Roach return 'Latn'; 634dfeee0a8SGreg Roach } elseif ($byte1 < 0xE0) { 635dfeee0a8SGreg Roach $code_point = (($byte1 & 0x1F) << 6) + (ord($string[$pos + 1]) & 0x3F); 636dfeee0a8SGreg Roach $chrlen = 2; 637dfeee0a8SGreg Roach } elseif ($byte1 < 0xF0) { 638dfeee0a8SGreg Roach $code_point = (($byte1 & 0x0F) << 12) + ((ord($string[$pos + 1]) & 0x3F) << 6) + (ord($string[$pos + 2]) & 0x3F); 639dfeee0a8SGreg Roach $chrlen = 3; 640dfeee0a8SGreg Roach } elseif ($byte1 < 0xF8) { 641dfeee0a8SGreg Roach $code_point = (($byte1 & 0x07) << 24) + ((ord($string[$pos + 1]) & 0x3F) << 12) + ((ord($string[$pos + 2]) & 0x3F) << 6) + (ord($string[$pos + 3]) & 0x3F); 642dfeee0a8SGreg Roach $chrlen = 3; 643dfeee0a8SGreg Roach } else { 644dfeee0a8SGreg Roach // Invalid UTF 645dfeee0a8SGreg Roach return 'Latn'; 646dfeee0a8SGreg Roach } 647dfeee0a8SGreg Roach 648991b93ddSGreg Roach foreach (self::SCRIPT_CHARACTER_RANGES as $range) { 649dfeee0a8SGreg Roach if ($code_point >= $range[1] && $code_point <= $range[2]) { 650dfeee0a8SGreg Roach return $range[0]; 651dfeee0a8SGreg Roach } 652dfeee0a8SGreg Roach } 653dfeee0a8SGreg Roach // Not a recognised script. Maybe punctuation, spacing, etc. Keep looking. 654dfeee0a8SGreg Roach $pos += $chrlen; 655dfeee0a8SGreg Roach } 656dfeee0a8SGreg Roach 657dfeee0a8SGreg Roach return 'Latn'; 658dfeee0a8SGreg Roach } 659dfeee0a8SGreg Roach 660dfeee0a8SGreg Roach /** 661dfeee0a8SGreg Roach * Convert a number of seconds into a relative time. For example, 630 => "10 hours, 30 minutes ago" 662dfeee0a8SGreg Roach * 663cbc1590aSGreg Roach * @param int $seconds 664dfeee0a8SGreg Roach * 665dfeee0a8SGreg Roach * @return string 666dfeee0a8SGreg Roach */ 667e364afe4SGreg Roach public static function timeAgo($seconds): string 668c1010edaSGreg Roach { 669dfeee0a8SGreg Roach $minute = 60; 670dfeee0a8SGreg Roach $hour = 60 * $minute; 671dfeee0a8SGreg Roach $day = 24 * $hour; 672dfeee0a8SGreg Roach $month = 30 * $day; 673dfeee0a8SGreg Roach $year = 365 * $day; 674dfeee0a8SGreg Roach 675dfeee0a8SGreg Roach if ($seconds > $year) { 676cdaafeeeSGreg Roach $years = intdiv($seconds, $year); 677cbc1590aSGreg Roach 678dfeee0a8SGreg Roach return self::plural('%s year ago', '%s years ago', $years, self::number($years)); 679b2ce94c6SRico Sonntag } 680b2ce94c6SRico Sonntag 681b2ce94c6SRico Sonntag if ($seconds > $month) { 682cdaafeeeSGreg Roach $months = intdiv($seconds, $month); 683cbc1590aSGreg Roach 684dfeee0a8SGreg Roach return self::plural('%s month ago', '%s months ago', $months, self::number($months)); 685b2ce94c6SRico Sonntag } 686b2ce94c6SRico Sonntag 687b2ce94c6SRico Sonntag if ($seconds > $day) { 688cdaafeeeSGreg Roach $days = intdiv($seconds, $day); 689cbc1590aSGreg Roach 690dfeee0a8SGreg Roach return self::plural('%s day ago', '%s days ago', $days, self::number($days)); 691b2ce94c6SRico Sonntag } 692b2ce94c6SRico Sonntag 693b2ce94c6SRico Sonntag if ($seconds > $hour) { 694cdaafeeeSGreg Roach $hours = intdiv($seconds, $hour); 695cbc1590aSGreg Roach 696dfeee0a8SGreg Roach return self::plural('%s hour ago', '%s hours ago', $hours, self::number($hours)); 697b2ce94c6SRico Sonntag } 698b2ce94c6SRico Sonntag 699b2ce94c6SRico Sonntag if ($seconds > $minute) { 700cdaafeeeSGreg Roach $minutes = intdiv($seconds, $minute); 701cbc1590aSGreg Roach 702dfeee0a8SGreg Roach return self::plural('%s minute ago', '%s minutes ago', $minutes, self::number($minutes)); 703dfeee0a8SGreg Roach } 704b2ce94c6SRico Sonntag 705b2ce94c6SRico Sonntag return self::plural('%s second ago', '%s seconds ago', $seconds, self::number($seconds)); 706dfeee0a8SGreg Roach } 707dfeee0a8SGreg Roach 708dfeee0a8SGreg Roach /** 709dfeee0a8SGreg Roach * What format is used to display dates in the current locale? 710dfeee0a8SGreg Roach * 711dfeee0a8SGreg Roach * @return string 712dfeee0a8SGreg Roach */ 7138f53f488SRico Sonntag public static function timeFormat(): string 714c1010edaSGreg Roach { 715bbb76c12SGreg Roach /* I18N: This is the format string for the time-of-day. See http://php.net/date for codes */ 716bbb76c12SGreg Roach return self::$translator->translate('%H:%i:%s'); 717dfeee0a8SGreg Roach } 718dfeee0a8SGreg Roach 719dfeee0a8SGreg Roach /** 720dfeee0a8SGreg Roach * Translate a string, and then substitute placeholders 721dfeee0a8SGreg Roach * echo I18N::translate('Hello World!'); 722dfeee0a8SGreg Roach * echo I18N::translate('The %s sat on the mat', 'cat'); 723dfeee0a8SGreg Roach * 724924d091bSGreg Roach * @param string $message 725a515be7cSGreg Roach * @param string ...$args 726c3283ed7SGreg Roach * 727dfeee0a8SGreg Roach * @return string 728dfeee0a8SGreg Roach */ 729924d091bSGreg Roach public static function translate(string $message, ...$args): string 730c1010edaSGreg Roach { 731924d091bSGreg Roach $message = self::$translator->translate($message); 732dfeee0a8SGreg Roach 733924d091bSGreg Roach return sprintf($message, ...$args); 734dfeee0a8SGreg Roach } 735dfeee0a8SGreg Roach 736dfeee0a8SGreg Roach /** 737dfeee0a8SGreg Roach * Context sensitive version of translate. 738a4956c0eSGreg Roach * echo I18N::translateContext('NOMINATIVE', 'January'); 739a4956c0eSGreg Roach * echo I18N::translateContext('GENITIVE', 'January'); 740dfeee0a8SGreg Roach * 741924d091bSGreg Roach * @param string $context 742924d091bSGreg Roach * @param string $message 743a515be7cSGreg Roach * @param string ...$args 744c3283ed7SGreg Roach * 745dfeee0a8SGreg Roach * @return string 746dfeee0a8SGreg Roach */ 747924d091bSGreg Roach public static function translateContext(string $context, string $message, ...$args): string 748c1010edaSGreg Roach { 749924d091bSGreg Roach $message = self::$translator->translateContext($context, $message); 750dfeee0a8SGreg Roach 751924d091bSGreg Roach return sprintf($message, ...$args); 752a25f0a04SGreg Roach } 753a25f0a04SGreg Roach} 754