1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 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 */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees; 17a25f0a04SGreg Roach 18991b93ddSGreg Roachuse Collator; 19f1af7e1cSGreg Roachuse Exception; 20c999a340SGreg Roachuse Fisharebest\Localization\Locale; 211e71bdc0SGreg Roachuse Fisharebest\Localization\Locale\LocaleEnUs; 2215834aaeSGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface; 233bdc890bSGreg Roachuse Fisharebest\Localization\Translation; 243bdc890bSGreg Roachuse Fisharebest\Localization\Translator; 2515d603e7SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit; 26a25f0a04SGreg Roach 27a25f0a04SGreg Roach/** 2876692c8bSGreg Roach * Internationalization (i18n) and localization (l10n). 29a25f0a04SGreg Roach */ 30c1010edaSGreg Roachclass I18N 31c1010edaSGreg Roach{ 3215834aaeSGreg Roach /** @var LocaleInterface The current locale (e.g. LocaleEnGb) */ 33c999a340SGreg Roach private static $locale; 34c999a340SGreg Roach 3576692c8bSGreg Roach /** @var Translator An object that performs translation */ 363bdc890bSGreg Roach private static $translator; 373bdc890bSGreg Roach 38991b93ddSGreg Roach /** @var Collator From the php-intl library */ 39991b93ddSGreg Roach private static $collator; 40991b93ddSGreg Roach 41a25f0a04SGreg Roach // Digits are always rendered LTR, even in RTL text. 42a25f0a04SGreg Roach const DIGITS = '0123456789٠١٢٣٤٥٦٧٨٩۰۱۲۳۴۵۶۷۸۹'; 43a25f0a04SGreg Roach 44991b93ddSGreg Roach // These locales need special handling for the dotless letter I. 45c1010edaSGreg Roach const DOTLESS_I_LOCALES = [ 46c1010edaSGreg Roach 'az', 47c1010edaSGreg Roach 'tr', 48c1010edaSGreg Roach ]; 49c1010edaSGreg Roach const DOTLESS_I_TOLOWER = [ 50c1010edaSGreg Roach 'I' => 'ı', 51c1010edaSGreg Roach 'İ' => 'i', 52c1010edaSGreg Roach ]; 53c1010edaSGreg Roach const DOTLESS_I_TOUPPER = [ 54c1010edaSGreg Roach 'ı' => 'I', 55c1010edaSGreg Roach 'i' => 'İ', 56c1010edaSGreg Roach ]; 57a25f0a04SGreg Roach 58991b93ddSGreg Roach // The ranges of characters used by each script. 59991b93ddSGreg Roach const SCRIPT_CHARACTER_RANGES = [ 60c1010edaSGreg Roach [ 61c1010edaSGreg Roach 'Latn', 62c1010edaSGreg Roach 0x0041, 63c1010edaSGreg Roach 0x005A, 64c1010edaSGreg Roach ], 65c1010edaSGreg Roach [ 66c1010edaSGreg Roach 'Latn', 67c1010edaSGreg Roach 0x0061, 68c1010edaSGreg Roach 0x007A, 69c1010edaSGreg Roach ], 70c1010edaSGreg Roach [ 71c1010edaSGreg Roach 'Latn', 72c1010edaSGreg Roach 0x0100, 73c1010edaSGreg Roach 0x02AF, 74c1010edaSGreg Roach ], 75c1010edaSGreg Roach [ 76c1010edaSGreg Roach 'Grek', 77c1010edaSGreg Roach 0x0370, 78c1010edaSGreg Roach 0x03FF, 79c1010edaSGreg Roach ], 80c1010edaSGreg Roach [ 81c1010edaSGreg Roach 'Cyrl', 82c1010edaSGreg Roach 0x0400, 83c1010edaSGreg Roach 0x052F, 84c1010edaSGreg Roach ], 85c1010edaSGreg Roach [ 86c1010edaSGreg Roach 'Hebr', 87c1010edaSGreg Roach 0x0590, 88c1010edaSGreg Roach 0x05FF, 89c1010edaSGreg Roach ], 90c1010edaSGreg Roach [ 91c1010edaSGreg Roach 'Arab', 92c1010edaSGreg Roach 0x0600, 93c1010edaSGreg Roach 0x06FF, 94c1010edaSGreg Roach ], 95c1010edaSGreg Roach [ 96c1010edaSGreg Roach 'Arab', 97c1010edaSGreg Roach 0x0750, 98c1010edaSGreg Roach 0x077F, 99c1010edaSGreg Roach ], 100c1010edaSGreg Roach [ 101c1010edaSGreg Roach 'Arab', 102c1010edaSGreg Roach 0x08A0, 103c1010edaSGreg Roach 0x08FF, 104c1010edaSGreg Roach ], 105c1010edaSGreg Roach [ 106c1010edaSGreg Roach 'Deva', 107c1010edaSGreg Roach 0x0900, 108c1010edaSGreg Roach 0x097F, 109c1010edaSGreg Roach ], 110c1010edaSGreg Roach [ 111c1010edaSGreg Roach 'Taml', 112c1010edaSGreg Roach 0x0B80, 113c1010edaSGreg Roach 0x0BFF, 114c1010edaSGreg Roach ], 115c1010edaSGreg Roach [ 116c1010edaSGreg Roach 'Sinh', 117c1010edaSGreg Roach 0x0D80, 118c1010edaSGreg Roach 0x0DFF, 119c1010edaSGreg Roach ], 120c1010edaSGreg Roach [ 121c1010edaSGreg Roach 'Thai', 122c1010edaSGreg Roach 0x0E00, 123c1010edaSGreg Roach 0x0E7F, 124c1010edaSGreg Roach ], 125c1010edaSGreg Roach [ 126c1010edaSGreg Roach 'Geor', 127c1010edaSGreg Roach 0x10A0, 128c1010edaSGreg Roach 0x10FF, 129c1010edaSGreg Roach ], 130c1010edaSGreg Roach [ 131c1010edaSGreg Roach 'Grek', 132c1010edaSGreg Roach 0x1F00, 133c1010edaSGreg Roach 0x1FFF, 134c1010edaSGreg Roach ], 135c1010edaSGreg Roach [ 136c1010edaSGreg Roach 'Deva', 137c1010edaSGreg Roach 0xA8E0, 138c1010edaSGreg Roach 0xA8FF, 139c1010edaSGreg Roach ], 140c1010edaSGreg Roach [ 141c1010edaSGreg Roach 'Hans', 142c1010edaSGreg Roach 0x3000, 143c1010edaSGreg Roach 0x303F, 144c1010edaSGreg Roach ], 145c1010edaSGreg Roach // Mixed CJK, not just Hans 146c1010edaSGreg Roach [ 147c1010edaSGreg Roach 'Hans', 148c1010edaSGreg Roach 0x3400, 149c1010edaSGreg Roach 0xFAFF, 150c1010edaSGreg Roach ], 151c1010edaSGreg Roach // Mixed CJK, not just Hans 152c1010edaSGreg Roach [ 153c1010edaSGreg Roach 'Hans', 154c1010edaSGreg Roach 0x20000, 155c1010edaSGreg Roach 0x2FA1F, 156c1010edaSGreg Roach ], 157c1010edaSGreg Roach // Mixed CJK, not just Hans 15813abd6f3SGreg Roach ]; 159a25f0a04SGreg Roach 160991b93ddSGreg Roach // Characters that are displayed in mirror form in RTL text. 161991b93ddSGreg Roach const MIRROR_CHARACTERS = [ 162a25f0a04SGreg Roach '(' => ')', 163a25f0a04SGreg Roach ')' => '(', 164a25f0a04SGreg Roach '[' => ']', 165a25f0a04SGreg Roach ']' => '[', 166a25f0a04SGreg Roach '{' => '}', 167a25f0a04SGreg Roach '}' => '{', 168a25f0a04SGreg Roach '<' => '>', 169a25f0a04SGreg Roach '>' => '<', 170a25f0a04SGreg Roach '‹ ' => '›', 171a25f0a04SGreg Roach '› ' => '‹', 172a25f0a04SGreg Roach '«' => '»', 173a25f0a04SGreg Roach '»' => '«', 174a25f0a04SGreg Roach '﴾ ' => '﴿', 175a25f0a04SGreg Roach '﴿ ' => '﴾', 176a25f0a04SGreg Roach '“ ' => '”', 177a25f0a04SGreg Roach '” ' => '“', 178a25f0a04SGreg Roach '‘ ' => '’', 179a25f0a04SGreg Roach '’ ' => '‘', 18013abd6f3SGreg Roach ]; 181a25f0a04SGreg Roach 182991b93ddSGreg Roach // Default list of locales to show in the menu. 183991b93ddSGreg Roach const DEFAULT_LOCALES = [ 184c1010edaSGreg Roach 'ar', 185c1010edaSGreg Roach 'bg', 186c1010edaSGreg Roach 'bs', 187c1010edaSGreg Roach 'ca', 188c1010edaSGreg Roach 'cs', 189c1010edaSGreg Roach 'da', 190c1010edaSGreg Roach 'de', 191c1010edaSGreg Roach 'el', 192c1010edaSGreg Roach 'en-GB', 193c1010edaSGreg Roach 'en-US', 194c1010edaSGreg Roach 'es', 195c1010edaSGreg Roach 'et', 196c1010edaSGreg Roach 'fi', 197c1010edaSGreg Roach 'fr', 198c1010edaSGreg Roach 'he', 199c1010edaSGreg Roach 'hr', 200c1010edaSGreg Roach 'hu', 201c1010edaSGreg Roach 'is', 202c1010edaSGreg Roach 'it', 203c1010edaSGreg Roach 'ka', 204c1010edaSGreg Roach 'kk', 205c1010edaSGreg Roach 'lt', 206c1010edaSGreg Roach 'mr', 207c1010edaSGreg Roach 'nb', 208c1010edaSGreg Roach 'nl', 209c1010edaSGreg Roach 'nn', 210c1010edaSGreg Roach 'pl', 211c1010edaSGreg Roach 'pt', 212c1010edaSGreg Roach 'ru', 213c1010edaSGreg Roach 'sk', 214c1010edaSGreg Roach 'sv', 215c1010edaSGreg Roach 'tr', 216c1010edaSGreg Roach 'uk', 217c1010edaSGreg Roach 'vi', 218c1010edaSGreg Roach 'zh-Hans', 219991b93ddSGreg Roach ]; 220991b93ddSGreg Roach 221a25f0a04SGreg Roach /** @var string Punctuation used to separate list items, typically a comma */ 222a25f0a04SGreg Roach public static $list_separator; 223a25f0a04SGreg Roach 224a25f0a04SGreg Roach /** 225dfeee0a8SGreg Roach * The prefered locales for this site, or a default list if no preference. 226dfeee0a8SGreg Roach * 227dfeee0a8SGreg Roach * @return LocaleInterface[] 228dfeee0a8SGreg Roach */ 2298f53f488SRico Sonntag public static function activeLocales(): array 230c1010edaSGreg Roach { 231dfeee0a8SGreg Roach $code_list = Site::getPreference('LANGUAGES'); 232dfeee0a8SGreg Roach 23315d603e7SGreg Roach if ($code_list === '') { 234991b93ddSGreg Roach $codes = self::DEFAULT_LOCALES; 235dfeee0a8SGreg Roach } else { 236991b93ddSGreg Roach $codes = explode(',', $code_list); 237dfeee0a8SGreg Roach } 238dfeee0a8SGreg Roach 23913abd6f3SGreg Roach $locales = []; 240dfeee0a8SGreg Roach foreach ($codes as $code) { 241dfeee0a8SGreg Roach if (file_exists(WT_ROOT . 'language/' . $code . '.mo')) { 242dfeee0a8SGreg Roach try { 243dfeee0a8SGreg Roach $locales[] = Locale::create($code); 244dfeee0a8SGreg Roach } catch (\Exception $ex) { 245bd52fa32SGreg Roach DebugBar::addThrowable($ex); 246bd52fa32SGreg Roach 247dfeee0a8SGreg Roach // No such locale exists? 248dfeee0a8SGreg Roach } 249dfeee0a8SGreg Roach } 250dfeee0a8SGreg Roach } 251dfeee0a8SGreg Roach usort($locales, '\Fisharebest\Localization\Locale::compare'); 252dfeee0a8SGreg Roach 253dfeee0a8SGreg Roach return $locales; 254dfeee0a8SGreg Roach } 255dfeee0a8SGreg Roach 256dfeee0a8SGreg Roach /** 257dfeee0a8SGreg Roach * Which MySQL collation should be used for this locale? 258dfeee0a8SGreg Roach * 259dfeee0a8SGreg Roach * @return string 260dfeee0a8SGreg Roach */ 261c1010edaSGreg Roach public static function collation() 262c1010edaSGreg Roach { 263dfeee0a8SGreg Roach $collation = self::$locale->collation(); 264dfeee0a8SGreg Roach switch ($collation) { 265dfeee0a8SGreg Roach case 'croatian_ci': 266dfeee0a8SGreg Roach case 'german2_ci': 267dfeee0a8SGreg Roach case 'vietnamese_ci': 268dfeee0a8SGreg Roach // Only available in MySQL 5.6 269dfeee0a8SGreg Roach return 'utf8_unicode_ci'; 270dfeee0a8SGreg Roach default: 271dfeee0a8SGreg Roach return 'utf8_' . $collation; 272dfeee0a8SGreg Roach } 273dfeee0a8SGreg Roach } 274dfeee0a8SGreg Roach 275dfeee0a8SGreg Roach /** 276dfeee0a8SGreg Roach * What format is used to display dates in the current locale? 277dfeee0a8SGreg Roach * 278dfeee0a8SGreg Roach * @return string 279dfeee0a8SGreg Roach */ 2808f53f488SRico Sonntag public static function dateFormat(): string 281c1010edaSGreg Roach { 282bbb76c12SGreg Roach /* I18N: This is the format string for full dates. See http://php.net/date for codes */ 283bbb76c12SGreg Roach return self::$translator->translate('%j %F %Y'); 284dfeee0a8SGreg Roach } 285dfeee0a8SGreg Roach 286dfeee0a8SGreg Roach /** 287dfeee0a8SGreg Roach * Generate consistent I18N for datatables.js 288dfeee0a8SGreg Roach * 28955664801SGreg Roach * @param int[] $lengths An optional array of page lengths 290dfeee0a8SGreg Roach * 291dfeee0a8SGreg Roach * @return string 292dfeee0a8SGreg Roach */ 293c1010edaSGreg Roach public static function datatablesI18N(array $lengths = [ 294c1010edaSGreg Roach 10, 295c1010edaSGreg Roach 20, 296c1010edaSGreg Roach 30, 297c1010edaSGreg Roach 50, 298c1010edaSGreg Roach 100, 299c1010edaSGreg Roach -1, 30055664801SGreg Roach ]): string 30155664801SGreg Roach { 30255664801SGreg Roach $length_options = Bootstrap4::select(FunctionsEdit::numericOptions($lengths), '10'); 303dfeee0a8SGreg Roach 304dfeee0a8SGreg Roach return 3052d9b2ebaSGreg Roach '"formatNumber": function(n) { return String(n).replace(/[0-9]/g, function(w) { return ("' . self::$locale->digits('0123456789') . '")[+w]; }); },' . 306dfeee0a8SGreg Roach '"language": {' . 307dfeee0a8SGreg Roach ' "paginate": {' . 308bbb76c12SGreg Roach ' "first": "' . self::translate('first') . '",' . 309bbb76c12SGreg Roach ' "last": "' . self::translate('last') . '",' . 310bbb76c12SGreg Roach ' "next": "' . self::translate('next') . '",' . 311bbb76c12SGreg Roach ' "previous": "' . self::translate('previous') . '"' . 312dfeee0a8SGreg Roach ' },' . 313dfeee0a8SGreg Roach ' "emptyTable": "' . self::translate('No records to display') . '",' . 314c1010edaSGreg Roach ' "info": "' . /* I18N: %s are placeholders for numbers */ 315c1010edaSGreg Roach self::translate('Showing %1$s to %2$s of %3$s', '_START_', '_END_', '_TOTAL_') . '",' . 3160b6446e1SGreg Roach ' "infoEmpty": "' . self::translate('Showing %1$s to %2$s of %3$s', self::$locale->digits('0'), self::$locale->digits('0'), self::$locale->digits('0')) . '",' . 317c1010edaSGreg Roach ' "infoFiltered": "' . /* I18N: %s is a placeholder for a number */ 318c1010edaSGreg Roach self::translate('(filtered from %s total entries)', '_MAX_') . '",' . 319c1010edaSGreg Roach ' "lengthMenu": "' . /* I18N: %s is a number of records per page */ 320c1010edaSGreg Roach self::translate('Display %s', addslashes($length_options)) . '",' . 321dfeee0a8SGreg Roach ' "loadingRecords": "' . self::translate('Loading…') . '",' . 322dfeee0a8SGreg Roach ' "processing": "' . self::translate('Loading…') . '",' . 323dfeee0a8SGreg Roach ' "search": "' . self::translate('Filter') . '",' . 324dfeee0a8SGreg Roach ' "zeroRecords": "' . self::translate('No records to display') . '"' . 3252d9b2ebaSGreg Roach '}'; 326dfeee0a8SGreg Roach } 327dfeee0a8SGreg Roach 328dfeee0a8SGreg Roach /** 329dfeee0a8SGreg Roach * Convert the digits 0-9 into the local script 330dfeee0a8SGreg Roach * 331dfeee0a8SGreg Roach * Used for years, etc., where we do not want thousands-separators, decimals, etc. 332dfeee0a8SGreg Roach * 33355664801SGreg Roach * @param string|int $n 334dfeee0a8SGreg Roach * 335dfeee0a8SGreg Roach * @return string 336dfeee0a8SGreg Roach */ 3378f53f488SRico Sonntag public static function digits($n): string 338c1010edaSGreg Roach { 33955664801SGreg Roach return self::$locale->digits((string) $n); 340dfeee0a8SGreg Roach } 341dfeee0a8SGreg Roach 342dfeee0a8SGreg Roach /** 343dfeee0a8SGreg Roach * What is the direction of the current locale 344dfeee0a8SGreg Roach * 345dfeee0a8SGreg Roach * @return string "ltr" or "rtl" 346dfeee0a8SGreg Roach */ 3478f53f488SRico Sonntag public static function direction(): string 348c1010edaSGreg Roach { 349dfeee0a8SGreg Roach return self::$locale->direction(); 350dfeee0a8SGreg Roach } 351dfeee0a8SGreg Roach 352dfeee0a8SGreg Roach /** 3537231a557SGreg Roach * What is the first day of the week. 3547231a557SGreg Roach * 355cbc1590aSGreg Roach * @return int Sunday=0, Monday=1, etc. 3567231a557SGreg Roach */ 3578f53f488SRico Sonntag public static function firstDay(): int 358c1010edaSGreg Roach { 3597231a557SGreg Roach return self::$locale->territory()->firstDay(); 3607231a557SGreg Roach } 3617231a557SGreg Roach 3627231a557SGreg Roach /** 363dfeee0a8SGreg Roach * Convert a GEDCOM age string into translated_text 364dfeee0a8SGreg Roach * 365dfeee0a8SGreg Roach * NB: The import function will have normalised this, so we don't need 366dfeee0a8SGreg Roach * to worry about badly formatted strings 3673d7a8a4cSGreg Roach * NOTE: this function is not yet complete - eventually it will replace FunctionsDate::get_age_at_event() 368dfeee0a8SGreg Roach * 369dfeee0a8SGreg Roach * @param $string 370dfeee0a8SGreg Roach * 371dfeee0a8SGreg Roach * @return string 372dfeee0a8SGreg Roach */ 37355664801SGreg Roach public static function gedcomAge(string $string): string 374c1010edaSGreg Roach { 375dfeee0a8SGreg Roach switch ($string) { 376dfeee0a8SGreg Roach case 'STILLBORN': 377dfeee0a8SGreg Roach // I18N: Description of an individual’s age at an event. For example, Died 14 Jan 1900 (stillborn) 378dfeee0a8SGreg Roach return self::translate('(stillborn)'); 379dfeee0a8SGreg Roach case 'INFANT': 380dfeee0a8SGreg Roach // I18N: Description of an individual’s age at an event. For example, Died 14 Jan 1900 (in infancy) 381dfeee0a8SGreg Roach return self::translate('(in infancy)'); 382dfeee0a8SGreg Roach case 'CHILD': 383dfeee0a8SGreg Roach // I18N: Description of an individual’s age at an event. For example, Died 14 Jan 1900 (in childhood) 384dfeee0a8SGreg Roach return self::translate('(in childhood)'); 385dfeee0a8SGreg Roach } 38613abd6f3SGreg Roach $age = []; 387dfeee0a8SGreg Roach if (preg_match('/(\d+)y/', $string, $match)) { 38855664801SGreg Roach $years = (int) $match[1]; 389dfeee0a8SGreg Roach // I18N: Part of an age string. e.g. 5 years, 4 months and 3 days 390dfeee0a8SGreg Roach $age[] = self::plural('%s year', '%s years', $years, self::number($years)); 391dfeee0a8SGreg Roach } else { 392dfeee0a8SGreg Roach $years = -1; 393dfeee0a8SGreg Roach } 394dfeee0a8SGreg Roach if (preg_match('/(\d+)m/', $string, $match)) { 39555664801SGreg Roach $months = (int) $match[1]; 396dfeee0a8SGreg Roach // I18N: Part of an age string. e.g. 5 years, 4 months and 3 days 39755664801SGreg Roach $age[] = self::plural('%s month', '%s months', $months, self::number($months)); 398dfeee0a8SGreg Roach } 399dfeee0a8SGreg Roach if (preg_match('/(\d+)w/', $string, $match)) { 40055664801SGreg Roach $weeks = (int) $match[1]; 401dfeee0a8SGreg Roach // I18N: Part of an age string. e.g. 7 weeks and 3 days 40255664801SGreg Roach $age[] = self::plural('%s week', '%s weeks', $weeks, self::number($weeks)); 403dfeee0a8SGreg Roach } 404dfeee0a8SGreg Roach if (preg_match('/(\d+)d/', $string, $match)) { 40555664801SGreg Roach $days = (int) $match[1]; 406dfeee0a8SGreg Roach // I18N: Part of an age string. e.g. 5 years, 4 months and 3 days 40755664801SGreg Roach $age[] = self::plural('%s day', '%s days', $days, self::number($days)); 408dfeee0a8SGreg Roach } 409dfeee0a8SGreg Roach // If an age is just a number of years, only show the number 410dfeee0a8SGreg Roach if (count($age) === 1 && $years >= 0) { 411dfeee0a8SGreg Roach $age = $years; 412dfeee0a8SGreg Roach } 413dfeee0a8SGreg Roach if ($age) { 414dfeee0a8SGreg Roach if (!substr_compare($string, '<', 0, 1)) { 415dfeee0a8SGreg Roach // I18N: Description of an individual’s age at an event. For example, Died 14 Jan 1900 (aged less than 21 years) 416dfeee0a8SGreg Roach return self::translate('(aged less than %s)', $age); 417*b2ce94c6SRico Sonntag } 418*b2ce94c6SRico Sonntag 419*b2ce94c6SRico Sonntag if (!substr_compare($string, '>', 0, 1)) { 420dfeee0a8SGreg Roach // I18N: Description of an individual’s age at an event. For example, Died 14 Jan 1900 (aged more than 21 years) 421dfeee0a8SGreg Roach return self::translate('(aged more than %s)', $age); 422*b2ce94c6SRico Sonntag } 423*b2ce94c6SRico Sonntag 424dfeee0a8SGreg Roach // I18N: Description of an individual’s age at an event. For example, Died 14 Jan 1900 (aged 43 years) 425dfeee0a8SGreg Roach return self::translate('(aged %s)', $age); 426dfeee0a8SGreg Roach } 427*b2ce94c6SRico Sonntag 428dfeee0a8SGreg Roach // Not a valid string? 429dfeee0a8SGreg Roach return self::translate('(aged %s)', $string); 430dfeee0a8SGreg Roach } 431dfeee0a8SGreg Roach 432dfeee0a8SGreg Roach /** 433dfeee0a8SGreg Roach * Generate i18n markup for the <html> tag, e.g. lang="ar" dir="rtl" 434dfeee0a8SGreg Roach * 435dfeee0a8SGreg Roach * @return string 436dfeee0a8SGreg Roach */ 4378f53f488SRico Sonntag public static function htmlAttributes(): string 438c1010edaSGreg Roach { 439dfeee0a8SGreg Roach return self::$locale->htmlAttributes(); 440dfeee0a8SGreg Roach } 441dfeee0a8SGreg Roach 442dfeee0a8SGreg Roach /** 443a25f0a04SGreg Roach * Initialise the translation adapter with a locale setting. 444a25f0a04SGreg Roach * 44515d603e7SGreg Roach * @param string $code Use this locale/language code, or choose one automatically 446a25f0a04SGreg Roach * 447a25f0a04SGreg Roach * @return string $string 448a25f0a04SGreg Roach */ 44955664801SGreg Roach public static function init(string $code = ''): string 450c1010edaSGreg Roach { 451c314ecc9SGreg Roach mb_internal_encoding('UTF-8'); 452c314ecc9SGreg Roach 45315d603e7SGreg Roach if ($code !== '') { 4543bdc890bSGreg Roach // Create the specified locale 4553bdc890bSGreg Roach self::$locale = Locale::create($code); 4563bdc890bSGreg Roach } else { 4573bdc890bSGreg Roach // Negotiate a locale, but if we can't then use a failsafe 45859f2f229SGreg Roach self::$locale = new LocaleEnUs(); 459cbf1be5dSGreg Roach if (Session::has('locale') && file_exists(WT_ROOT . 'language/' . Session::get('locale') . '.mo')) { 4603bdc890bSGreg Roach // Previously used 46131bc7874SGreg Roach self::$locale = Locale::create(Session::get('locale')); 4623bdc890bSGreg Roach } else { 4631e71bdc0SGreg Roach // Browser negotiation 46459f2f229SGreg Roach $default_locale = new LocaleEnUs(); 4653bdc890bSGreg Roach try { 466c7e7cb40SGreg Roach // @TODO, when no language is requested by the user (e.g. search engines), we should use 467c7e7cb40SGreg Roach // the tree's default language. However, we currently initialise languages before trees, 468c7e7cb40SGreg Roach // so there is no tree available for us to use. 4693bdc890bSGreg Roach } catch (\Exception $ex) { 470bd52fa32SGreg Roach DebugBar::addThrowable($ex); 4713bdc890bSGreg Roach } 472149573a1SGreg Roach self::$locale = Locale::httpAcceptLanguage($_SERVER, self::installedLocales(), $default_locale); 4733bdc890bSGreg Roach } 4743bdc890bSGreg Roach } 4753bdc890bSGreg Roach 476f1af7e1cSGreg Roach $cache_dir = WT_DATA_DIR . 'cache/'; 477f1af7e1cSGreg Roach $cache_file = $cache_dir . 'language-' . self::$locale->languageTag() . '-cache.php'; 4783bdc890bSGreg Roach if (file_exists($cache_file)) { 4793bdc890bSGreg Roach $filemtime = filemtime($cache_file); 4803bdc890bSGreg Roach } else { 4813bdc890bSGreg Roach $filemtime = 0; 4823bdc890bSGreg Roach } 4833bdc890bSGreg Roach 4843bdc890bSGreg Roach // Load the translation file(s) 4857d6e38dfSGreg Roach // Note that glob() returns false instead of an empty array when open_basedir_restriction 4867d6e38dfSGreg Roach // is in force and no files are found. See PHP bug #47358. 4877a7f87d7SGreg Roach if (defined('GLOB_BRACE')) { 4883bdc890bSGreg Roach $translation_files = array_merge( 48913abd6f3SGreg Roach [WT_ROOT . 'language/' . self::$locale->languageTag() . '.mo'], 49013abd6f3SGreg Roach glob(WT_MODULES_DIR . '*/language/' . self::$locale->languageTag() . '.{csv,php,mo}', GLOB_BRACE) ?: [], 49113abd6f3SGreg Roach glob(WT_DATA_DIR . 'language/' . self::$locale->languageTag() . '.{csv,php,mo}', GLOB_BRACE) ?: [] 492a25f0a04SGreg Roach ); 4937a7f87d7SGreg Roach } else { 4947a7f87d7SGreg Roach // Some servers do not have GLOB_BRACE - see http://php.net/manual/en/function.glob.php 4957a7f87d7SGreg Roach $translation_files = array_merge( 49613abd6f3SGreg Roach [WT_ROOT . 'language/' . self::$locale->languageTag() . '.mo'], 49713abd6f3SGreg Roach glob(WT_MODULES_DIR . '*/language/' . self::$locale->languageTag() . '.csv') ?: [], 49813abd6f3SGreg Roach glob(WT_MODULES_DIR . '*/language/' . self::$locale->languageTag() . '.php') ?: [], 49913abd6f3SGreg Roach glob(WT_MODULES_DIR . '*/language/' . self::$locale->languageTag() . '.mo') ?: [], 50013abd6f3SGreg Roach glob(WT_DATA_DIR . 'language/' . self::$locale->languageTag() . '.csv') ?: [], 50113abd6f3SGreg Roach glob(WT_DATA_DIR . 'language/' . self::$locale->languageTag() . '.php') ?: [], 50213abd6f3SGreg Roach glob(WT_DATA_DIR . 'language/' . self::$locale->languageTag() . '.mo') ?: [] 5037a7f87d7SGreg Roach ); 5047a7f87d7SGreg Roach } 5057a7f87d7SGreg Roach // Rebuild files after one hour 5067a7f87d7SGreg Roach $rebuild_cache = time() > $filemtime + 3600; 5071e71bdc0SGreg Roach // Rebuild files if any translation file has been updated 5083bdc890bSGreg Roach foreach ($translation_files as $translation_file) { 5093bdc890bSGreg Roach if (filemtime($translation_file) > $filemtime) { 5103bdc890bSGreg Roach $rebuild_cache = true; 511a25f0a04SGreg Roach break; 512a25f0a04SGreg Roach } 513a25f0a04SGreg Roach } 5143bdc890bSGreg Roach 5153bdc890bSGreg Roach if ($rebuild_cache) { 51613abd6f3SGreg Roach $translations = []; 5173bdc890bSGreg Roach foreach ($translation_files as $translation_file) { 5183bdc890bSGreg Roach $translation = new Translation($translation_file); 5193bdc890bSGreg Roach $translations = array_merge($translations, $translation->asArray()); 520a25f0a04SGreg Roach } 521f1af7e1cSGreg Roach try { 522f1af7e1cSGreg Roach File::mkdir($cache_dir); 523f1af7e1cSGreg Roach file_put_contents($cache_file, '<?php return ' . var_export($translations, true) . ';'); 524f1af7e1cSGreg Roach } catch (Exception $ex) { 525bd52fa32SGreg Roach DebugBar::addThrowable($ex); 526bd52fa32SGreg Roach 5277c2999b4SGreg Roach // During setup, we may not have been able to create it. 528c85fb0c4SGreg Roach } 5293bdc890bSGreg Roach } else { 5303bdc890bSGreg Roach $translations = include $cache_file; 531a25f0a04SGreg Roach } 532a25f0a04SGreg Roach 5333bdc890bSGreg Roach // Create a translator 5343bdc890bSGreg Roach self::$translator = new Translator($translations, self::$locale->pluralRule()); 535a25f0a04SGreg Roach 536bbb76c12SGreg Roach /* I18N: This punctuation is used to separate lists of items */ 537bbb76c12SGreg Roach self::$list_separator = self::translate(', '); 538a25f0a04SGreg Roach 539991b93ddSGreg Roach // Create a collator 540991b93ddSGreg Roach try { 541444a65ecSGreg Roach // PHP 5.6 cannot catch errors, so test first 542444a65ecSGreg Roach if (class_exists('Collator')) { 543991b93ddSGreg Roach self::$collator = new Collator(self::$locale->code()); 544991b93ddSGreg Roach // Ignore upper/lower case differences 545991b93ddSGreg Roach self::$collator->setStrength(Collator::SECONDARY); 546444a65ecSGreg Roach } 547991b93ddSGreg Roach } catch (Exception $ex) { 548bd52fa32SGreg Roach DebugBar::addThrowable($ex); 549bd52fa32SGreg Roach 550991b93ddSGreg Roach // PHP-INTL is not installed? We'll use a fallback later. 551991b93ddSGreg Roach } 552991b93ddSGreg Roach 5535331c5eaSGreg Roach return self::$locale->languageTag(); 554a25f0a04SGreg Roach } 555a25f0a04SGreg Roach 556a25f0a04SGreg Roach /** 557c999a340SGreg Roach * All locales for which a translation file exists. 558c999a340SGreg Roach * 55915834aaeSGreg Roach * @return LocaleInterface[] 560c999a340SGreg Roach */ 5618f53f488SRico Sonntag public static function installedLocales(): array 562c1010edaSGreg Roach { 56313abd6f3SGreg Roach $locales = []; 564c999a340SGreg Roach foreach (glob(WT_ROOT . 'language/*.mo') as $file) { 565c999a340SGreg Roach try { 566c999a340SGreg Roach $locales[] = Locale::create(basename($file, '.mo')); 567c999a340SGreg Roach } catch (\Exception $ex) { 568bd52fa32SGreg Roach DebugBar::addThrowable($ex); 569bd52fa32SGreg Roach 5703bdc890bSGreg Roach // Not a recognised locale 571a25f0a04SGreg Roach } 572a25f0a04SGreg Roach } 573c999a340SGreg Roach usort($locales, '\Fisharebest\Localization\Locale::compare'); 574c999a340SGreg Roach 575c999a340SGreg Roach return $locales; 576a25f0a04SGreg Roach } 577a25f0a04SGreg Roach 578a25f0a04SGreg Roach /** 579a25f0a04SGreg Roach * Return the endonym for a given language - as per http://cldr.unicode.org/ 580a25f0a04SGreg Roach * 581a25f0a04SGreg Roach * @param string $locale 582a25f0a04SGreg Roach * 583a25f0a04SGreg Roach * @return string 584a25f0a04SGreg Roach */ 58555664801SGreg Roach public static function languageName(string $locale): string 586c1010edaSGreg Roach { 587c999a340SGreg Roach return Locale::create($locale)->endonym(); 588a25f0a04SGreg Roach } 589a25f0a04SGreg Roach 590a25f0a04SGreg Roach /** 591a25f0a04SGreg Roach * Return the script used by a given language 592a25f0a04SGreg Roach * 593a25f0a04SGreg Roach * @param string $locale 594a25f0a04SGreg Roach * 595a25f0a04SGreg Roach * @return string 596a25f0a04SGreg Roach */ 59755664801SGreg Roach public static function languageScript(string $locale): string 598c1010edaSGreg Roach { 599c999a340SGreg Roach return Locale::create($locale)->script()->code(); 600a25f0a04SGreg Roach } 601a25f0a04SGreg Roach 602a25f0a04SGreg Roach /** 603dfeee0a8SGreg Roach * Translate a number into the local representation. 604a25f0a04SGreg Roach * 605dfeee0a8SGreg Roach * e.g. 12345.67 becomes 606dfeee0a8SGreg Roach * en: 12,345.67 607dfeee0a8SGreg Roach * fr: 12 345,67 608dfeee0a8SGreg Roach * de: 12.345,67 609dfeee0a8SGreg Roach * 610dfeee0a8SGreg Roach * @param float $n 611cbc1590aSGreg Roach * @param int $precision 612a25f0a04SGreg Roach * 613a25f0a04SGreg Roach * @return string 614a25f0a04SGreg Roach */ 61555664801SGreg Roach public static function number(float $n, int $precision = 0): string 616c1010edaSGreg Roach { 617dfeee0a8SGreg Roach return self::$locale->number(round($n, $precision)); 618dfeee0a8SGreg Roach } 619dfeee0a8SGreg Roach 620dfeee0a8SGreg Roach /** 621dfeee0a8SGreg Roach * Translate a fraction into a percentage. 622dfeee0a8SGreg Roach * 623dfeee0a8SGreg Roach * e.g. 0.123 becomes 624dfeee0a8SGreg Roach * en: 12.3% 625dfeee0a8SGreg Roach * fr: 12,3 % 626dfeee0a8SGreg Roach * de: 12,3% 627dfeee0a8SGreg Roach * 628dfeee0a8SGreg Roach * @param float $n 629cbc1590aSGreg Roach * @param int $precision 630dfeee0a8SGreg Roach * 631dfeee0a8SGreg Roach * @return string 632dfeee0a8SGreg Roach */ 63355664801SGreg Roach public static function percentage(float $n, int $precision = 0): string 634c1010edaSGreg Roach { 635dfeee0a8SGreg Roach return self::$locale->percent(round($n, $precision + 2)); 636dfeee0a8SGreg Roach } 637dfeee0a8SGreg Roach 638dfeee0a8SGreg Roach /** 639dfeee0a8SGreg Roach * Translate a plural string 640dfeee0a8SGreg Roach * echo self::plural('There is an error', 'There are errors', $num_errors); 641dfeee0a8SGreg Roach * echo self::plural('There is one error', 'There are %s errors', $num_errors); 642dfeee0a8SGreg Roach * echo self::plural('There is %1$s %2$s cat', 'There are %1$s %2$s cats', $num, $num, $colour); 643dfeee0a8SGreg Roach * 644e93111adSRico Sonntag * @param array $args 645e93111adSRico Sonntag * 646dfeee0a8SGreg Roach * @return string 647dfeee0a8SGreg Roach */ 6488f53f488SRico Sonntag public static function plural(...$args): string 649c1010edaSGreg Roach { 650beb72eacSGreg Roach $args[0] = self::$translator->translatePlural($args[0], $args[1], (int) $args[2]); 651dfeee0a8SGreg Roach unset($args[1], $args[2]); 652dfeee0a8SGreg Roach 653c58d56c4SGreg Roach return sprintf(...$args); 654dfeee0a8SGreg Roach } 655dfeee0a8SGreg Roach 656dfeee0a8SGreg Roach /** 657dfeee0a8SGreg Roach * UTF8 version of PHP::strrev() 658dfeee0a8SGreg Roach * 659dfeee0a8SGreg Roach * Reverse RTL text for third-party libraries such as GD2 and googlechart. 660dfeee0a8SGreg Roach * 661dfeee0a8SGreg Roach * These do not support UTF8 text direction, so we must mimic it for them. 662dfeee0a8SGreg Roach * 663dfeee0a8SGreg Roach * Numbers are always rendered LTR, even in RTL text. 664dfeee0a8SGreg Roach * The visual direction of characters such as parentheses should be reversed. 665dfeee0a8SGreg Roach * 666dfeee0a8SGreg Roach * @param string $text Text to be reversed 667dfeee0a8SGreg Roach * 668dfeee0a8SGreg Roach * @return string 669dfeee0a8SGreg Roach */ 6708f53f488SRico Sonntag public static function reverseText($text): string 671c1010edaSGreg Roach { 672dfeee0a8SGreg Roach // Remove HTML markup - we can't display it and it is LTR. 6739524b7b5SGreg Roach $text = strip_tags($text); 6749524b7b5SGreg Roach // Remove HTML entities. 6759524b7b5SGreg Roach $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8'); 676dfeee0a8SGreg Roach 677dfeee0a8SGreg Roach // LTR text doesn't need reversing 678dfeee0a8SGreg Roach if (self::scriptDirection(self::textScript($text)) === 'ltr') { 679dfeee0a8SGreg Roach return $text; 680dfeee0a8SGreg Roach } 681dfeee0a8SGreg Roach 682dfeee0a8SGreg Roach // Mirrored characters 683991b93ddSGreg Roach $text = strtr($text, self::MIRROR_CHARACTERS); 684dfeee0a8SGreg Roach 685dfeee0a8SGreg Roach $reversed = ''; 686dfeee0a8SGreg Roach $digits = ''; 687dfeee0a8SGreg Roach while ($text != '') { 688dfeee0a8SGreg Roach $letter = mb_substr($text, 0, 1); 689dfeee0a8SGreg Roach $text = mb_substr($text, 1); 690dfeee0a8SGreg Roach if (strpos(self::DIGITS, $letter) !== false) { 691dfeee0a8SGreg Roach $digits .= $letter; 692a25f0a04SGreg Roach } else { 693dfeee0a8SGreg Roach $reversed = $letter . $digits . $reversed; 694dfeee0a8SGreg Roach $digits = ''; 695dfeee0a8SGreg Roach } 696a25f0a04SGreg Roach } 697a25f0a04SGreg Roach 698dfeee0a8SGreg Roach return $digits . $reversed; 699a25f0a04SGreg Roach } 700a25f0a04SGreg Roach 701a25f0a04SGreg Roach /** 702a25f0a04SGreg Roach * Return the direction (ltr or rtl) for a given script 703a25f0a04SGreg Roach * 704a25f0a04SGreg Roach * The PHP/intl library does not provde this information, so we need 705a25f0a04SGreg Roach * our own lookup table. 706a25f0a04SGreg Roach * 707a25f0a04SGreg Roach * @param string $script 708a25f0a04SGreg Roach * 709a25f0a04SGreg Roach * @return string 710a25f0a04SGreg Roach */ 711c1010edaSGreg Roach public static function scriptDirection($script) 712c1010edaSGreg Roach { 713a25f0a04SGreg Roach switch ($script) { 714a25f0a04SGreg Roach case 'Arab': 715a25f0a04SGreg Roach case 'Hebr': 716a25f0a04SGreg Roach case 'Mong': 717a25f0a04SGreg Roach case 'Thaa': 718a25f0a04SGreg Roach return 'rtl'; 719a25f0a04SGreg Roach default: 720a25f0a04SGreg Roach return 'ltr'; 721a25f0a04SGreg Roach } 722a25f0a04SGreg Roach } 723a25f0a04SGreg Roach 724a25f0a04SGreg Roach /** 725991b93ddSGreg Roach * Perform a case-insensitive comparison of two strings. 726a25f0a04SGreg Roach * 727a25f0a04SGreg Roach * @param string $string1 728a25f0a04SGreg Roach * @param string $string2 729a25f0a04SGreg Roach * 730cbc1590aSGreg Roach * @return int 731a25f0a04SGreg Roach */ 732c1010edaSGreg Roach public static function strcasecmp($string1, $string2) 733c1010edaSGreg Roach { 734991b93ddSGreg Roach if (self::$collator instanceof Collator) { 735991b93ddSGreg Roach return self::$collator->compare($string1, $string2); 736a25f0a04SGreg Roach } 737*b2ce94c6SRico Sonntag 738*b2ce94c6SRico Sonntag return strcmp(self::strtolower($string1), self::strtolower($string2)); 739a25f0a04SGreg Roach } 740a25f0a04SGreg Roach 741a25f0a04SGreg Roach /** 742991b93ddSGreg Roach * Convert a string to lower case. 743a25f0a04SGreg Roach * 744dfeee0a8SGreg Roach * @param string $string 745a25f0a04SGreg Roach * 746a25f0a04SGreg Roach * @return string 747a25f0a04SGreg Roach */ 7488f53f488SRico Sonntag public static function strtolower($string): string 749c1010edaSGreg Roach { 750991b93ddSGreg Roach if (in_array(self::$locale->language()->code(), self::DOTLESS_I_LOCALES)) { 751991b93ddSGreg Roach $string = strtr($string, self::DOTLESS_I_TOLOWER); 752a25f0a04SGreg Roach } 7535ddad20bSGreg Roach 7545ddad20bSGreg Roach return mb_strtolower($string); 755a25f0a04SGreg Roach } 756a25f0a04SGreg Roach 757a25f0a04SGreg Roach /** 758991b93ddSGreg Roach * Convert a string to upper case. 759dfeee0a8SGreg Roach * 760dfeee0a8SGreg Roach * @param string $string 761a25f0a04SGreg Roach * 762a25f0a04SGreg Roach * @return string 763a25f0a04SGreg Roach */ 7648f53f488SRico Sonntag public static function strtoupper($string): string 765c1010edaSGreg Roach { 766991b93ddSGreg Roach if (in_array(self::$locale->language()->code(), self::DOTLESS_I_LOCALES)) { 767991b93ddSGreg Roach $string = strtr($string, self::DOTLESS_I_TOUPPER); 768a25f0a04SGreg Roach } 7695ddad20bSGreg Roach 7705ddad20bSGreg Roach return mb_strtoupper($string); 771a25f0a04SGreg Roach } 772a25f0a04SGreg Roach 773dfeee0a8SGreg Roach /** 774dfeee0a8SGreg Roach * Identify the script used for a piece of text 775dfeee0a8SGreg Roach * 776dfeee0a8SGreg Roach * @param $string 777dfeee0a8SGreg Roach * 778dfeee0a8SGreg Roach * @return string 779dfeee0a8SGreg Roach */ 7808f53f488SRico Sonntag public static function textScript($string): string 781c1010edaSGreg Roach { 782dfeee0a8SGreg Roach $string = strip_tags($string); // otherwise HTML tags show up as latin 783dfeee0a8SGreg Roach $string = html_entity_decode($string, ENT_QUOTES, 'UTF-8'); // otherwise HTML entities show up as latin 784c1010edaSGreg Roach $string = str_replace([ 785c1010edaSGreg Roach '@N.N.', 786c1010edaSGreg Roach '@P.N.', 787c1010edaSGreg Roach ], '', $string); // otherwise unknown names show up as latin 788dfeee0a8SGreg Roach $pos = 0; 789dfeee0a8SGreg Roach $strlen = strlen($string); 790dfeee0a8SGreg Roach while ($pos < $strlen) { 791dfeee0a8SGreg Roach // get the Unicode Code Point for the character at position $pos 792dfeee0a8SGreg Roach $byte1 = ord($string[$pos]); 793dfeee0a8SGreg Roach if ($byte1 < 0x80) { 794dfeee0a8SGreg Roach $code_point = $byte1; 795dfeee0a8SGreg Roach $chrlen = 1; 796dfeee0a8SGreg Roach } elseif ($byte1 < 0xC0) { 797dfeee0a8SGreg Roach // Invalid continuation character 798dfeee0a8SGreg Roach return 'Latn'; 799dfeee0a8SGreg Roach } elseif ($byte1 < 0xE0) { 800dfeee0a8SGreg Roach $code_point = (($byte1 & 0x1F) << 6) + (ord($string[$pos + 1]) & 0x3F); 801dfeee0a8SGreg Roach $chrlen = 2; 802dfeee0a8SGreg Roach } elseif ($byte1 < 0xF0) { 803dfeee0a8SGreg Roach $code_point = (($byte1 & 0x0F) << 12) + ((ord($string[$pos + 1]) & 0x3F) << 6) + (ord($string[$pos + 2]) & 0x3F); 804dfeee0a8SGreg Roach $chrlen = 3; 805dfeee0a8SGreg Roach } elseif ($byte1 < 0xF8) { 806dfeee0a8SGreg Roach $code_point = (($byte1 & 0x07) << 24) + ((ord($string[$pos + 1]) & 0x3F) << 12) + ((ord($string[$pos + 2]) & 0x3F) << 6) + (ord($string[$pos + 3]) & 0x3F); 807dfeee0a8SGreg Roach $chrlen = 3; 808dfeee0a8SGreg Roach } else { 809dfeee0a8SGreg Roach // Invalid UTF 810dfeee0a8SGreg Roach return 'Latn'; 811dfeee0a8SGreg Roach } 812dfeee0a8SGreg Roach 813991b93ddSGreg Roach foreach (self::SCRIPT_CHARACTER_RANGES as $range) { 814dfeee0a8SGreg Roach if ($code_point >= $range[1] && $code_point <= $range[2]) { 815dfeee0a8SGreg Roach return $range[0]; 816dfeee0a8SGreg Roach } 817dfeee0a8SGreg Roach } 818dfeee0a8SGreg Roach // Not a recognised script. Maybe punctuation, spacing, etc. Keep looking. 819dfeee0a8SGreg Roach $pos += $chrlen; 820dfeee0a8SGreg Roach } 821dfeee0a8SGreg Roach 822dfeee0a8SGreg Roach return 'Latn'; 823dfeee0a8SGreg Roach } 824dfeee0a8SGreg Roach 825dfeee0a8SGreg Roach /** 826dfeee0a8SGreg Roach * Convert a number of seconds into a relative time. For example, 630 => "10 hours, 30 minutes ago" 827dfeee0a8SGreg Roach * 828cbc1590aSGreg Roach * @param int $seconds 829dfeee0a8SGreg Roach * 830dfeee0a8SGreg Roach * @return string 831dfeee0a8SGreg Roach */ 832c1010edaSGreg Roach public static function timeAgo($seconds) 833c1010edaSGreg Roach { 834dfeee0a8SGreg Roach $minute = 60; 835dfeee0a8SGreg Roach $hour = 60 * $minute; 836dfeee0a8SGreg Roach $day = 24 * $hour; 837dfeee0a8SGreg Roach $month = 30 * $day; 838dfeee0a8SGreg Roach $year = 365 * $day; 839dfeee0a8SGreg Roach 840dfeee0a8SGreg Roach if ($seconds > $year) { 841dfeee0a8SGreg Roach $years = (int)($seconds / $year); 842cbc1590aSGreg Roach 843dfeee0a8SGreg Roach return self::plural('%s year ago', '%s years ago', $years, self::number($years)); 844*b2ce94c6SRico Sonntag } 845*b2ce94c6SRico Sonntag 846*b2ce94c6SRico Sonntag if ($seconds > $month) { 847dfeee0a8SGreg Roach $months = (int)($seconds / $month); 848cbc1590aSGreg Roach 849dfeee0a8SGreg Roach return self::plural('%s month ago', '%s months ago', $months, self::number($months)); 850*b2ce94c6SRico Sonntag } 851*b2ce94c6SRico Sonntag 852*b2ce94c6SRico Sonntag if ($seconds > $day) { 853dfeee0a8SGreg Roach $days = (int)($seconds / $day); 854cbc1590aSGreg Roach 855dfeee0a8SGreg Roach return self::plural('%s day ago', '%s days ago', $days, self::number($days)); 856*b2ce94c6SRico Sonntag } 857*b2ce94c6SRico Sonntag 858*b2ce94c6SRico Sonntag if ($seconds > $hour) { 859dfeee0a8SGreg Roach $hours = (int)($seconds / $hour); 860cbc1590aSGreg Roach 861dfeee0a8SGreg Roach return self::plural('%s hour ago', '%s hours ago', $hours, self::number($hours)); 862*b2ce94c6SRico Sonntag } 863*b2ce94c6SRico Sonntag 864*b2ce94c6SRico Sonntag if ($seconds > $minute) { 865dfeee0a8SGreg Roach $minutes = (int)($seconds / $minute); 866cbc1590aSGreg Roach 867dfeee0a8SGreg Roach return self::plural('%s minute ago', '%s minutes ago', $minutes, self::number($minutes)); 868dfeee0a8SGreg Roach } 869*b2ce94c6SRico Sonntag 870*b2ce94c6SRico Sonntag return self::plural('%s second ago', '%s seconds ago', $seconds, self::number($seconds)); 871dfeee0a8SGreg Roach } 872dfeee0a8SGreg Roach 873dfeee0a8SGreg Roach /** 874dfeee0a8SGreg Roach * What format is used to display dates in the current locale? 875dfeee0a8SGreg Roach * 876dfeee0a8SGreg Roach * @return string 877dfeee0a8SGreg Roach */ 8788f53f488SRico Sonntag public static function timeFormat(): string 879c1010edaSGreg Roach { 880bbb76c12SGreg Roach /* I18N: This is the format string for the time-of-day. See http://php.net/date for codes */ 881bbb76c12SGreg Roach return self::$translator->translate('%H:%i:%s'); 882dfeee0a8SGreg Roach } 883dfeee0a8SGreg Roach 884dfeee0a8SGreg Roach /** 885dfeee0a8SGreg Roach * Translate a string, and then substitute placeholders 886dfeee0a8SGreg Roach * 887dfeee0a8SGreg Roach * echo I18N::translate('Hello World!'); 888dfeee0a8SGreg Roach * echo I18N::translate('The %s sat on the mat', 'cat'); 889dfeee0a8SGreg Roach * 890e93111adSRico Sonntag * @param array $args 891c3283ed7SGreg Roach * 892dfeee0a8SGreg Roach * @return string 893dfeee0a8SGreg Roach */ 8948f53f488SRico Sonntag public static function translate(...$args): string 895c1010edaSGreg Roach { 896dfeee0a8SGreg Roach $args[0] = self::$translator->translate($args[0]); 897dfeee0a8SGreg Roach 898c58d56c4SGreg Roach return sprintf(...$args); 899dfeee0a8SGreg Roach } 900dfeee0a8SGreg Roach 901dfeee0a8SGreg Roach /** 902dfeee0a8SGreg Roach * Context sensitive version of translate. 903a4956c0eSGreg Roach * echo I18N::translateContext('NOMINATIVE', 'January'); 904a4956c0eSGreg Roach * echo I18N::translateContext('GENITIVE', 'January'); 905dfeee0a8SGreg Roach * 906e93111adSRico Sonntag * @param array $args 907c3283ed7SGreg Roach * 908dfeee0a8SGreg Roach * @return string 909dfeee0a8SGreg Roach */ 9108f53f488SRico Sonntag public static function translateContext(...$args): string 911c1010edaSGreg Roach { 912c58d56c4SGreg Roach $args[1] = self::$translator->translateContext($args[0], $args[1]); 913c58d56c4SGreg Roach unset($args[0]); 914dfeee0a8SGreg Roach 915c58d56c4SGreg Roach return sprintf(...$args); 916a25f0a04SGreg Roach } 917a25f0a04SGreg Roach} 918