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