xref: /webtrees/app/I18N.php (revision 2d9b2eba47b5b83af704323613250883557db46d)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
4369c0ce6SGreg Roach * Copyright (C) 2016 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;
2027a79457SGreg Roachuse Fisharebest\ExtCalendar\ArabicCalendar;
2127a79457SGreg Roachuse Fisharebest\ExtCalendar\CalendarInterface;
2227a79457SGreg Roachuse Fisharebest\ExtCalendar\GregorianCalendar;
2327a79457SGreg Roachuse Fisharebest\ExtCalendar\JewishCalendar;
2427a79457SGreg Roachuse Fisharebest\ExtCalendar\PersianCalendar;
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;
30a25f0a04SGreg Roach
31a25f0a04SGreg Roach/**
3276692c8bSGreg Roach * Internationalization (i18n) and localization (l10n).
33a25f0a04SGreg Roach */
34a25f0a04SGreg Roachclass I18N {
3515834aaeSGreg Roach	/** @var LocaleInterface The current locale (e.g. LocaleEnGb) */
36c999a340SGreg Roach	private static $locale;
37c999a340SGreg Roach
3876692c8bSGreg Roach	/** @var Translator An object that performs translation*/
393bdc890bSGreg Roach	private static $translator;
403bdc890bSGreg Roach
41991b93ddSGreg Roach	/** @var  Collator From the php-intl library */
42991b93ddSGreg Roach	private static $collator;
43991b93ddSGreg Roach
44a25f0a04SGreg Roach	// Digits are always rendered LTR, even in RTL text.
45a25f0a04SGreg Roach	const DIGITS = '0123456789٠١٢٣٤٥٦٧٨٩۰۱۲۳۴۵۶۷۸۹';
46a25f0a04SGreg Roach
47991b93ddSGreg Roach	// These locales need special handling for the dotless letter I.
48991b93ddSGreg Roach	const DOTLESS_I_LOCALES = ['az', 'tr'];
49991b93ddSGreg Roach	const DOTLESS_I_TOLOWER = ['I' => 'ı', 'İ' => 'i'];
50991b93ddSGreg Roach	const DOTLESS_I_TOUPPER = ['ı' => 'I', 'i' => 'İ'];
51a25f0a04SGreg Roach
52991b93ddSGreg Roach	// The ranges of characters used by each script.
53991b93ddSGreg Roach	const SCRIPT_CHARACTER_RANGES = [
54991b93ddSGreg Roach		['Latn', 0x0041, 0x005A],
55991b93ddSGreg Roach		['Latn', 0x0061, 0x007A],
5613abd6f3SGreg Roach		['Latn', 0x0100, 0x02AF],
5713abd6f3SGreg Roach		['Grek', 0x0370, 0x03FF],
5813abd6f3SGreg Roach		['Cyrl', 0x0400, 0x052F],
5913abd6f3SGreg Roach		['Hebr', 0x0590, 0x05FF],
6013abd6f3SGreg Roach		['Arab', 0x0600, 0x06FF],
6113abd6f3SGreg Roach		['Arab', 0x0750, 0x077F],
6213abd6f3SGreg Roach		['Arab', 0x08A0, 0x08FF],
6313abd6f3SGreg Roach		['Deva', 0x0900, 0x097F],
6413abd6f3SGreg Roach		['Taml', 0x0B80, 0x0BFF],
6513abd6f3SGreg Roach		['Sinh', 0x0D80, 0x0DFF],
6613abd6f3SGreg Roach		['Thai', 0x0E00, 0x0E7F],
6713abd6f3SGreg Roach		['Geor', 0x10A0, 0x10FF],
6813abd6f3SGreg Roach		['Grek', 0x1F00, 0x1FFF],
6913abd6f3SGreg Roach		['Deva', 0xA8E0, 0xA8FF],
7013abd6f3SGreg Roach		['Hans', 0x3000, 0x303F], // Mixed CJK, not just Hans
7113abd6f3SGreg Roach		['Hans', 0x3400, 0xFAFF], // Mixed CJK, not just Hans
7213abd6f3SGreg Roach		['Hans', 0x20000, 0x2FA1F], // Mixed CJK, not just Hans
7313abd6f3SGreg Roach	];
74a25f0a04SGreg Roach
75991b93ddSGreg Roach	// Characters that are displayed in mirror form in RTL text.
76991b93ddSGreg Roach	const MIRROR_CHARACTERS = [
77a25f0a04SGreg Roach		'(' => ')',
78a25f0a04SGreg Roach		')' => '(',
79a25f0a04SGreg Roach		'[' => ']',
80a25f0a04SGreg Roach		']' => '[',
81a25f0a04SGreg Roach		'{' => '}',
82a25f0a04SGreg Roach		'}' => '{',
83a25f0a04SGreg Roach		'<' => '>',
84a25f0a04SGreg Roach		'>' => '<',
85a25f0a04SGreg Roach		'‹' => '›',
86a25f0a04SGreg Roach		'›' => '‹',
87a25f0a04SGreg Roach		'«' => '»',
88a25f0a04SGreg Roach		'»' => '«',
89a25f0a04SGreg Roach		'﴾' => '﴿',
90a25f0a04SGreg Roach		'﴿' => '﴾',
91a25f0a04SGreg Roach		'“' => '”',
92a25f0a04SGreg Roach		'”' => '“',
93a25f0a04SGreg Roach		'‘' => '’',
94a25f0a04SGreg Roach		'’' => '‘',
9513abd6f3SGreg Roach	];
96a25f0a04SGreg Roach
97991b93ddSGreg Roach	// Default list of locales to show in the menu.
98991b93ddSGreg Roach	const DEFAULT_LOCALES = [
99991b93ddSGreg Roach		'ar', 'bg', 'bs', 'ca', 'cs', 'da', 'de', 'el', 'en-GB', 'en-US', 'es',
100991b93ddSGreg Roach		'et', 'fi', 'fr', 'he', 'hr', 'hu', 'is', 'it', 'ka', 'lt', 'mr', 'nb',
101991b93ddSGreg Roach		'nl', 'nn', 'pl', 'pt', 'ru', 'sk', 'sv', 'tr', 'uk', 'vi', 'zh-Hans',
102991b93ddSGreg Roach	];
103991b93ddSGreg Roach
104a25f0a04SGreg Roach	/** @var string Punctuation used to separate list items, typically a comma */
105a25f0a04SGreg Roach	public static $list_separator;
106a25f0a04SGreg Roach
107a25f0a04SGreg Roach	/**
108dfeee0a8SGreg Roach	 * The prefered locales for this site, or a default list if no preference.
109dfeee0a8SGreg Roach	 *
110dfeee0a8SGreg Roach	 * @return LocaleInterface[]
111dfeee0a8SGreg Roach	 */
112dfeee0a8SGreg Roach	public static function activeLocales() {
113dfeee0a8SGreg Roach		$code_list = Site::getPreference('LANGUAGES');
114dfeee0a8SGreg Roach
115991b93ddSGreg Roach		if (empty($code_list)) {
116991b93ddSGreg Roach			$codes = self::DEFAULT_LOCALES;
117dfeee0a8SGreg Roach		} else {
118991b93ddSGreg Roach			$codes = explode(',', $code_list);
119dfeee0a8SGreg Roach		}
120dfeee0a8SGreg Roach
12113abd6f3SGreg Roach		$locales = [];
122dfeee0a8SGreg Roach		foreach ($codes as $code) {
123dfeee0a8SGreg Roach			if (file_exists(WT_ROOT . 'language/' . $code . '.mo')) {
124dfeee0a8SGreg Roach				try {
125dfeee0a8SGreg Roach					$locales[] = Locale::create($code);
126dfeee0a8SGreg Roach				} catch (\Exception $ex) {
127dfeee0a8SGreg Roach					// No such locale exists?
128dfeee0a8SGreg Roach				}
129dfeee0a8SGreg Roach			}
130dfeee0a8SGreg Roach		}
131dfeee0a8SGreg Roach		usort($locales, '\Fisharebest\Localization\Locale::compare');
132dfeee0a8SGreg Roach
133dfeee0a8SGreg Roach		return $locales;
134dfeee0a8SGreg Roach	}
135dfeee0a8SGreg Roach
136dfeee0a8SGreg Roach	/**
137dfeee0a8SGreg Roach	 * Which MySQL collation should be used for this locale?
138dfeee0a8SGreg Roach	 *
139dfeee0a8SGreg Roach	 * @return string
140dfeee0a8SGreg Roach	 */
141dfeee0a8SGreg Roach	public static function collation() {
142dfeee0a8SGreg Roach		$collation = self::$locale->collation();
143dfeee0a8SGreg Roach		switch ($collation) {
144dfeee0a8SGreg Roach		case 'croatian_ci':
145dfeee0a8SGreg Roach		case 'german2_ci':
146dfeee0a8SGreg Roach		case 'vietnamese_ci':
147dfeee0a8SGreg Roach			// Only available in MySQL 5.6
148dfeee0a8SGreg Roach			return 'utf8_unicode_ci';
149dfeee0a8SGreg Roach		default:
150dfeee0a8SGreg Roach			return 'utf8_' . $collation;
151dfeee0a8SGreg Roach		}
152dfeee0a8SGreg Roach	}
153dfeee0a8SGreg Roach
154dfeee0a8SGreg Roach	/**
155dfeee0a8SGreg Roach	 * What format is used to display dates in the current locale?
156dfeee0a8SGreg Roach	 *
157dfeee0a8SGreg Roach	 * @return string
158dfeee0a8SGreg Roach	 */
159dfeee0a8SGreg Roach	public static function dateFormat() {
160dfeee0a8SGreg Roach		return /* I18N: This is the format string for full dates. See http://php.net/date for codes */ self::$translator->translate('%j %F %Y');
161dfeee0a8SGreg Roach	}
162dfeee0a8SGreg Roach
163dfeee0a8SGreg Roach	/**
164dfeee0a8SGreg Roach	 * Generate consistent I18N for datatables.js
165dfeee0a8SGreg Roach	 *
166dfeee0a8SGreg Roach	 * @param array|null $lengths An optional array of page lengths
167dfeee0a8SGreg Roach	 *
168dfeee0a8SGreg Roach	 * @return string
169dfeee0a8SGreg Roach	 */
170dfeee0a8SGreg Roach	public static function datatablesI18N(array $lengths = null) {
171dfeee0a8SGreg Roach		if ($lengths === null) {
17213abd6f3SGreg Roach			$lengths = [10, 20, 30, 50, 100, -1];
173dfeee0a8SGreg Roach		}
174dfeee0a8SGreg Roach
175dfeee0a8SGreg Roach		$length_menu = '';
176dfeee0a8SGreg Roach		foreach ($lengths as $length) {
177dfeee0a8SGreg Roach			$length_menu .=
178dfeee0a8SGreg Roach				'<option value="' . $length . '">' .
179dfeee0a8SGreg Roach				($length === -1 ? /* I18N: listbox option, e.g. “10,25,50,100,all” */ self::translate('All') : self::number($length)) .
180dfeee0a8SGreg Roach				'</option>';
181dfeee0a8SGreg Roach		}
182dfeee0a8SGreg Roach		$length_menu = '<select>' . $length_menu . '</select>';
183dfeee0a8SGreg Roach		$length_menu = /* I18N: Display %s [records per page], %s is a placeholder for listbox containing numeric options */ self::translate('Display %s', $length_menu);
184dfeee0a8SGreg Roach
185dfeee0a8SGreg Roach		return
186*2d9b2ebaSGreg Roach			'"infoCallback": function(oSettings, iStart, iEnd, iMax, iTotal, sPre) { return sPre.replace(/[0-9]/g, function(w) { return ("' . self::$locale->digits('0123456789') . '")[+w]; }); },' .
187*2d9b2ebaSGreg Roach			'"formatNumber": function(n) { return String(n).replace(/[0-9]/g, function(w) { return ("' . self::$locale->digits('0123456789') . '")[+w]; }); },' .
188dfeee0a8SGreg Roach			'"language": {' .
189dfeee0a8SGreg Roach			' "paginate": {' .
190651f5a91SGreg Roach			'  "first":    "' . /* I18N: A button label, first page */ self::translate('first') . '",' .
191651f5a91SGreg Roach			'  "last":     "' . /* I18N: A button label, last page */ self::translate('last') . '",' .
192651f5a91SGreg Roach			'  "next":     "' . /* I18N: A button label, next page */ self::translate('next') . '",' .
193651f5a91SGreg Roach			'  "previous": "' . /* I18N: A button label, previous page */ self::translate('previous') . '"' .
194dfeee0a8SGreg Roach			' },' .
195dfeee0a8SGreg Roach			' "emptyTable":     "' . self::translate('No records to display') . '",' .
196dfeee0a8SGreg Roach			' "info":           "' . /* I18N: %s are placeholders for numbers */ self::translate('Showing %1$s to %2$s of %3$s', '_START_', '_END_', '_TOTAL_') . '",' .
197dfeee0a8SGreg Roach			' "infoEmpty":      "' . self::translate('Showing %1$s to %2$s of %3$s', 0, 0, 0) . '",' .
198dfeee0a8SGreg Roach			' "infoFiltered":   "' . /* I18N: %s is a placeholder for a number */ self::translate('(filtered from %s total entries)', '_MAX_') . '",' .
199dfeee0a8SGreg Roach			' "lengthMenu":     "' . Filter::escapeJs($length_menu) . '",' .
200dfeee0a8SGreg Roach			' "loadingRecords": "' . self::translate('Loading…') . '",' .
201dfeee0a8SGreg Roach			' "processing":     "' . self::translate('Loading…') . '",' .
202dfeee0a8SGreg Roach			' "search":         "' . self::translate('Filter') . '",' .
203dfeee0a8SGreg Roach			' "zeroRecords":    "' . self::translate('No records to display') . '"' .
204*2d9b2ebaSGreg Roach			'}';
205dfeee0a8SGreg Roach	}
206dfeee0a8SGreg Roach
207dfeee0a8SGreg Roach	/**
208dfeee0a8SGreg Roach	 * Convert the digits 0-9 into the local script
209dfeee0a8SGreg Roach	 *
210dfeee0a8SGreg Roach	 * Used for years, etc., where we do not want thousands-separators, decimals, etc.
211dfeee0a8SGreg Roach	 *
212cbc1590aSGreg Roach	 * @param int $n
213dfeee0a8SGreg Roach	 *
214dfeee0a8SGreg Roach	 * @return string
215dfeee0a8SGreg Roach	 */
216dfeee0a8SGreg Roach	public static function digits($n) {
217dfeee0a8SGreg Roach		return self::$locale->digits($n);
218dfeee0a8SGreg Roach	}
219dfeee0a8SGreg Roach
220dfeee0a8SGreg Roach	/**
221dfeee0a8SGreg Roach	 * What is the direction of the current locale
222dfeee0a8SGreg Roach	 *
223dfeee0a8SGreg Roach	 * @return string "ltr" or "rtl"
224dfeee0a8SGreg Roach	 */
225dfeee0a8SGreg Roach	public static function direction() {
226dfeee0a8SGreg Roach		return self::$locale->direction();
227dfeee0a8SGreg Roach	}
228dfeee0a8SGreg Roach
229dfeee0a8SGreg Roach	/**
2307231a557SGreg Roach	 * What is the first day of the week.
2317231a557SGreg Roach	 *
232cbc1590aSGreg Roach	 * @return int Sunday=0, Monday=1, etc.
2337231a557SGreg Roach	 */
2347231a557SGreg Roach	public static function firstDay() {
2357231a557SGreg Roach		return self::$locale->territory()->firstDay();
2367231a557SGreg Roach	}
2377231a557SGreg Roach
2387231a557SGreg Roach	/**
239dfeee0a8SGreg Roach	 * Convert a GEDCOM age string into translated_text
240dfeee0a8SGreg Roach	 *
241dfeee0a8SGreg Roach	 * NB: The import function will have normalised this, so we don't need
242dfeee0a8SGreg Roach	 * to worry about badly formatted strings
2433d7a8a4cSGreg Roach	 * NOTE: this function is not yet complete - eventually it will replace FunctionsDate::get_age_at_event()
244dfeee0a8SGreg Roach	 *
245dfeee0a8SGreg Roach	 * @param $string
246dfeee0a8SGreg Roach	 *
247dfeee0a8SGreg Roach	 * @return string
248dfeee0a8SGreg Roach	 */
249dfeee0a8SGreg Roach	public static function gedcomAge($string) {
250dfeee0a8SGreg Roach		switch ($string) {
251dfeee0a8SGreg Roach		case 'STILLBORN':
252dfeee0a8SGreg Roach			// I18N: Description of an individual’s age at an event. For example, Died 14 Jan 1900 (stillborn)
253dfeee0a8SGreg Roach			return self::translate('(stillborn)');
254dfeee0a8SGreg Roach		case 'INFANT':
255dfeee0a8SGreg Roach			// I18N: Description of an individual’s age at an event. For example, Died 14 Jan 1900 (in infancy)
256dfeee0a8SGreg Roach			return self::translate('(in infancy)');
257dfeee0a8SGreg Roach		case 'CHILD':
258dfeee0a8SGreg Roach			// I18N: Description of an individual’s age at an event. For example, Died 14 Jan 1900 (in childhood)
259dfeee0a8SGreg Roach			return self::translate('(in childhood)');
260dfeee0a8SGreg Roach		}
26113abd6f3SGreg Roach		$age = [];
262dfeee0a8SGreg Roach		if (preg_match('/(\d+)y/', $string, $match)) {
263dfeee0a8SGreg Roach			// I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
264dfeee0a8SGreg Roach			$years = $match[1];
265dfeee0a8SGreg Roach			$age[] = self::plural('%s year', '%s years', $years, self::number($years));
266dfeee0a8SGreg Roach		} else {
267dfeee0a8SGreg Roach			$years = -1;
268dfeee0a8SGreg Roach		}
269dfeee0a8SGreg Roach		if (preg_match('/(\d+)m/', $string, $match)) {
270dfeee0a8SGreg Roach			// I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
271dfeee0a8SGreg Roach			$age[] = self::plural('%s month', '%s months', $match[1], self::number($match[1]));
272dfeee0a8SGreg Roach		}
273dfeee0a8SGreg Roach		if (preg_match('/(\d+)w/', $string, $match)) {
274dfeee0a8SGreg Roach			// I18N: Part of an age string. e.g. 7 weeks and 3 days
275dfeee0a8SGreg Roach			$age[] = self::plural('%s week', '%s weeks', $match[1], self::number($match[1]));
276dfeee0a8SGreg Roach		}
277dfeee0a8SGreg Roach		if (preg_match('/(\d+)d/', $string, $match)) {
278dfeee0a8SGreg Roach			// I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
279dfeee0a8SGreg Roach			$age[] = self::plural('%s day', '%s days', $match[1], self::number($match[1]));
280dfeee0a8SGreg Roach		}
281dfeee0a8SGreg Roach		// If an age is just a number of years, only show the number
282dfeee0a8SGreg Roach		if (count($age) === 1 && $years >= 0) {
283dfeee0a8SGreg Roach			$age = $years;
284dfeee0a8SGreg Roach		}
285dfeee0a8SGreg Roach		if ($age) {
286dfeee0a8SGreg Roach			if (!substr_compare($string, '<', 0, 1)) {
287dfeee0a8SGreg Roach				// I18N: Description of an individual’s age at an event. For example, Died 14 Jan 1900 (aged less than 21 years)
288dfeee0a8SGreg Roach				return self::translate('(aged less than %s)', $age);
289dfeee0a8SGreg Roach			} elseif (!substr_compare($string, '>', 0, 1)) {
290dfeee0a8SGreg Roach				// I18N: Description of an individual’s age at an event. For example, Died 14 Jan 1900 (aged more than 21 years)
291dfeee0a8SGreg Roach				return self::translate('(aged more than %s)', $age);
292dfeee0a8SGreg Roach			} else {
293dfeee0a8SGreg Roach				// I18N: Description of an individual’s age at an event. For example, Died 14 Jan 1900 (aged 43 years)
294dfeee0a8SGreg Roach				return self::translate('(aged %s)', $age);
295dfeee0a8SGreg Roach			}
296dfeee0a8SGreg Roach		} else {
297dfeee0a8SGreg Roach			// Not a valid string?
298dfeee0a8SGreg Roach			return self::translate('(aged %s)', $string);
299dfeee0a8SGreg Roach		}
300dfeee0a8SGreg Roach	}
301dfeee0a8SGreg Roach
302dfeee0a8SGreg Roach	/**
303dfeee0a8SGreg Roach	 * Generate i18n markup for the <html> tag, e.g. lang="ar" dir="rtl"
304dfeee0a8SGreg Roach	 *
305dfeee0a8SGreg Roach	 * @return string
306dfeee0a8SGreg Roach	 */
307dfeee0a8SGreg Roach	public static function htmlAttributes() {
308dfeee0a8SGreg Roach		return self::$locale->htmlAttributes();
309dfeee0a8SGreg Roach	}
310dfeee0a8SGreg Roach
311dfeee0a8SGreg Roach	/**
312a25f0a04SGreg Roach	 * Initialise the translation adapter with a locale setting.
313a25f0a04SGreg Roach	 *
3143bdc890bSGreg Roach	 * @param string|null $code Use this locale/language code, or choose one automatically
315a25f0a04SGreg Roach	 *
316a25f0a04SGreg Roach	 * @return string $string
317a25f0a04SGreg Roach	 */
3183bdc890bSGreg Roach	public static function init($code = null) {
31931bc7874SGreg Roach		global $WT_TREE;
320a25f0a04SGreg Roach
321c314ecc9SGreg Roach		mb_internal_encoding('UTF-8');
322c314ecc9SGreg Roach
3233bdc890bSGreg Roach		if ($code !== null) {
3243bdc890bSGreg Roach			// Create the specified locale
3253bdc890bSGreg Roach			self::$locale = Locale::create($code);
3263bdc890bSGreg Roach		} else {
3273bdc890bSGreg Roach			// Negotiate a locale, but if we can't then use a failsafe
3283bdc890bSGreg Roach			self::$locale = new LocaleEnUs;
3299b134a0aSGreg Roach			if (Session::has('locale')) {
3303bdc890bSGreg Roach				// Previously used
33131bc7874SGreg Roach				self::$locale = Locale::create(Session::get('locale'));
3323bdc890bSGreg Roach			} else {
3331e71bdc0SGreg Roach				// Browser negotiation
3341e71bdc0SGreg Roach				$default_locale = new LocaleEnUs;
3353bdc890bSGreg Roach				try {
3361e71bdc0SGreg Roach					if ($WT_TREE) {
3371e71bdc0SGreg Roach						$default_locale = Locale::create($WT_TREE->getPreference('LANGUAGE'));
3383bdc890bSGreg Roach					}
3393bdc890bSGreg Roach				} catch (\Exception $ex) {
3403bdc890bSGreg Roach				}
341149573a1SGreg Roach				self::$locale = Locale::httpAcceptLanguage($_SERVER, self::installedLocales(), $default_locale);
3423bdc890bSGreg Roach			}
3433bdc890bSGreg Roach		}
3443bdc890bSGreg Roach
345f1af7e1cSGreg Roach		$cache_dir  = WT_DATA_DIR . 'cache/';
346f1af7e1cSGreg Roach		$cache_file = $cache_dir . 'language-' . self::$locale->languageTag() . '-cache.php';
3473bdc890bSGreg Roach		if (file_exists($cache_file)) {
3483bdc890bSGreg Roach			$filemtime = filemtime($cache_file);
3493bdc890bSGreg Roach		} else {
3503bdc890bSGreg Roach			$filemtime = 0;
3513bdc890bSGreg Roach		}
3523bdc890bSGreg Roach
3533bdc890bSGreg Roach		// Load the translation file(s)
3547d6e38dfSGreg Roach		// Note that glob() returns false instead of an empty array when open_basedir_restriction
3557d6e38dfSGreg Roach		// is in force and no files are found. See PHP bug #47358.
3567a7f87d7SGreg Roach		if (defined('GLOB_BRACE')) {
3573bdc890bSGreg Roach			$translation_files = array_merge(
35813abd6f3SGreg Roach				[WT_ROOT . 'language/' . self::$locale->languageTag() . '.mo'],
35913abd6f3SGreg Roach				glob(WT_MODULES_DIR . '*/language/' . self::$locale->languageTag() . '.{csv,php,mo}', GLOB_BRACE) ?: [],
36013abd6f3SGreg Roach				glob(WT_DATA_DIR . 'language/' . self::$locale->languageTag() . '.{csv,php,mo}', GLOB_BRACE) ?: []
361a25f0a04SGreg Roach			);
3627a7f87d7SGreg Roach		} else {
3637a7f87d7SGreg Roach			// Some servers do not have GLOB_BRACE - see http://php.net/manual/en/function.glob.php
3647a7f87d7SGreg Roach			$translation_files = array_merge(
36513abd6f3SGreg Roach				[WT_ROOT . 'language/' . self::$locale->languageTag() . '.mo'],
36613abd6f3SGreg Roach				glob(WT_MODULES_DIR . '*/language/' . self::$locale->languageTag() . '.csv') ?: [],
36713abd6f3SGreg Roach				glob(WT_MODULES_DIR . '*/language/' . self::$locale->languageTag() . '.php') ?: [],
36813abd6f3SGreg Roach				glob(WT_MODULES_DIR . '*/language/' . self::$locale->languageTag() . '.mo') ?: [],
36913abd6f3SGreg Roach				glob(WT_DATA_DIR . 'language/' . self::$locale->languageTag() . '.csv') ?: [],
37013abd6f3SGreg Roach				glob(WT_DATA_DIR . 'language/' . self::$locale->languageTag() . '.php') ?: [],
37113abd6f3SGreg Roach				glob(WT_DATA_DIR . 'language/' . self::$locale->languageTag() . '.mo') ?: []
3727a7f87d7SGreg Roach			);
3737a7f87d7SGreg Roach		}
3747a7f87d7SGreg Roach		// Rebuild files after one hour
3757a7f87d7SGreg Roach		$rebuild_cache = time() > $filemtime + 3600;
3761e71bdc0SGreg Roach		// Rebuild files if any translation file has been updated
3773bdc890bSGreg Roach		foreach ($translation_files as $translation_file) {
3783bdc890bSGreg Roach			if (filemtime($translation_file) > $filemtime) {
3793bdc890bSGreg Roach				$rebuild_cache = true;
380a25f0a04SGreg Roach				break;
381a25f0a04SGreg Roach			}
382a25f0a04SGreg Roach		}
3833bdc890bSGreg Roach
3843bdc890bSGreg Roach		if ($rebuild_cache) {
38513abd6f3SGreg Roach			$translations = [];
3863bdc890bSGreg Roach			foreach ($translation_files as $translation_file) {
3873bdc890bSGreg Roach				$translation  = new Translation($translation_file);
3883bdc890bSGreg Roach				$translations = array_merge($translations, $translation->asArray());
389a25f0a04SGreg Roach			}
390f1af7e1cSGreg Roach			try {
391f1af7e1cSGreg Roach				File::mkdir($cache_dir);
392f1af7e1cSGreg Roach				file_put_contents($cache_file, '<?php return ' . var_export($translations, true) . ';');
393f1af7e1cSGreg Roach			} catch (Exception $ex) {
3947c2999b4SGreg Roach				// During setup, we may not have been able to create it.
395c85fb0c4SGreg Roach			}
3963bdc890bSGreg Roach		} else {
3973bdc890bSGreg Roach			$translations = include $cache_file;
398a25f0a04SGreg Roach		}
399a25f0a04SGreg Roach
4003bdc890bSGreg Roach		// Create a translator
4013bdc890bSGreg Roach		self::$translator = new Translator($translations, self::$locale->pluralRule());
402a25f0a04SGreg Roach
4033cfaf201SGreg Roach		self::$list_separator = /* I18N: This punctuation is used to separate lists of items */ self::translate(', ');
404a25f0a04SGreg Roach
405991b93ddSGreg Roach		// Create a collator
406991b93ddSGreg Roach		try {
407991b93ddSGreg Roach			self::$collator = new Collator(self::$locale->code());
408991b93ddSGreg Roach			// Ignore upper/lower case differences
409991b93ddSGreg Roach			self::$collator->setStrength(Collator::SECONDARY);
410991b93ddSGreg Roach		} catch (Exception $ex) {
411991b93ddSGreg Roach			// PHP-INTL is not installed?  We'll use a fallback later.
412991b93ddSGreg Roach		}
413991b93ddSGreg Roach
4145331c5eaSGreg Roach		return self::$locale->languageTag();
415a25f0a04SGreg Roach	}
416a25f0a04SGreg Roach
417a25f0a04SGreg Roach	/**
418c999a340SGreg Roach	 * All locales for which a translation file exists.
419c999a340SGreg Roach	 *
42015834aaeSGreg Roach	 * @return LocaleInterface[]
421c999a340SGreg Roach	 */
422c999a340SGreg Roach	public static function installedLocales() {
42313abd6f3SGreg Roach		$locales = [];
424c999a340SGreg Roach		foreach (glob(WT_ROOT . 'language/*.mo') as $file) {
425c999a340SGreg Roach			try {
426c999a340SGreg Roach				$locales[] = Locale::create(basename($file, '.mo'));
427c999a340SGreg Roach			} catch (\Exception $ex) {
4283bdc890bSGreg Roach				// Not a recognised locale
429a25f0a04SGreg Roach			}
430a25f0a04SGreg Roach		}
431c999a340SGreg Roach		usort($locales, '\Fisharebest\Localization\Locale::compare');
432c999a340SGreg Roach
433c999a340SGreg Roach		return $locales;
434a25f0a04SGreg Roach	}
435a25f0a04SGreg Roach
436a25f0a04SGreg Roach	/**
437a25f0a04SGreg Roach	 * Return the endonym for a given language - as per http://cldr.unicode.org/
438a25f0a04SGreg Roach	 *
439a25f0a04SGreg Roach	 * @param string $locale
440a25f0a04SGreg Roach	 *
441a25f0a04SGreg Roach	 * @return string
442a25f0a04SGreg Roach	 */
443a25f0a04SGreg Roach	public static function languageName($locale) {
444c999a340SGreg Roach		return Locale::create($locale)->endonym();
445a25f0a04SGreg Roach	}
446a25f0a04SGreg Roach
447a25f0a04SGreg Roach	/**
448a25f0a04SGreg Roach	 * Return the script used by a given language
449a25f0a04SGreg Roach	 *
450a25f0a04SGreg Roach	 * @param string $locale
451a25f0a04SGreg Roach	 *
452a25f0a04SGreg Roach	 * @return string
453a25f0a04SGreg Roach	 */
454a25f0a04SGreg Roach	public static function languageScript($locale) {
455c999a340SGreg Roach		return Locale::create($locale)->script()->code();
456a25f0a04SGreg Roach	}
457a25f0a04SGreg Roach
458a25f0a04SGreg Roach	/**
459dfeee0a8SGreg Roach	 * Translate a number into the local representation.
460a25f0a04SGreg Roach	 *
461dfeee0a8SGreg Roach	 * e.g. 12345.67 becomes
462dfeee0a8SGreg Roach	 * en: 12,345.67
463dfeee0a8SGreg Roach	 * fr: 12 345,67
464dfeee0a8SGreg Roach	 * de: 12.345,67
465dfeee0a8SGreg Roach	 *
466dfeee0a8SGreg Roach	 * @param float $n
467cbc1590aSGreg Roach	 * @param int   $precision
468a25f0a04SGreg Roach	 *
469a25f0a04SGreg Roach	 * @return string
470a25f0a04SGreg Roach	 */
471dfeee0a8SGreg Roach	public static function number($n, $precision = 0) {
472dfeee0a8SGreg Roach		return self::$locale->number(round($n, $precision));
473dfeee0a8SGreg Roach	}
474dfeee0a8SGreg Roach
475dfeee0a8SGreg Roach	/**
476dfeee0a8SGreg Roach	 * Translate a fraction into a percentage.
477dfeee0a8SGreg Roach	 *
478dfeee0a8SGreg Roach	 * e.g. 0.123 becomes
479dfeee0a8SGreg Roach	 * en: 12.3%
480dfeee0a8SGreg Roach	 * fr: 12,3 %
481dfeee0a8SGreg Roach	 * de: 12,3%
482dfeee0a8SGreg Roach	 *
483dfeee0a8SGreg Roach	 * @param float $n
484cbc1590aSGreg Roach	 * @param int   $precision
485dfeee0a8SGreg Roach	 *
486dfeee0a8SGreg Roach	 * @return string
487dfeee0a8SGreg Roach	 */
488dfeee0a8SGreg Roach	public static function percentage($n, $precision = 0) {
489dfeee0a8SGreg Roach		return self::$locale->percent(round($n, $precision + 2));
490dfeee0a8SGreg Roach	}
491dfeee0a8SGreg Roach
492dfeee0a8SGreg Roach	/**
493dfeee0a8SGreg Roach	 * Translate a plural string
494dfeee0a8SGreg Roach	 *
495dfeee0a8SGreg Roach	 * echo self::plural('There is an error', 'There are errors', $num_errors);
496dfeee0a8SGreg Roach	 * echo self::plural('There is one error', 'There are %s errors', $num_errors);
497dfeee0a8SGreg Roach	 * echo self::plural('There is %1$s %2$s cat', 'There are %1$s %2$s cats', $num, $num, $colour);
498dfeee0a8SGreg Roach	 *
499dfeee0a8SGreg Roach	 * @return string
500dfeee0a8SGreg Roach	 */
501dfeee0a8SGreg Roach	public static function plural(/* var_args */) {
502dfeee0a8SGreg Roach		$args    = func_get_args();
503beb72eacSGreg Roach		$args[0] = self::$translator->translatePlural($args[0], $args[1], (int) $args[2]);
504dfeee0a8SGreg Roach		unset($args[1], $args[2]);
505dfeee0a8SGreg Roach
506ffa2e252SGreg Roach		return self::substitutePlaceholders($args);
507dfeee0a8SGreg Roach	}
508dfeee0a8SGreg Roach
509dfeee0a8SGreg Roach	/**
510dfeee0a8SGreg Roach	 * UTF8 version of PHP::strrev()
511dfeee0a8SGreg Roach	 *
512dfeee0a8SGreg Roach	 * Reverse RTL text for third-party libraries such as GD2 and googlechart.
513dfeee0a8SGreg Roach	 *
514dfeee0a8SGreg Roach	 * These do not support UTF8 text direction, so we must mimic it for them.
515dfeee0a8SGreg Roach	 *
516dfeee0a8SGreg Roach	 * Numbers are always rendered LTR, even in RTL text.
517dfeee0a8SGreg Roach	 * The visual direction of characters such as parentheses should be reversed.
518dfeee0a8SGreg Roach	 *
519dfeee0a8SGreg Roach	 * @param string $text Text to be reversed
520dfeee0a8SGreg Roach	 *
521dfeee0a8SGreg Roach	 * @return string
522dfeee0a8SGreg Roach	 */
523dfeee0a8SGreg Roach	public static function reverseText($text) {
524dfeee0a8SGreg Roach		// Remove HTML markup - we can't display it and it is LTR.
525dfeee0a8SGreg Roach		$text = Filter::unescapeHtml($text);
526dfeee0a8SGreg Roach
527dfeee0a8SGreg Roach		// LTR text doesn't need reversing
528dfeee0a8SGreg Roach		if (self::scriptDirection(self::textScript($text)) === 'ltr') {
529dfeee0a8SGreg Roach			return $text;
530dfeee0a8SGreg Roach		}
531dfeee0a8SGreg Roach
532dfeee0a8SGreg Roach		// Mirrored characters
533991b93ddSGreg Roach		$text = strtr($text, self::MIRROR_CHARACTERS);
534dfeee0a8SGreg Roach
535dfeee0a8SGreg Roach		$reversed = '';
536dfeee0a8SGreg Roach		$digits   = '';
537dfeee0a8SGreg Roach		while ($text != '') {
538dfeee0a8SGreg Roach			$letter = mb_substr($text, 0, 1);
539dfeee0a8SGreg Roach			$text   = mb_substr($text, 1);
540dfeee0a8SGreg Roach			if (strpos(self::DIGITS, $letter) !== false) {
541dfeee0a8SGreg Roach				$digits .= $letter;
542a25f0a04SGreg Roach			} else {
543dfeee0a8SGreg Roach				$reversed = $letter . $digits . $reversed;
544dfeee0a8SGreg Roach				$digits   = '';
545dfeee0a8SGreg Roach			}
546a25f0a04SGreg Roach		}
547a25f0a04SGreg Roach
548dfeee0a8SGreg Roach		return $digits . $reversed;
549a25f0a04SGreg Roach	}
550a25f0a04SGreg Roach
551a25f0a04SGreg Roach	/**
552a25f0a04SGreg Roach	 * Return the direction (ltr or rtl) for a given script
553a25f0a04SGreg Roach	 *
554a25f0a04SGreg Roach	 * The PHP/intl library does not provde this information, so we need
555a25f0a04SGreg Roach	 * our own lookup table.
556a25f0a04SGreg Roach	 *
557a25f0a04SGreg Roach	 * @param string $script
558a25f0a04SGreg Roach	 *
559a25f0a04SGreg Roach	 * @return string
560a25f0a04SGreg Roach	 */
561a25f0a04SGreg Roach	public static function scriptDirection($script) {
562a25f0a04SGreg Roach		switch ($script) {
563a25f0a04SGreg Roach		case 'Arab':
564a25f0a04SGreg Roach		case 'Hebr':
565a25f0a04SGreg Roach		case 'Mong':
566a25f0a04SGreg Roach		case 'Thaa':
567a25f0a04SGreg Roach			return 'rtl';
568a25f0a04SGreg Roach		default:
569a25f0a04SGreg Roach			return 'ltr';
570a25f0a04SGreg Roach		}
571a25f0a04SGreg Roach	}
572a25f0a04SGreg Roach
573a25f0a04SGreg Roach	/**
574991b93ddSGreg Roach	 * Perform a case-insensitive comparison of two strings.
575a25f0a04SGreg Roach	 *
576a25f0a04SGreg Roach	 * @param string $string1
577a25f0a04SGreg Roach	 * @param string $string2
578a25f0a04SGreg Roach	 *
579cbc1590aSGreg Roach	 * @return int
580a25f0a04SGreg Roach	 */
581a25f0a04SGreg Roach	public static function strcasecmp($string1, $string2) {
582991b93ddSGreg Roach		if (self::$collator instanceof Collator) {
583991b93ddSGreg Roach			return self::$collator->compare($string1, $string2);
584a25f0a04SGreg Roach		} else {
585991b93ddSGreg Roach			return strcmp(self::strtolower($string1), self::strtolower($string2));
586a25f0a04SGreg Roach		}
587a25f0a04SGreg Roach	}
588a25f0a04SGreg Roach
589a25f0a04SGreg Roach	/**
590991b93ddSGreg Roach	 * Convert a string to lower case.
591a25f0a04SGreg Roach	 *
592dfeee0a8SGreg Roach	 * @param string $string
593a25f0a04SGreg Roach	 *
594a25f0a04SGreg Roach	 * @return string
595a25f0a04SGreg Roach	 */
596dfeee0a8SGreg Roach	public static function strtolower($string) {
597991b93ddSGreg Roach		if (in_array(self::$locale->language()->code(), self::DOTLESS_I_LOCALES)) {
598991b93ddSGreg Roach			$string = strtr($string, self::DOTLESS_I_TOLOWER);
599a25f0a04SGreg Roach		}
6005ddad20bSGreg Roach
6015ddad20bSGreg Roach		return mb_strtolower($string);
602a25f0a04SGreg Roach	}
603a25f0a04SGreg Roach
604a25f0a04SGreg Roach	/**
605991b93ddSGreg Roach	 * Convert a string to upper case.
606dfeee0a8SGreg Roach	 *
607dfeee0a8SGreg Roach	 * @param string $string
608a25f0a04SGreg Roach	 *
609a25f0a04SGreg Roach	 * @return string
610a25f0a04SGreg Roach	 */
611dfeee0a8SGreg Roach	public static function strtoupper($string) {
612991b93ddSGreg Roach		if (in_array(self::$locale->language()->code(), self::DOTLESS_I_LOCALES)) {
613991b93ddSGreg Roach			$string = strtr($string, self::DOTLESS_I_TOUPPER);
614a25f0a04SGreg Roach		}
6155ddad20bSGreg Roach
6165ddad20bSGreg Roach		return mb_strtoupper($string);
617a25f0a04SGreg Roach	}
618a25f0a04SGreg Roach
619dfeee0a8SGreg Roach	/**
620ffa2e252SGreg Roach	 * Substitute any "%s" placeholders in a translated string.
621ffa2e252SGreg Roach	 * This also allows us to have translated strings that contain
622ffa2e252SGreg Roach	 * "%" characters, which can't be passed to sprintf.
623ffa2e252SGreg Roach	 *
624ffa2e252SGreg Roach	 * @param string[] $args translated string plus optional parameters
625ffa2e252SGreg Roach	 *
626ffa2e252SGreg Roach	 * @return string
627ffa2e252SGreg Roach	 */
628ffa2e252SGreg Roach	private static function substitutePlaceholders(array $args) {
629ffa2e252SGreg Roach		if (count($args) > 1) {
630ffa2e252SGreg Roach			return call_user_func_array('sprintf', $args);
631ffa2e252SGreg Roach		} else {
632ffa2e252SGreg Roach			return $args[0];
633ffa2e252SGreg Roach		}
634ffa2e252SGreg Roach	}
635ffa2e252SGreg Roach
636ffa2e252SGreg Roach	/**
637dfeee0a8SGreg Roach	 * Identify the script used for a piece of text
638dfeee0a8SGreg Roach	 *
639dfeee0a8SGreg Roach	 * @param $string
640dfeee0a8SGreg Roach	 *
641dfeee0a8SGreg Roach	 * @return string
642dfeee0a8SGreg Roach	 */
643dfeee0a8SGreg Roach	public static function textScript($string) {
644dfeee0a8SGreg Roach		$string = strip_tags($string); // otherwise HTML tags show up as latin
645dfeee0a8SGreg Roach		$string = html_entity_decode($string, ENT_QUOTES, 'UTF-8'); // otherwise HTML entities show up as latin
64613abd6f3SGreg Roach		$string = str_replace(['@N.N.', '@P.N.'], '', $string); // otherwise unknown names show up as latin
647dfeee0a8SGreg Roach		$pos    = 0;
648dfeee0a8SGreg Roach		$strlen = strlen($string);
649dfeee0a8SGreg Roach		while ($pos < $strlen) {
650dfeee0a8SGreg Roach			// get the Unicode Code Point for the character at position $pos
651dfeee0a8SGreg Roach			$byte1 = ord($string[$pos]);
652dfeee0a8SGreg Roach			if ($byte1 < 0x80) {
653dfeee0a8SGreg Roach				$code_point = $byte1;
654dfeee0a8SGreg Roach				$chrlen     = 1;
655dfeee0a8SGreg Roach			} elseif ($byte1 < 0xC0) {
656dfeee0a8SGreg Roach				// Invalid continuation character
657dfeee0a8SGreg Roach				return 'Latn';
658dfeee0a8SGreg Roach			} elseif ($byte1 < 0xE0) {
659dfeee0a8SGreg Roach				$code_point = (($byte1 & 0x1F) << 6) + (ord($string[$pos + 1]) & 0x3F);
660dfeee0a8SGreg Roach				$chrlen     = 2;
661dfeee0a8SGreg Roach			} elseif ($byte1 < 0xF0) {
662dfeee0a8SGreg Roach				$code_point = (($byte1 & 0x0F) << 12) + ((ord($string[$pos + 1]) & 0x3F) << 6) + (ord($string[$pos + 2]) & 0x3F);
663dfeee0a8SGreg Roach				$chrlen     = 3;
664dfeee0a8SGreg Roach			} elseif ($byte1 < 0xF8) {
665dfeee0a8SGreg Roach				$code_point = (($byte1 & 0x07) << 24) + ((ord($string[$pos + 1]) & 0x3F) << 12) + ((ord($string[$pos + 2]) & 0x3F) << 6) + (ord($string[$pos + 3]) & 0x3F);
666dfeee0a8SGreg Roach				$chrlen     = 3;
667dfeee0a8SGreg Roach			} else {
668dfeee0a8SGreg Roach				// Invalid UTF
669dfeee0a8SGreg Roach				return 'Latn';
670dfeee0a8SGreg Roach			}
671dfeee0a8SGreg Roach
672991b93ddSGreg Roach			foreach (self::SCRIPT_CHARACTER_RANGES as $range) {
673dfeee0a8SGreg Roach				if ($code_point >= $range[1] && $code_point <= $range[2]) {
674dfeee0a8SGreg Roach					return $range[0];
675dfeee0a8SGreg Roach				}
676dfeee0a8SGreg Roach			}
677dfeee0a8SGreg Roach			// Not a recognised script. Maybe punctuation, spacing, etc. Keep looking.
678dfeee0a8SGreg Roach			$pos += $chrlen;
679dfeee0a8SGreg Roach		}
680dfeee0a8SGreg Roach
681dfeee0a8SGreg Roach		return 'Latn';
682dfeee0a8SGreg Roach	}
683dfeee0a8SGreg Roach
684dfeee0a8SGreg Roach	/**
685dfeee0a8SGreg Roach	 * Convert a number of seconds into a relative time. For example, 630 => "10 hours, 30 minutes ago"
686dfeee0a8SGreg Roach	 *
687cbc1590aSGreg Roach	 * @param int $seconds
688dfeee0a8SGreg Roach	 *
689dfeee0a8SGreg Roach	 * @return string
690dfeee0a8SGreg Roach	 */
691dfeee0a8SGreg Roach	public static function timeAgo($seconds) {
692dfeee0a8SGreg Roach		$minute = 60;
693dfeee0a8SGreg Roach		$hour   = 60 * $minute;
694dfeee0a8SGreg Roach		$day    = 24 * $hour;
695dfeee0a8SGreg Roach		$month  = 30 * $day;
696dfeee0a8SGreg Roach		$year   = 365 * $day;
697dfeee0a8SGreg Roach
698dfeee0a8SGreg Roach		if ($seconds > $year) {
699dfeee0a8SGreg Roach			$years = (int) ($seconds / $year);
700cbc1590aSGreg Roach
701dfeee0a8SGreg Roach			return self::plural('%s year ago', '%s years ago', $years, self::number($years));
702dfeee0a8SGreg Roach		} elseif ($seconds > $month) {
703dfeee0a8SGreg Roach			$months = (int) ($seconds / $month);
704cbc1590aSGreg Roach
705dfeee0a8SGreg Roach			return self::plural('%s month ago', '%s months ago', $months, self::number($months));
706dfeee0a8SGreg Roach		} elseif ($seconds > $day) {
707dfeee0a8SGreg Roach			$days = (int) ($seconds / $day);
708cbc1590aSGreg Roach
709dfeee0a8SGreg Roach			return self::plural('%s day ago', '%s days ago', $days, self::number($days));
710dfeee0a8SGreg Roach		} elseif ($seconds > $hour) {
711dfeee0a8SGreg Roach			$hours = (int) ($seconds / $hour);
712cbc1590aSGreg Roach
713dfeee0a8SGreg Roach			return self::plural('%s hour ago', '%s hours ago', $hours, self::number($hours));
714dfeee0a8SGreg Roach		} elseif ($seconds > $minute) {
715dfeee0a8SGreg Roach			$minutes = (int) ($seconds / $minute);
716cbc1590aSGreg Roach
717dfeee0a8SGreg Roach			return self::plural('%s minute ago', '%s minutes ago', $minutes, self::number($minutes));
718dfeee0a8SGreg Roach		} else {
719dfeee0a8SGreg Roach			return self::plural('%s second ago', '%s seconds ago', $seconds, self::number($seconds));
720dfeee0a8SGreg Roach		}
721dfeee0a8SGreg Roach	}
722dfeee0a8SGreg Roach
723dfeee0a8SGreg Roach	/**
724dfeee0a8SGreg Roach	 * What format is used to display dates in the current locale?
725dfeee0a8SGreg Roach	 *
726dfeee0a8SGreg Roach	 * @return string
727dfeee0a8SGreg Roach	 */
728dfeee0a8SGreg Roach	public static function timeFormat() {
729dfeee0a8SGreg Roach		return /* I18N: This is the format string for the time-of-day. See http://php.net/date for codes */ self::$translator->translate('%H:%i:%s');
730dfeee0a8SGreg Roach	}
731dfeee0a8SGreg Roach
732dfeee0a8SGreg Roach	/**
733dfeee0a8SGreg Roach	 * Translate a string, and then substitute placeholders
734dfeee0a8SGreg Roach	 *
735dfeee0a8SGreg Roach	 * echo I18N::translate('Hello World!');
736dfeee0a8SGreg Roach	 * echo I18N::translate('The %s sat on the mat', 'cat');
737dfeee0a8SGreg Roach	 *
738dfeee0a8SGreg Roach	 * @return string
739dfeee0a8SGreg Roach	 */
740dfeee0a8SGreg Roach	public static function translate(/* var_args */) {
741dfeee0a8SGreg Roach		$args    = func_get_args();
742dfeee0a8SGreg Roach		$args[0] = self::$translator->translate($args[0]);
743dfeee0a8SGreg Roach
744ffa2e252SGreg Roach		return self::substitutePlaceholders($args);
745dfeee0a8SGreg Roach	}
746dfeee0a8SGreg Roach
747dfeee0a8SGreg Roach	/**
748dfeee0a8SGreg Roach	 * Context sensitive version of translate.
749dfeee0a8SGreg Roach	 *
750a4956c0eSGreg Roach	 * echo I18N::translateContext('NOMINATIVE', 'January');
751a4956c0eSGreg Roach	 * echo I18N::translateContext('GENITIVE', 'January');
752dfeee0a8SGreg Roach	 *
753dfeee0a8SGreg Roach	 * @return string
754dfeee0a8SGreg Roach	 */
755dfeee0a8SGreg Roach	public static function translateContext(/* var_args */) {
756dfeee0a8SGreg Roach		$args    = func_get_args();
757dfeee0a8SGreg Roach		$args[0] = self::$translator->translateContext($args[0], $args[1]);
758dfeee0a8SGreg Roach		unset($args[1]);
759dfeee0a8SGreg Roach
760ffa2e252SGreg Roach		return self::substitutePlaceholders($args);
761a25f0a04SGreg Roach	}
7627231a557SGreg Roach
7637231a557SGreg Roach	/**
7647231a557SGreg Roach	 * What is the last day of the weekend.
7657231a557SGreg Roach	 *
766cbc1590aSGreg Roach	 * @return int Sunday=0, Monday=1, etc.
7677231a557SGreg Roach	 */
7687231a557SGreg Roach	public static function weekendEnd() {
7697231a557SGreg Roach		return self::$locale->territory()->weekendEnd();
7707231a557SGreg Roach	}
7717231a557SGreg Roach
7727231a557SGreg Roach	/**
7737231a557SGreg Roach	 * What is the first day of the weekend.
7747231a557SGreg Roach	 *
775cbc1590aSGreg Roach	 * @return int Sunday=0, Monday=1, etc.
7767231a557SGreg Roach	 */
7777231a557SGreg Roach	public static function weekendStart() {
7787231a557SGreg Roach		return self::$locale->territory()->weekendStart();
7797231a557SGreg Roach	}
780c4d5db72SDavid Drury
781c4d5db72SDavid Drury	/**
78227a79457SGreg Roach	 * Which calendar prefered in this locale?
78327a79457SGreg Roach	 *
78427a79457SGreg Roach	 * @return CalendarInterface
785c4d5db72SDavid Drury	 */
786c4d5db72SDavid Drury	public static function defaultCalendar() {
78727a79457SGreg Roach		switch (self::$locale->languageTag()) {
78827a79457SGreg Roach		case 'ar':
78927a79457SGreg Roach			return new ArabicCalendar;
79027a79457SGreg Roach		case 'fa':
79127a79457SGreg Roach			return new PersianCalendar;
79227a79457SGreg Roach		case 'he':
79327a79457SGreg Roach		case 'yi':
79427a79457SGreg Roach			return new JewishCalendar;
79527a79457SGreg Roach		default:
79627a79457SGreg Roach			return new GregorianCalendar;
79727a79457SGreg Roach		}
798c4d5db72SDavid Drury	}
799a25f0a04SGreg Roach}
800