xref: /webtrees/app/I18N.php (revision 15834aae1622f0d5ca0bc01cccc1ac35a02bdd1f)
1<?php
2namespace Fisharebest\Webtrees;
3
4/**
5 * webtrees: online genealogy
6 * Copyright (C) 2015 webtrees development team
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19use Fisharebest\Localization\Locale;
20use Fisharebest\Localization\Locale\LocaleEnUs;
21use Fisharebest\Localization\Locale\LocaleInterface;
22use Fisharebest\Localization\Translation;
23use Fisharebest\Localization\Translator;
24use Patchwork\TurkishUtf8;
25
26/**
27 * Class I18N - Functions to support internationalization (i18n) functionality.
28 */
29class I18N {
30	/** @var LocaleInterface The current locale (e.g. LocaleEnGb) */
31	private static $locale;
32
33	/** @var Translator */
34	private static $translator;
35
36	// Digits are always rendered LTR, even in RTL text.
37	const DIGITS = '0123456789٠١٢٣٤٥٦٧٨٩۰۱۲۳۴۵۶۷۸۹';
38
39	// Reversable character conversions from the UNICODE 5.1 database.
40	// It excludes ambiguous (turkish dotless i) and mixed-case (Dz) characters.
41	// The characters should be arranged in default unicode-collation order.
42	const ALPHABET_LOWER = 'aàáâãäåāăąǎǟǡǻȁȃȧḁạảấầẩẫậắằẳẵặⓐaæǣǽbḃḅḇⓑbƀɓƃcçćĉċčḉⅽⓒcƈdďḋḍḏḑḓⅾⓓddždzđɖɗƌðeèéêëēĕėęěȅȇȩḕḗḙḛḝẹẻẽếềểễệⓔeǝəɛfḟⓕfƒgĝğġģǧǵḡⓖgǥɠɣƣhĥȟḣḥḧḩḫⓗhƕħiìíîïĩīĭįǐȉȋḭḯỉịⅰⓘiⅱⅲijⅳⅸɨɩjĵⓙjkķǩḱḳḵⓚkƙlĺļľḷḹḻḽⅼⓛlŀljłƚmḿṁṃⅿⓜmnñńņňǹṅṇṉṋⓝnnjɲƞŋoòóôõöōŏőơǒǫǭȍȏȫȭȯȱṍṏṑṓọỏốồổỗộớờởỡợⓞoœøǿɔɵȣpṕṗⓟpƥqⓠqrŕŗřȑȓṙṛṝṟⓡrʀsśŝşšșṡṣṥṧṩⓢsʃtţťțṫṭṯṱⓣtŧƭʈuùúûüũūŭůűųưǔǖǘǚǜȕȗṳṵṷṹṻụủứừửữựⓤuʉɯʊvṽṿⅴⓥvⅵⅶⅷʋʌwŵẁẃẅẇẉⓦwxẋẍⅹⓧxⅺⅻyýÿŷȳẏỳỵỷỹⓨyƴzźżžẑẓẕⓩzƶȥǯʒƹȝþƿƨƽƅάαἀἁἂἃἄἅἆἇὰάᾀᾁᾂᾃᾄᾅᾆᾇᾰᾱᾳβγδέεἐἑἒἓἔἕὲέϝϛζήηἠἡἢἣἤἥἦἧὴήᾐᾑᾒᾓᾔᾕᾖᾗῃθϊἰἱἲἳἴἵἶἷὶίῐῑκϗλμνξοόὀὁὂὃὄὅὸόπϟϙρῥσϲτυϋύὑὓὕὗὺύῠῡφχψωώὠὡὢὣὤὥὦὧὼώᾠᾡᾢᾣᾤᾥᾦᾧῳϡϸϻϣϥϧϩϫϭϯаӑӓәӛӕбвгґғҕдԁђԃѓҙеѐёӗєжӂӝҗзԅӟѕӡԇиѝӣҋӥіїйјкқӄҡҟҝлӆљԉмӎнӊңӈҥњԋоӧөӫпҧҁрҏсԍҫтԏҭћќуӯўӱӳүұѹфхҳһѡѿѽѻцҵчӵҷӌҹҽҿџшщъыӹьҍѣэӭюяѥѧѫѩѭѯѱѳѵѷҩաբգդեզէըթժիլխծկհձղճմյնշոչպջռսվտրցւփքօֆȼɂɇɉɋɍɏͱͳͷͻͼͽӏӷӻӽӿԑԓԕԗԙԛԝԟԡԣԥᵹᵽỻỽỿⅎↄⰰⰱⰲⰳⰴⰵⰶⰷⰸⰹⰺⰻⰼⰽⰾⰿⱀⱁⱂⱃⱄⱅⱆⱇⱈⱉⱊⱋⱌⱍⱎⱏⱐⱑⱒⱓⱔⱕⱖⱗⱘⱙⱚⱛⱜⱝⱞⱡⱨⱪⱬⱳⱶⲁⲃⲅⲇⲉⲋⲍⲏⲑⲓⲕⲗⲙⲛⲝⲟⲡⲣⲥⲧⲩⲫⲭⲯⲱⲳⲵⲷⲹⲻⲽⲿⳁⳃⳅⳇⳉⳋⳍⳏⳑⳓⳕⳗⳙⳛⳝⳟⳡⳣⳬⳮⴀⴁⴂⴃⴄⴅⴆⴇⴈⴉⴊⴋⴌⴍⴎⴏⴐⴑⴒⴓⴔⴕⴖⴗⴘⴙⴚⴛⴜⴝⴞⴟⴠⴡⴢⴣⴤⴥꙁꙃꙅꙇꙉꙋꙍꙏꙑꙓꙕꙗꙙꙛꙝꙟꙣꙥꙧꙩꙫꙭꚁꚃꚅꚇꚉꚋꚍꚏꚑꚓꚕꚗꜣꜥꜧꜩꜫꜭꜯꜳꜵꜷꜹꜻꜽꜿꝁꝃꝅꝇꝉꝋꝍꝏꝑꝓꝕꝗꝙꝛꝝꝟꝡꝣꝥꝧꝩꝫꝭꝯꝺꝼꝿꞁꞃꞅꞇꞌ';
43	const ALPHABET_UPPER = 'AÀÁÂÃÄÅĀĂĄǍǞǠǺȀȂȦḀẠẢẤẦẨẪẬẮẰẲẴẶⒶAÆǢǼBḂḄḆⒷBɃƁƂCÇĆĈĊČḈⅭⒸCƇDĎḊḌḎḐḒⅮⒹDDŽDZĐƉƊƋÐEÈÉÊËĒĔĖĘĚȄȆȨḔḖḘḚḜẸẺẼẾỀỂỄỆⒺEƎƏƐFḞⒻFƑGĜĞĠĢǦǴḠⒼGǤƓƔƢHĤȞḢḤḦḨḪⒽHǶĦIÌÍÎÏĨĪĬĮǏȈȊḬḮỈỊⅠⒾIⅡⅢIJⅣⅨƗƖJĴⒿJKĶǨḰḲḴⓀKƘLĹĻĽḶḸḺḼⅬⓁLĿLJŁȽMḾṀṂⅯⓂMNÑŃŅŇǸṄṆṈṊⓃNNJƝȠŊOÒÓÔÕÖŌŎŐƠǑǪǬȌȎȪȬȮȰṌṎṐṒỌỎỐỒỔỖỘỚỜỞỠỢⓄOŒØǾƆƟȢPṔṖⓅPƤQⓆQRŔŖŘȐȒṘṚṜṞⓇRƦSŚŜŞŠȘṠṢṤṦṨⓈSƩTŢŤȚṪṬṮṰⓉTŦƬƮUÙÚÛÜŨŪŬŮŰŲƯǓǕǗǙǛȔȖṲṴṶṸṺỤỦỨỪỬỮỰⓊUɄƜƱVṼṾⅤⓋVⅥⅦⅧƲɅWŴẀẂẄẆẈⓌWXẊẌⅩⓍXⅪⅫYÝŸŶȲẎỲỴỶỸⓎYƳZŹŻŽẐẒẔⓏZƵȤǮƷƸȜÞǷƧƼƄΆΑἈἉἊἋἌἍἎἏᾺΆᾈᾉᾊᾋᾌᾍᾎᾏᾸᾹᾼΒΓΔΈΕἘἙἚἛἜἝῈΈϜϚΖΉΗἨἩἪἫἬἭἮἯῊΉᾘᾙᾚᾛᾜᾝᾞᾟῌΘΪἸἹἺἻἼἽἾἿῚΊῘῙΚϏΛΜΝΞΟΌὈὉὊὋὌὍῸΌΠϞϘΡῬΣϹΤΥΫΎὙὛὝὟῪΎῨῩΦΧΨΩΏὨὩὪὫὬὭὮὯῺΏᾨᾩᾪᾫᾬᾭᾮᾯῼϠϷϺϢϤϦϨϪϬϮАӐӒӘӚӔБВГҐҒҔДԀЂԂЃҘЕЀЁӖЄЖӁӜҖЗԄӞЅӠԆИЍӢҊӤІЇЙЈКҚӃҠҞҜЛӅЉԈМӍНӉҢӇҤЊԊОӦӨӪПҦҀРҎСԌҪТԎҬЋЌУӮЎӰӲҮҰѸФХҲҺѠѾѼѺЦҴЧӴҶӋҸҼҾЏШЩЪЫӸЬҌѢЭӬЮЯѤѦѪѨѬѮѰѲѴѶҨԱԲԳԴԵԶԷԸԹԺԻԼԽԾԿՀՁՂՃՄՅՆՇՈՉՊՋՌՍՎՏՐՑՒՓՔՕՖȻɁɆɈɊɌɎͰͲͶϽϾϿӀӶӺӼӾԐԒԔԖԘԚԜԞԠԢԤꝽⱣỺỼỾℲↃⰀⰁⰂⰃⰄⰅⰆⰇⰈⰉⰊⰋⰌⰍⰎⰏⰐⰑⰒⰓⰔⰕⰖⰗⰘⰙⰚⰛⰜⰝⰞⰟⰠⰡⰢⰣⰤⰥⰦⰧⰨⰩⰪⰫⰬⰭⰮⱠⱧⱩⱫⱲⱵⲀⲂⲄⲆⲈⲊⲌⲎⲐⲒⲔⲖⲘⲚⲜⲞⲠⲢⲤⲦⲨⲪⲬⲮⲰⲲⲴⲶⲸⲺⲼⲾⳀⳂⳄⳆⳈⳊⳌⳎⳐⳒⳔⳖⳘⳚⳜⳞⳠⳢⳫⳭႠႡႢႣႤႥႦႧႨႩႪႫႬႭႮႯႰႱႲႳႴႵႶႷႸႹႺႻႼႽႾႿჀჁჂჃჄჅꙀꙂꙄꙆꙈꙊꙌꙎꙐꙒꙔꙖꙘꙚꙜꙞꙢꙤꙦꙨꙪꙬꚀꚂꚄꚆꚈꚊꚌꚎꚐꚒꚔꚖꜢꜤꜦꜨꜪꜬꜮꜲꜴꜶꜸꜺꜼꜾꝀꝂꝄꝆꝈꝊꝌꝎꝐꝒꝔꝖꝘꝚꝜꝞꝠꝢꝤꝦꝨꝪꝬꝮꝹꝻꝾꞀꞂꞄꞆꞋ';
44
45	// Alphabet for the currently selected locale
46	private static $alphabet_lower = 'abcdefghijklmnopqrstuvwxyz';
47	private static $alphabet_upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
48
49	// Lookup table to convert unicode code-points into scripts.
50	// See https://en.wikipedia.org/wiki/Unicode_block
51	// Note: we only need details for scripts of languages into which webtrees is translated.
52	private static $scripts = array(
53		array('Latn', 0x0041, 0x005A), // a-z
54		array('Latn', 0x0061, 0x007A), // A-Z
55		array('Latn', 0x0100, 0x02AF),
56		array('Grek', 0x0370, 0x03FF),
57		array('Cyrl', 0x0400, 0x052F),
58		array('Hebr', 0x0590, 0x05FF),
59		array('Arab', 0x0600, 0x06FF),
60		array('Arab', 0x0750, 0x077F),
61		array('Arab', 0x08A0, 0x08FF),
62		array('Deva', 0x0900, 0x097F),
63		array('Taml', 0x0B80, 0x0BFF),
64		array('Sinh', 0x0D80, 0x0DFF),
65		array('Thai', 0x0E00, 0x0E7F),
66		array('Geor', 0x10A0, 0x10FF),
67		array('Grek', 0x1F00, 0x1FFF),
68		array('Deva', 0xA8E0, 0xA8FF),
69		array('Hans', 0x3000, 0x303F), // Mixed CJK, not just Hans
70		array('Hans', 0x3400, 0xFAFF), // Mixed CJK, not just Hans
71		array('Hans', 0x20000, 0x2FA1F), // Mixed CJK, not just Hans
72	);
73
74	// Characters that are displayed in mirror form in RTL text.
75	private static $mirror_characters = array(
76		'(' => ')',
77		')' => '(',
78		'[' => ']',
79		']' => '[',
80		'{' => '}',
81		'}' => '{',
82		'<' => '>',
83		'>' => '<',
84		'‹' => '›',
85		'›' => '‹',
86		'«' => '»',
87		'»' => '«',
88		'﴾' => '﴿',
89		'﴿' => '﴾',
90		'“' => '”',
91		'”' => '“',
92		'‘' => '’',
93		'’' => '‘',
94	);
95
96	/** @var string Punctuation used to separate list items, typically a comma */
97	public static $list_separator;
98
99	/**
100	 * Initialise the translation adapter with a locale setting.
101	 *
102	 * @param string|null $code Use this locale/language code, or choose one automatically
103	 *
104	 * @return string $string
105	 */
106	public static function init($code = null) {
107		global $WT_SESSION, $WT_TREE;
108
109		if ($code !== null) {
110			// Create the specified locale
111			self::$locale = Locale::create($code);
112		} else {
113			// Negotiate a locale, but if we can't then use a failsafe
114			self::$locale = new LocaleEnUs;
115			if (Filter::get('lang')) {
116				// A request in the URL
117				try {
118					$locale = Locale::create(Filter::get('lang'));
119					if (file_exists(WT_ROOT . 'language/' . $locale->languageTag() . '.mo')) {
120						self::$locale = $locale;
121					}
122				} catch (\Exception $ex) {
123				}
124			} elseif ($WT_SESSION->locale) {
125				// Previously used
126				self::$locale = Locale::create($WT_SESSION->locale);
127			} else {
128				// Browser negotiation
129				$default_locale = new LocaleEnUs;
130				try {
131					if ($WT_TREE) {
132						$default_locale = Locale::create($WT_TREE->getPreference('LANGUAGE'));
133					}
134				} catch (\Exception $ex) {
135				}
136				self::$locale = Locale::httpAcceptLanguage($_SESSION, self::installedLocales(), $default_locale);
137			}
138		}
139
140		File::mkdir(WT_DATA_DIR . 'cache');
141		$cache_file = WT_DATA_DIR . 'cache/language-' . self::$locale->languageTag() . '-cache.php';
142		if (file_exists($cache_file)) {
143			$filemtime = filemtime($cache_file);
144		} else {
145			$filemtime = 0;
146		}
147
148		// Load the translation file(s)
149		// Note that glob() returns false instead of an empty array when open_basedir_restriction
150		// is in force and no files are found.  See PHP bug #47358.
151		$translation_files = array_merge(
152			array(WT_ROOT . 'language/' . self::$locale->languageTag() . '.mo'),
153			glob(WT_MODULES_DIR . '*/language/' . self::$locale->languageTag() . '.{csv,php,mo}', GLOB_BRACE) ?: array(),
154			glob(WT_DATA_DIR . 'language/' . self::$locale->languageTag() . '.{csv,php,mo}', GLOB_BRACE) ?: array()
155		);
156
157		// Rebuild files after 2 hours
158		$rebuild_cache = time() > $filemtime + 7200;
159		// Rebuild files if any translation file has been updated
160		foreach ($translation_files as $translation_file) {
161			if (filemtime($translation_file) > $filemtime) {
162				$rebuild_cache = true;
163				break;
164			}
165		}
166
167		if ($rebuild_cache) {
168			$translations = array();
169			foreach ($translation_files as $translation_file) {
170				$translation = new Translation($translation_file);
171				$translations = array_merge($translations, $translation->asArray());
172			}
173			file_put_contents($cache_file, '<' . '?php return ' . var_export($translations, true) . ';');
174		} else {
175			$translations = include $cache_file;
176		}
177
178		// Create a translator
179		self::$translator = new Translator($translations, self::$locale->pluralRule());
180
181		// Extract language settings from the translation file
182		global $DATE_FORMAT; // I18N: This is the format string for full dates.  See http://php.net/date for codes
183		$DATE_FORMAT = self::$translator->translate('%j %F %Y');
184
185		global $TIME_FORMAT; // I18N: This is the format string for the time-of-day.  See http://php.net/date for codes
186		$TIME_FORMAT = self::$translator->translate('%H:%i:%s');
187
188		// Alphabetic sorting sequence (upper-case letters), used by webtrees to sort strings
189		list(, self::$alphabet_upper) = explode('=', self::$translator->translate('ALPHABET_upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ'));
190		// Alphabetic sorting sequence (lower-case letters), used by webtrees to sort strings
191		list(, self::$alphabet_lower) = explode('=', self::$translator->translate('ALPHABET_lower=abcdefghijklmnopqrstuvwxyz'));
192
193		global $WEEK_START;
194		$WEEK_START = self::$locale->territory()->firstDay();
195
196		self::$list_separator = /* I18N: This punctuation is used to separate lists of items */ self::translate(', ');
197
198		return self::$locale->languageTag();
199	}
200
201	/**
202	 * The prefered locales for this site, or a default list if no preference.
203	 *
204	 * @return LocaleInterface[]
205	 */
206	public static function activeLocales() {
207		$code_list = Site::getPreference('LANGUAGES');
208
209		if ($code_list) {
210			$codes = explode(',', $code_list);
211		} else {
212			$codes = array(
213				'ar', 'bg', 'bs', 'ca', 'cs', 'da', 'de', 'el', 'en-GB', 'en-US', 'es',
214				'et', 'fi', 'fr', 'he', 'hr', 'hu', 'is', 'it', 'ka', 'lt', 'mr', 'nb',
215				'nl', 'nn', 'pl', 'pt', 'ru', 'sk', 'sv', 'tr', 'uk', 'vi', 'zh-Hans',
216			);
217		}
218
219		$locales = array();
220		foreach ($codes as $code) {
221			if (file_exists(WT_ROOT . 'language/' . $code . '.mo')) {
222				try {
223					$locales[] = Locale::create($code);
224				} catch (\Exception $ex) {
225					// No such locale exists?
226				}
227			}
228		}
229		usort($locales, '\Fisharebest\Localization\Locale::compare');
230
231		return $locales;
232	}
233
234	/**
235	 * Which MySQL collation should be used for this locale?
236	 *
237	 * @return string
238	 */
239	public static function collation() {
240		$collation = self::$locale->collation();
241		switch ($collation) {
242		case 'croatian_ci':
243		case 'german2_ci':
244		case 'vietnamese_ci':
245			// Only available in MySQL 5.6
246			return 'utf8_unicode_ci';
247		default:
248			return 'utf8_' . $collation;
249		}
250	}
251
252	/**
253	 * All locales for which a translation file exists.
254	 *
255	 * @return LocaleInterface[]
256	 */
257	public static function installedLocales() {
258		$locales = array();
259		foreach (glob(WT_ROOT . 'language/*.mo') as $file) {
260			try {
261				$locales[] = Locale::create(basename($file, '.mo'));
262			} catch (\Exception $ex) {
263				// Not a recognised locale
264			}
265		}
266		usort($locales, '\Fisharebest\Localization\Locale::compare');
267
268		return $locales;
269	}
270
271	/**
272	 * Generate i18n markup for the <html> tag, e.g. lang="ar" dir="rtl"
273	 *
274	 * @return string
275	 */
276	public static function htmlAttributes() {
277		return self::$locale->htmlAttributes();
278	}
279
280	/**
281	 * Translate a number into the local representation.
282	 *
283	 * e.g. 12345.67 becomes
284	 * en: 12,345.67
285	 * fr: 12 345,67
286	 * de: 12.345,67
287	 *
288	 * @param float   $n
289	 * @param integer $precision
290	 *
291	 * @return string
292	 */
293	public static function number($n, $precision = 0) {
294		return self::$locale->number(round($n, $precision));
295	}
296
297	/**
298	 * Convert the digits 0-9 into the local script
299	 *
300	 * Used for years, etc., where we do not want thousands-separators, decimals, etc.
301	 *
302	 * @param integer $n
303	 *
304	 * @return string
305	 */
306	public static function digits($n) {
307		return self::$locale->digits($n);
308	}
309
310	/**
311	 * Translate a fraction into a percentage.
312	 *
313	 * e.g. 0.123 becomes
314	 * en: 12.3%
315	 * fr: 12,3 %
316	 * de: 12,3%
317	 *
318	 * @param float   $n
319	 * @param integer $precision
320	 *
321	 * @return string
322	 */
323	public static function percentage($n, $precision = 0) {
324		return self::$locale->percent(round($n, $precision + 2));
325	}
326
327	/**
328	 * What is the direction of the current locale
329	 *
330	 * @return string "ltr" or "rtl"
331	 */
332	public static function direction() {
333		return self::$locale->direction();
334	}
335
336	/**
337	 * Translate a string, and then substitute placeholders
338	 *
339	 * echo I18N::translate('Hello World!');
340	 * echo I18N::translate('The %s sat on the mat', 'cat');
341	 *
342	 * @return string
343	 */
344	public static function translate(/* var_args */) {
345		$args = func_get_args();
346		$args[0] = self::$translator->translate($args[0]);
347
348		return call_user_func_array('sprintf', $args);
349	}
350
351	/**
352	 * Context sensitive version of translate.
353	 *
354	 * echo I18N::translate_c('NOMINATIVE', 'January');
355	 * echo I18N::translate_c('GENITIVE',   'January');
356	 *
357	 * @return string
358	 */
359	public static function translateContext(/* var_args */) {
360		$args = func_get_args();
361		$args[0] = self::$translator->translateContext($args[0], $args[1]);
362		unset($args[1]);
363
364		return call_user_func_array('sprintf', $args);
365	}
366
367	/**
368	 * Similar to translate, but do perform "no operation" on it.
369	 *
370	 * This is necessary to fetch a format string (containing % characters) without
371	 * performing sustitution of arguments.
372	 *
373	 * @param string $string
374	 *
375	 * @return string
376	 */
377	public static function noop($string) {
378		return self::$translator->translate($string);
379	}
380
381	/**
382	 * Translate a plural string
383	 *
384	 * echo self::plural('There is an error', 'There are errors', $num_errors);
385	 * echo self::plural('There is one error', 'There are %s errors', $num_errors);
386	 * echo self::plural('There is %1$s %2$s cat', 'There are %1$s %2$s cats', $num, $num, $colour);
387	 *
388	 * @return string
389	 */
390	public static function plural(/* var_args */) {
391		$args = func_get_args();
392		$args[0] = self::$translator->translatePlural($args[0], $args[1], $args[2]);
393		unset($args[1], $args[2]);
394
395		return call_user_func_array('sprintf', $args);
396	}
397
398	/**
399	 * Convert a GEDCOM age string into translated_text
400	 *
401	 * NB: The import function will have normalised this, so we don't need
402	 * to worry about badly formatted strings
403	 * NOTE: this function is not yet complete - eventually it will replace get_age_at_event()
404	 *
405	 * @param $string
406	 *
407	 * @return string
408	 */
409	public static function gedcomAge($string) {
410		switch ($string) {
411		case 'STILLBORN':
412			// I18N: Description of an individual’s age at an event.  For example, Died 14 Jan 1900 (stillborn)
413			return self::translate('(stillborn)');
414		case 'INFANT':
415			// I18N: Description of an individual’s age at an event.  For example, Died 14 Jan 1900 (in infancy)
416			return self::translate('(in infancy)');
417		case 'CHILD':
418			// I18N: Description of an individual’s age at an event.  For example, Died 14 Jan 1900 (in childhood)
419			return self::translate('(in childhood)');
420		}
421		$age = array();
422		if (preg_match('/(\d+)y/', $string, $match)) {
423			// I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
424			$years = $match[1];
425			$age[] = self::plural('%s year', '%s years', $years, self::number($years));
426		} else {
427			$years = -1;
428		}
429		if (preg_match('/(\d+)m/', $string, $match)) {
430			// I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
431			$age[] = self::plural('%s month', '%s months', $match[1], self::number($match[1]));
432		}
433		if (preg_match('/(\d+)w/', $string, $match)) {
434			// I18N: Part of an age string. e.g. 7 weeks and 3 days
435			$age[] = self::plural('%s week', '%s weeks', $match[1], self::number($match[1]));
436		}
437		if (preg_match('/(\d+)d/', $string, $match)) {
438			// I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
439			$age[] = self::plural('%s day', '%s days', $match[1], self::number($match[1]));
440		}
441		// If an age is just a number of years, only show the number
442		if (count($age) === 1 && $years >= 0) {
443			$age = $years;
444		}
445		if ($age) {
446			if (!substr_compare($string, '<', 0, 1)) {
447				// I18N: Description of an individual’s age at an event.  For example, Died 14 Jan 1900 (aged less than 21 years)
448				return self::translate('(aged less than %s)', $age);
449			} elseif (!substr_compare($string, '>', 0, 1)) {
450				// I18N: Description of an individual’s age at an event.  For example, Died 14 Jan 1900 (aged more than 21 years)
451				return self::translate('(aged more than %s)', $age);
452			} else {
453				// I18N: Description of an individual’s age at an event.  For example, Died 14 Jan 1900 (aged 43 years)
454				return self::translate('(aged %s)', $age);
455			}
456		} else {
457			// Not a valid string?
458			return self::translate('(aged %s)', $string);
459		}
460	}
461
462	/**
463	 * Convert a number of seconds into a relative time.  For example, 630 => "10 hours, 30 minutes ago"
464	 *
465	 * @param integer $seconds
466	 *
467	 * @return string
468	 */
469	public static function timeAgo($seconds) {
470		$minute = 60;
471		$hour   = 60 * $minute;
472		$day    = 24 * $hour;
473		$month  = 30 * $day;
474		$year   = 365 * $day;
475
476		if ($seconds > $year) {
477			$years = (int) ($seconds / $year);
478			return self::plural('%s year ago', '%s years ago', $years, self::number($years));
479		} elseif ($seconds > $month) {
480			$months = (int) ($seconds / $month);
481			return self::plural('%s month ago', '%s months ago', $months, self::number($months));
482		} elseif ($seconds > $day) {
483			$days = (int) ($seconds / $day);
484			return self::plural('%s day ago', '%s days ago', $days, self::number($days));
485		} elseif ($seconds > $hour) {
486			$hours = (int) ($seconds / $hour);
487			return self::plural('%s hour ago', '%s hours ago', $hours, self::number($hours));
488		} elseif ($seconds > $minute) {
489			$minutes = (int) ($seconds / $minute);
490			return self::plural('%s minute ago', '%s minutes ago', $minutes, self::number($minutes));
491		} else {
492			return self::plural('%s second ago', '%s seconds ago', $seconds, self::number($seconds));
493		}
494	}
495
496	/**
497	 * Return the endonym for a given language - as per http://cldr.unicode.org/
498	 *
499	 * @param string $locale
500	 *
501	 * @return string
502	 */
503	public static function languageName($locale) {
504		return Locale::create($locale)->endonym();
505	}
506
507	/**
508	 * Return the script used by a given language
509	 *
510	 * @param string $locale
511	 *
512	 * @return string
513	 */
514	public static function languageScript($locale) {
515		return Locale::create($locale)->script()->code();
516	}
517
518	/**
519	 * Return the current locale object
520	 *
521	 * @return LocaleInterface
522	 */
523	public static function locale() {
524		return self::$locale;
525	}
526
527	/**
528	 * Identify the script used for a piece of text
529	 *
530	 * @param $string
531	 *
532	 * @return string
533	 */
534	public static function textScript($string) {
535		$string = strip_tags($string); // otherwise HTML tags show up as latin
536		$string = html_entity_decode($string, ENT_QUOTES, 'UTF-8'); // otherwise HTML entities show up as latin
537		$string = str_replace(array('@N.N.', '@P.N.'), '', $string); // otherwise unknown names show up as latin
538		$pos = 0;
539		$strlen = strlen($string);
540		while ($pos < $strlen) {
541			// get the Unicode Code Point for the character at position $pos
542			$byte1 = ord($string[$pos]);
543			if ($byte1 < 0x80) {
544				$code_point = $byte1;
545				$chrlen = 1;
546			} elseif ($byte1 < 0xC0) {
547				// Invalid continuation character
548				return 'Latn';
549			} elseif ($byte1 < 0xE0) {
550				$code_point = (($byte1 & 0x1F) << 6) + (ord($string[$pos + 1]) & 0x3F);
551				$chrlen = 2;
552			} elseif ($byte1 < 0xF0) {
553				$code_point = (($byte1 & 0x0F) << 12) + ((ord($string[$pos + 1]) & 0x3F) << 6) + (ord($string[$pos + 2]) & 0x3F);
554				$chrlen = 3;
555			} elseif ($byte1 < 0xF8) {
556				$code_point = (($byte1 & 0x07) << 24) + ((ord($string[$pos + 1]) & 0x3F) << 12) + ((ord($string[$pos + 2]) & 0x3F) << 6) + (ord($string[$pos + 3]) & 0x3F);
557				$chrlen = 3;
558 			} else {
559				// Invalid UTF
560				return 'Latn';
561			}
562
563			foreach (self::$scripts as $range) {
564				if ($code_point >= $range[1] && $code_point <= $range[2]) {
565					return $range[0];
566				}
567			}
568			// Not a recognised script.  Maybe punctuation, spacing, etc.  Keep looking.
569			$pos += $chrlen;
570		}
571
572		return 'Latn';
573	}
574
575	/**
576	 * Return the direction (ltr or rtl) for a given script
577	 *
578	 * The PHP/intl library does not provde this information, so we need
579	 * our own lookup table.
580	 *
581	 * @param string $script
582	 *
583	 * @return string
584	 */
585	public static function scriptDirection($script) {
586		switch ($script) {
587		case 'Arab':
588		case 'Hebr':
589		case 'Mong':
590		case 'Thaa':
591			return 'rtl';
592		default:
593			return 'ltr';
594		}
595	}
596
597	/**
598	 * UTF8 version of PHP::strtoupper()
599	 *
600	 * Convert a string to upper case, using the rules from the current locale
601	 *
602	 * @param string $string
603	 *
604	 * @return string
605	 */
606	public static function strtoupper($string) {
607		if (self::$locale->language()->code() === 'tr' || self::$locale->language()->code() === 'az') {
608			return TurkishUtf8::strtoupper($string);
609		} else {
610			return mb_strtoupper($string);
611		}
612	}
613
614	/**
615	 * UTF8 version of PHP::strtolower()
616	 *
617	 * Convert a string to lower case, using the rules from the current locale
618	 *
619	 * @param string $string
620	 *
621	 * @return string
622	 */
623	public static function strtolower($string) {
624		if (self::$locale->language()->code() === 'tr' || self::$locale->language()->code() === 'az') {
625			return TurkishUtf8::strtolower($string);
626		} else {
627			return mb_strtolower($string);
628		}
629	}
630
631	/**
632	 * UTF8 version of PHP::strcasecmp()
633	 *
634	 * Perform a case-insensitive comparison of two strings, using rules from the current locale
635	 *
636	 * @param string $string1
637	 * @param string $string2
638	 *
639	 * @return integer
640	 */
641	public static function strcasecmp($string1, $string2) {
642		$strpos1 = 0;
643		$strpos2 = 0;
644		$strlen1 = strlen($string1);
645		$strlen2 = strlen($string2);
646		while ($strpos1 < $strlen1 && $strpos2 < $strlen2) {
647			$byte1 = ord($string1[$strpos1]);
648			$byte2 = ord($string2[$strpos2]);
649			if (($byte1 & 0xE0) === 0xC0) {
650				$chr1 = $string1[$strpos1++] . $string1[$strpos1++];
651			} elseif (($byte1 & 0xF0) === 0xE0) {
652				$chr1 = $string1[$strpos1++] . $string1[$strpos1++] . $string1[$strpos1++];
653			} else {
654				$chr1 = $string1[$strpos1++];
655			}
656			if (($byte2 & 0xE0) === 0xC0) {
657				$chr2 = $string2[$strpos2++] . $string2[$strpos2++];
658			} elseif (($byte2 & 0xF0) === 0xE0) {
659				$chr2 = $string2[$strpos2++] . $string2[$strpos2++] . $string2[$strpos2++];
660			} else {
661				$chr2 = $string2[$strpos2++];
662			}
663			if ($chr1 === $chr2) {
664				continue;
665			}
666			// Try the local alphabet first
667			$offset1 = strpos(self::$alphabet_lower, $chr1);
668			if ($offset1 === false) {
669				$offset1 = strpos(self::$alphabet_upper, $chr1);
670			}
671			$offset2 = strpos(self::$alphabet_lower, $chr2);
672			if ($offset2 === false) {
673				$offset2 = strpos(self::$alphabet_upper, $chr2);
674			}
675			if ($offset1 !== false && $offset2 !== false) {
676				if ($offset1 === $offset2) {
677					continue;
678				} else {
679					return $offset1 - $offset2;
680				}
681			}
682			// Try the global alphabet next
683			$offset1 = strpos(self::ALPHABET_LOWER, $chr1);
684			if ($offset1 === false) {
685				$offset1 = strpos(self::ALPHABET_UPPER, $chr1);
686			}
687			$offset2 = strpos(self::ALPHABET_LOWER, $chr2);
688			if ($offset2 === false) {
689				$offset2 = strpos(self::ALPHABET_UPPER, $chr2);
690			}
691			if ($offset1 !== false && $offset2 !== false) {
692				if ($offset1 === $offset2) {
693					continue;
694				} else {
695					return $offset1 - $offset2;
696				}
697			}
698			// Just compare by unicode order
699			return strcmp($chr1, $chr2);
700		}
701		// Shortest string comes first.
702		return ($strlen1 - $strpos1) - ($strlen2 - $strpos2);
703	}
704
705	/**
706	 * UTF8 version of PHP::strrev()
707	 *
708	 * Reverse RTL text for third-party libraries such as GD2 and googlechart.
709	 *
710	 * These do not support UTF8 text direction, so we must mimic it for them.
711	 *
712	 * Numbers are always rendered LTR, even in RTL text.
713	 * The visual direction of characters such as parentheses should be reversed.
714	 *
715	 * @param string $text Text to be reversed
716	 *
717	 * @return string
718	 */
719	public static function reverseText($text) {
720		// Remove HTML markup - we can't display it and it is LTR.
721		$text = Filter::unescapeHtml($text);
722
723		// LTR text doesn't need reversing
724		if (self::scriptDirection(self::textScript($text)) === 'ltr') {
725			return $text;
726		}
727
728		// Mirrored characters
729		$text = strtr($text, self::$mirror_characters);
730
731		$reversed = '';
732		$digits = '';
733		while ($text != '') {
734			$letter = mb_substr($text, 0, 1);
735			$text = mb_substr($text, 1);
736			if (strpos(self::DIGITS, $letter) !== false) {
737				$digits .= $letter;
738			} else {
739				$reversed = $letter . $digits . $reversed;
740				$digits = '';
741			}
742		}
743
744		return $digits . $reversed;
745	}
746
747	/**
748	 * Generate consistent I18N for datatables.js
749	 *
750	 * @param array|null $lengths An optional array of page lengths
751	 *
752	 * @return string
753	 */
754	public static function datatablesI18N(array $lengths = null) {
755		if ($lengths === null) {
756			$lengths = array(10, 20, 30, 50, 100, -1);
757		}
758
759		$length_menu = '';
760		foreach ($lengths as $length) {
761			$length_menu .=
762				'<option value="' . $length . '">' .
763				($length === -1 ? /* I18N: listbox option, e.g. “10,25,50,100,all” */ self::translate('All') : self::number($length)) .
764				'</option>';
765		}
766		$length_menu = '<select>' . $length_menu . '</select>';
767		$length_menu = /* I18N: Display %s [records per page], %s is a placeholder for listbox containing numeric options */ self::translate('Display %s', $length_menu);
768
769		$digits = self::$locale->digits('0123456789');
770		if ($digits === '0123456789') {
771			$callback = '';
772		} else {
773			$callback = ',
774				"infoCallback": function(oSettings, iStart, iEnd, iMax, iTotal, sPre) {
775					return sPre
776						.replace(/0/g, "' . mb_substr($digits, 0, 1) . '")
777						.replace(/1/g, "' . mb_substr($digits, 1, 1) . '")
778						.replace(/2/g, "' . mb_substr($digits, 2, 1) . '")
779						.replace(/3/g, "' . mb_substr($digits, 3, 1) . '")
780						.replace(/4/g, "' . mb_substr($digits, 4, 1) . '")
781						.replace(/5/g, "' . mb_substr($digits, 5, 1) . '")
782						.replace(/6/g, "' . mb_substr($digits, 6, 1) . '")
783						.replace(/7/g, "' . mb_substr($digits, 7, 1) . '")
784						.replace(/8/g, "' . mb_substr($digits, 8, 1) . '")
785						.replace(/9/g, "' . mb_substr($digits, 9, 1) . '");
786				},
787				"formatNumber": function(iIn) {
788					return String(iIn)
789						.replace(/0/g, "' . mb_substr($digits, 0, 1) . '")
790						.replace(/1/g, "' . mb_substr($digits, 1, 1) . '")
791						.replace(/2/g, "' . mb_substr($digits, 2, 1) . '")
792						.replace(/3/g, "' . mb_substr($digits, 3, 1) . '")
793						.replace(/4/g, "' . mb_substr($digits, 4, 1) . '")
794						.replace(/5/g, "' . mb_substr($digits, 5, 1) . '")
795						.replace(/6/g, "' . mb_substr($digits, 6, 1) . '")
796						.replace(/7/g, "' . mb_substr($digits, 7, 1) . '")
797						.replace(/8/g, "' . mb_substr($digits, 8, 1) . '")
798						.replace(/9/g, "' . mb_substr($digits, 9, 1) . '");
799				}
800			';
801		}
802
803		return
804			'"language": {' .
805			' "paginate": {' .
806			'  "first":    "' . /* I18N: button label, first page    */ self::translate('first') . '",' .
807			'  "last":     "' . /* I18N: button label, last page     */ self::translate('last') . '",' .
808			'  "next":     "' . /* I18N: button label, next page     */ self::translate('next') . '",' .
809			'  "previous": "' . /* I18N: button label, previous page */ self::translate('previous') . '"' .
810			' },' .
811			' "emptyTable":     "' . self::translate('No records to display') . '",' .
812			' "info":           "' . /* I18N: %s are placeholders for numbers */ self::translate('Showing %1$s to %2$s of %3$s', '_START_', '_END_', '_TOTAL_') . '",' .
813			' "infoEmpty":      "' . self::translate('Showing %1$s to %2$s of %3$s', 0, 0, 0) . '",' .
814			' "infoFiltered":   "' . /* I18N: %s is a placeholder for a number */ self::translate('(filtered from %s total entries)', '_MAX_') . '",' .
815			' "infoPostfix":    "",' .
816			' "lengthMenu":     "' . Filter::escapeJs($length_menu) . '",' .
817			' "loadingRecords": "' . self::translate('Loading…') . '",' .
818			' "processing":     "' . self::translate('Loading…') . '",' .
819			' "search":         "' . self::translate('Filter') . '",' .
820			' "url":            "",' .
821			' "zeroRecords":    "' . self::translate('No records to display') . '"' .
822			'}' .
823			$callback;
824	}
825}
826