xref: /webtrees/app/I18N.php (revision 3cf92ae205660ec36316541b9e23f2ecbf0af8bb)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2015 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees;
17
18use Fisharebest\ExtCalendar\ArabicCalendar;
19use Fisharebest\ExtCalendar\CalendarInterface;
20use Fisharebest\ExtCalendar\GregorianCalendar;
21use Fisharebest\ExtCalendar\JewishCalendar;
22use Fisharebest\ExtCalendar\PersianCalendar;
23use Fisharebest\Localization\Locale;
24use Fisharebest\Localization\Locale\LocaleEnUs;
25use Fisharebest\Localization\Locale\LocaleInterface;
26use Fisharebest\Localization\Translation;
27use Fisharebest\Localization\Translator;
28use Patchwork\TurkishUtf8;
29
30/**
31 * Internationalization (i18n) and localization (l10n).
32 */
33class I18N {
34	/** @var LocaleInterface The current locale (e.g. LocaleEnGb) */
35	private static $locale;
36
37	/** @var Translator An object that performs translation*/
38	private static $translator;
39
40	// Digits are always rendered LTR, even in RTL text.
41	const DIGITS = '0123456789٠١٢٣٤٥٦٧٨٩۰۱۲۳۴۵۶۷۸۹';
42
43	// Reversable character conversions from the UNICODE 5.1 database.
44	// It excludes ambiguous (turkish dotless i) and mixed-case (Dz) characters.
45	// The characters should be arranged in default unicode-collation order.
46	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ƶȥǯʒƹȝþƿƨƽƅάαἀἁἂἃἄἅἆἇὰάᾀᾁᾂᾃᾄᾅᾆᾇᾰᾱᾳβγδέεἐἑἒἓἔἕὲέϝϛζήηἠἡἢἣἤἥἦἧὴήᾐᾑᾒᾓᾔᾕᾖᾗῃθϊἰἱἲἳἴἵἶἷὶίῐῑκϗλμνξοόὀὁὂὃὄὅὸόπϟϙρῥσϲτυϋύὑὓὕὗὺύῠῡφχψωώὠὡὢὣὤὥὦὧὼώᾠᾡᾢᾣᾤᾥᾦᾧῳϡϸϻϣϥϧϩϫϭϯаӑӓәӛӕбвгґғҕдԁђԃѓҙеѐёӗєжӂӝҗзԅӟѕӡԇиѝӣҋӥіїйјкқӄҡҟҝлӆљԉмӎнӊңӈҥњԋоӧөӫпҧҁрҏсԍҫтԏҭћќуӯўӱӳүұѹфхҳһѡѿѽѻцҵчӵҷӌҹҽҿџшщъыӹьҍѣэӭюяѥѧѫѩѭѯѱѳѵѷҩաբգդեզէըթժիլխծկհձղճմյնշոչպջռսվտրցւփքօֆȼɂɇɉɋɍɏͱͳͷͻͼͽӏӷӻӽӿԑԓԕԗԙԛԝԟԡԣԥᵹᵽỻỽỿⅎↄⰰⰱⰲⰳⰴⰵⰶⰷⰸⰹⰺⰻⰼⰽⰾⰿⱀⱁⱂⱃⱄⱅⱆⱇⱈⱉⱊⱋⱌⱍⱎⱏⱐⱑⱒⱓⱔⱕⱖⱗⱘⱙⱚⱛⱜⱝⱞⱡⱨⱪⱬⱳⱶⲁⲃⲅⲇⲉⲋⲍⲏⲑⲓⲕⲗⲙⲛⲝⲟⲡⲣⲥⲧⲩⲫⲭⲯⲱⲳⲵⲷⲹⲻⲽⲿⳁⳃⳅⳇⳉⳋⳍⳏⳑⳓⳕⳗⳙⳛⳝⳟⳡⳣⳬⳮⴀⴁⴂⴃⴄⴅⴆⴇⴈⴉⴊⴋⴌⴍⴎⴏⴐⴑⴒⴓⴔⴕⴖⴗⴘⴙⴚⴛⴜⴝⴞⴟⴠⴡⴢⴣⴤⴥꙁꙃꙅꙇꙉꙋꙍꙏꙑꙓꙕꙗꙙꙛꙝꙟꙣꙥꙧꙩꙫꙭꚁꚃꚅꚇꚉꚋꚍꚏꚑꚓꚕꚗꜣꜥꜧꜩꜫꜭꜯꜳꜵꜷꜹꜻꜽꜿꝁꝃꝅꝇꝉꝋꝍꝏꝑꝓꝕꝗꝙꝛꝝꝟꝡꝣꝥꝧꝩꝫꝭꝯꝺꝼꝿꞁꞃꞅꞇꞌ';
47	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ƵȤǮƷƸȜÞǷƧƼƄΆΑἈἉἊἋἌἍἎἏᾺΆᾈᾉᾊᾋᾌᾍᾎᾏᾸᾹᾼΒΓΔΈΕἘἙἚἛἜἝῈΈϜϚΖΉΗἨἩἪἫἬἭἮἯῊΉᾘᾙᾚᾛᾜᾝᾞᾟῌΘΪἸἹἺἻἼἽἾἿῚΊῘῙΚϏΛΜΝΞΟΌὈὉὊὋὌὍῸΌΠϞϘΡῬΣϹΤΥΫΎὙὛὝὟῪΎῨῩΦΧΨΩΏὨὩὪὫὬὭὮὯῺΏᾨᾩᾪᾫᾬᾭᾮᾯῼϠϷϺϢϤϦϨϪϬϮАӐӒӘӚӔБВГҐҒҔДԀЂԂЃҘЕЀЁӖЄЖӁӜҖЗԄӞЅӠԆИЍӢҊӤІЇЙЈКҚӃҠҞҜЛӅЉԈМӍНӉҢӇҤЊԊОӦӨӪПҦҀРҎСԌҪТԎҬЋЌУӮЎӰӲҮҰѸФХҲҺѠѾѼѺЦҴЧӴҶӋҸҼҾЏШЩЪЫӸЬҌѢЭӬЮЯѤѦѪѨѬѮѰѲѴѶҨԱԲԳԴԵԶԷԸԹԺԻԼԽԾԿՀՁՂՃՄՅՆՇՈՉՊՋՌՍՎՏՐՑՒՓՔՕՖȻɁɆɈɊɌɎͰͲͶϽϾϿӀӶӺӼӾԐԒԔԖԘԚԜԞԠԢԤꝽⱣỺỼỾℲↃⰀⰁⰂⰃⰄⰅⰆⰇⰈⰉⰊⰋⰌⰍⰎⰏⰐⰑⰒⰓⰔⰕⰖⰗⰘⰙⰚⰛⰜⰝⰞⰟⰠⰡⰢⰣⰤⰥⰦⰧⰨⰩⰪⰫⰬⰭⰮⱠⱧⱩⱫⱲⱵⲀⲂⲄⲆⲈⲊⲌⲎⲐⲒⲔⲖⲘⲚⲜⲞⲠⲢⲤⲦⲨⲪⲬⲮⲰⲲⲴⲶⲸⲺⲼⲾⳀⳂⳄⳆⳈⳊⳌⳎⳐⳒⳔⳖⳘⳚⳜⳞⳠⳢⳫⳭႠႡႢႣႤႥႦႧႨႩႪႫႬႭႮႯႰႱႲႳႴႵႶႷႸႹႺႻႼႽႾႿჀჁჂჃჄჅꙀꙂꙄꙆꙈꙊꙌꙎꙐꙒꙔꙖꙘꙚꙜꙞꙢꙤꙦꙨꙪꙬꚀꚂꚄꚆꚈꚊꚌꚎꚐꚒꚔꚖꜢꜤꜦꜨꜪꜬꜮꜲꜴꜶꜸꜺꜼꜾꝀꝂꝄꝆꝈꝊꝌꝎꝐꝒꝔꝖꝘꝚꝜꝞꝠꝢꝤꝦꝨꝪꝬꝮꝹꝻꝾꞀꞂꞄꞆꞋ';
48
49	/** @var string Alphabet, in lower case, for the current locale. */
50	private static $alphabet_lower = 'abcdefghijklmnopqrstuvwxyz';
51
52	/** @var string Alphabet, in upper case, for the current locale. */
53	private static $alphabet_upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
54
55	/** @var int[][] Character ranges used by each script. */
56	private static $scripts = array(
57		array('Latn', 0x0041, 0x005A), // a-z
58		array('Latn', 0x0061, 0x007A), // A-Z
59		array('Latn', 0x0100, 0x02AF),
60		array('Grek', 0x0370, 0x03FF),
61		array('Cyrl', 0x0400, 0x052F),
62		array('Hebr', 0x0590, 0x05FF),
63		array('Arab', 0x0600, 0x06FF),
64		array('Arab', 0x0750, 0x077F),
65		array('Arab', 0x08A0, 0x08FF),
66		array('Deva', 0x0900, 0x097F),
67		array('Taml', 0x0B80, 0x0BFF),
68		array('Sinh', 0x0D80, 0x0DFF),
69		array('Thai', 0x0E00, 0x0E7F),
70		array('Geor', 0x10A0, 0x10FF),
71		array('Grek', 0x1F00, 0x1FFF),
72		array('Deva', 0xA8E0, 0xA8FF),
73		array('Hans', 0x3000, 0x303F), // Mixed CJK, not just Hans
74		array('Hans', 0x3400, 0xFAFF), // Mixed CJK, not just Hans
75		array('Hans', 0x20000, 0x2FA1F), // Mixed CJK, not just Hans
76	);
77
78	/** @var string[] Characters that are displayed in mirror form in RTL text. */
79	private static $mirror_characters = array(
80		'('   => ')',
81		')'   => '(',
82		'['   => ']',
83		']'   => '[',
84		'{'   => '}',
85		'}'   => '{',
86		'<'   => '>',
87		'>'   => '<',
88		'‹' => '›',
89		'›' => '‹',
90		'«'  => '»',
91		'»'  => '«',
92		'﴾' => '﴿',
93		'﴿' => '﴾',
94		'“' => '”',
95		'”' => '“',
96		'‘' => '’',
97		'’' => '‘',
98	);
99
100	/** @var string Punctuation used to separate list items, typically a comma */
101	public static $list_separator;
102
103	/**
104	 * The prefered locales for this site, or a default list if no preference.
105	 *
106	 * @return LocaleInterface[]
107	 */
108	public static function activeLocales() {
109		$code_list = Site::getPreference('LANGUAGES');
110
111		if ($code_list) {
112			$codes = explode(',', $code_list);
113		} else {
114			$codes = array(
115				'ar', 'bg', 'bs', 'ca', 'cs', 'da', 'de', 'el', 'en-GB', 'en-US', 'es',
116				'et', 'fi', 'fr', 'he', 'hr', 'hu', 'is', 'it', 'ka', 'lt', 'mr', 'nb',
117				'nl', 'nn', 'pl', 'pt', 'ru', 'sk', 'sv', 'tr', 'uk', 'vi', 'zh-Hans',
118			);
119		}
120
121		$locales = array();
122		foreach ($codes as $code) {
123			if (file_exists(WT_ROOT . 'language/' . $code . '.mo')) {
124				try {
125					$locales[] = Locale::create($code);
126				} catch (\Exception $ex) {
127					// No such locale exists?
128				}
129			}
130		}
131		usort($locales, '\Fisharebest\Localization\Locale::compare');
132
133		return $locales;
134	}
135
136	/**
137	 * Which MySQL collation should be used for this locale?
138	 *
139	 * @return string
140	 */
141	public static function collation() {
142		$collation = self::$locale->collation();
143		switch ($collation) {
144		case 'croatian_ci':
145		case 'german2_ci':
146		case 'vietnamese_ci':
147			// Only available in MySQL 5.6
148			return 'utf8_unicode_ci';
149		default:
150			return 'utf8_' . $collation;
151		}
152	}
153
154	/**
155	 * What format is used to display dates in the current locale?
156	 *
157	 * @return string
158	 */
159	public static function dateFormat() {
160		return /* I18N: This is the format string for full dates.  See http://php.net/date for codes */ self::$translator->translate('%j %F %Y');
161	}
162
163	/**
164	 * Generate consistent I18N for datatables.js
165	 *
166	 * @param array|null $lengths An optional array of page lengths
167	 *
168	 * @return string
169	 */
170	public static function datatablesI18N(array $lengths = null) {
171		if ($lengths === null) {
172			$lengths = array(10, 20, 30, 50, 100, -1);
173		}
174
175		$length_menu = '';
176		foreach ($lengths as $length) {
177			$length_menu .=
178				'<option value="' . $length . '">' .
179				($length === -1 ? /* I18N: listbox option, e.g. “10,25,50,100,all” */ self::translate('All') : self::number($length)) .
180				'</option>';
181		}
182		$length_menu = '<select>' . $length_menu . '</select>';
183		$length_menu = /* I18N: Display %s [records per page], %s is a placeholder for listbox containing numeric options */ self::translate('Display %s', $length_menu);
184
185		$digits = self::$locale->digits('0123456789');
186		if ($digits === '0123456789') {
187			$callback = '';
188		} else {
189			$callback = ',
190				"infoCallback": function(oSettings, iStart, iEnd, iMax, iTotal, sPre) {
191					return sPre
192						.replace(/0/g, "' . mb_substr($digits, 0, 1) . '")
193						.replace(/1/g, "' . mb_substr($digits, 1, 1) . '")
194						.replace(/2/g, "' . mb_substr($digits, 2, 1) . '")
195						.replace(/3/g, "' . mb_substr($digits, 3, 1) . '")
196						.replace(/4/g, "' . mb_substr($digits, 4, 1) . '")
197						.replace(/5/g, "' . mb_substr($digits, 5, 1) . '")
198						.replace(/6/g, "' . mb_substr($digits, 6, 1) . '")
199						.replace(/7/g, "' . mb_substr($digits, 7, 1) . '")
200						.replace(/8/g, "' . mb_substr($digits, 8, 1) . '")
201						.replace(/9/g, "' . mb_substr($digits, 9, 1) . '");
202				},
203				"formatNumber": function(iIn) {
204					return String(iIn)
205						.replace(/0/g, "' . mb_substr($digits, 0, 1) . '")
206						.replace(/1/g, "' . mb_substr($digits, 1, 1) . '")
207						.replace(/2/g, "' . mb_substr($digits, 2, 1) . '")
208						.replace(/3/g, "' . mb_substr($digits, 3, 1) . '")
209						.replace(/4/g, "' . mb_substr($digits, 4, 1) . '")
210						.replace(/5/g, "' . mb_substr($digits, 5, 1) . '")
211						.replace(/6/g, "' . mb_substr($digits, 6, 1) . '")
212						.replace(/7/g, "' . mb_substr($digits, 7, 1) . '")
213						.replace(/8/g, "' . mb_substr($digits, 8, 1) . '")
214						.replace(/9/g, "' . mb_substr($digits, 9, 1) . '");
215				}
216			';
217		}
218
219		return
220			'"language": {' .
221			' "paginate": {' .
222			'  "first":    "' . /* I18N: button label, first page    */ self::translate('first') . '",' .
223			'  "last":     "' . /* I18N: button label, last page     */ self::translate('last') . '",' .
224			'  "next":     "' . /* I18N: button label, next page     */ self::translate('next') . '",' .
225			'  "previous": "' . /* I18N: button label, previous page */ self::translate('previous') . '"' .
226			' },' .
227			' "emptyTable":     "' . self::translate('No records to display') . '",' .
228			' "info":           "' . /* I18N: %s are placeholders for numbers */ self::translate('Showing %1$s to %2$s of %3$s', '_START_', '_END_', '_TOTAL_') . '",' .
229			' "infoEmpty":      "' . self::translate('Showing %1$s to %2$s of %3$s', 0, 0, 0) . '",' .
230			' "infoFiltered":   "' . /* I18N: %s is a placeholder for a number */ self::translate('(filtered from %s total entries)', '_MAX_') . '",' .
231			' "infoPostfix":    "",' .
232			' "lengthMenu":     "' . Filter::escapeJs($length_menu) . '",' .
233			' "loadingRecords": "' . self::translate('Loading…') . '",' .
234			' "processing":     "' . self::translate('Loading…') . '",' .
235			' "search":         "' . self::translate('Filter') . '",' .
236			' "url":            "",' .
237			' "zeroRecords":    "' . self::translate('No records to display') . '"' .
238			'}' .
239			$callback;
240	}
241
242	/**
243	 * Convert the digits 0-9 into the local script
244	 *
245	 * Used for years, etc., where we do not want thousands-separators, decimals, etc.
246	 *
247	 * @param int $n
248	 *
249	 * @return string
250	 */
251	public static function digits($n) {
252		return self::$locale->digits($n);
253	}
254
255	/**
256	 * What is the direction of the current locale
257	 *
258	 * @return string "ltr" or "rtl"
259	 */
260	public static function direction() {
261		return self::$locale->direction();
262	}
263
264	/**
265	 * What is the first day of the week.
266	 *
267	 * @return int Sunday=0, Monday=1, etc.
268	 */
269	public static function firstDay() {
270		return self::$locale->territory()->firstDay();
271	}
272
273	/**
274	 * Convert a GEDCOM age string into translated_text
275	 *
276	 * NB: The import function will have normalised this, so we don't need
277	 * to worry about badly formatted strings
278	 * NOTE: this function is not yet complete - eventually it will replace FunctionsDate::get_age_at_event()
279	 *
280	 * @param $string
281	 *
282	 * @return string
283	 */
284	public static function gedcomAge($string) {
285		switch ($string) {
286			case 'STILLBORN':
287				// I18N: Description of an individual’s age at an event.  For example, Died 14 Jan 1900 (stillborn)
288				return self::translate('(stillborn)');
289			case 'INFANT':
290				// I18N: Description of an individual’s age at an event.  For example, Died 14 Jan 1900 (in infancy)
291				return self::translate('(in infancy)');
292			case 'CHILD':
293				// I18N: Description of an individual’s age at an event.  For example, Died 14 Jan 1900 (in childhood)
294				return self::translate('(in childhood)');
295		}
296		$age = array();
297		if (preg_match('/(\d+)y/', $string, $match)) {
298			// I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
299			$years = $match[1];
300			$age[] = self::plural('%s year', '%s years', $years, self::number($years));
301		} else {
302			$years = -1;
303		}
304		if (preg_match('/(\d+)m/', $string, $match)) {
305			// I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
306			$age[] = self::plural('%s month', '%s months', $match[1], self::number($match[1]));
307		}
308		if (preg_match('/(\d+)w/', $string, $match)) {
309			// I18N: Part of an age string. e.g. 7 weeks and 3 days
310			$age[] = self::plural('%s week', '%s weeks', $match[1], self::number($match[1]));
311		}
312		if (preg_match('/(\d+)d/', $string, $match)) {
313			// I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
314			$age[] = self::plural('%s day', '%s days', $match[1], self::number($match[1]));
315		}
316		// If an age is just a number of years, only show the number
317		if (count($age) === 1 && $years >= 0) {
318			$age = $years;
319		}
320		if ($age) {
321			if (!substr_compare($string, '<', 0, 1)) {
322				// I18N: Description of an individual’s age at an event.  For example, Died 14 Jan 1900 (aged less than 21 years)
323				return self::translate('(aged less than %s)', $age);
324			} elseif (!substr_compare($string, '>', 0, 1)) {
325				// I18N: Description of an individual’s age at an event.  For example, Died 14 Jan 1900 (aged more than 21 years)
326				return self::translate('(aged more than %s)', $age);
327			} else {
328				// I18N: Description of an individual’s age at an event.  For example, Died 14 Jan 1900 (aged 43 years)
329				return self::translate('(aged %s)', $age);
330			}
331		} else {
332			// Not a valid string?
333			return self::translate('(aged %s)', $string);
334		}
335	}
336
337	/**
338	 * Generate i18n markup for the <html> tag, e.g. lang="ar" dir="rtl"
339	 *
340	 * @return string
341	 */
342	public static function htmlAttributes() {
343		return self::$locale->htmlAttributes();
344	}
345
346	/**
347	 * Initialise the translation adapter with a locale setting.
348	 *
349	 * @param string|null $code Use this locale/language code, or choose one automatically
350	 *
351	 * @return string $string
352	 */
353	public static function init($code = null) {
354		global $WT_TREE;
355
356		if ($code !== null) {
357			// Create the specified locale
358			self::$locale = Locale::create($code);
359		} else {
360			// Negotiate a locale, but if we can't then use a failsafe
361			self::$locale = new LocaleEnUs;
362			if (Filter::get('lang')) {
363				// A request in the URL
364				try {
365					$locale = Locale::create(Filter::get('lang'));
366					if (file_exists(WT_ROOT . 'language/' . $locale->languageTag() . '.mo')) {
367						self::$locale = $locale;
368					}
369				} catch (\Exception $ex) {
370				}
371			} elseif (Session::has('locale')) {
372				// Previously used
373				self::$locale = Locale::create(Session::get('locale'));
374			} else {
375				// Browser negotiation
376				$default_locale = new LocaleEnUs;
377				try {
378					if ($WT_TREE) {
379						$default_locale = Locale::create($WT_TREE->getPreference('LANGUAGE'));
380					}
381				} catch (\Exception $ex) {
382				}
383				self::$locale = Locale::httpAcceptLanguage($_SERVER, self::installedLocales(), $default_locale);
384			}
385		}
386
387		File::mkdir(WT_DATA_DIR . 'cache');
388		$cache_file = WT_DATA_DIR . 'cache/language-' . self::$locale->languageTag() . '-cache.php';
389		if (file_exists($cache_file)) {
390			$filemtime = filemtime($cache_file);
391		} else {
392			$filemtime = 0;
393		}
394
395		// Load the translation file(s)
396		// Note that glob() returns false instead of an empty array when open_basedir_restriction
397		// is in force and no files are found.  See PHP bug #47358.
398		$translation_files = array_merge(
399			array(WT_ROOT . 'language/' . self::$locale->languageTag() . '.mo'),
400			glob(WT_MODULES_DIR . '*/language/' . self::$locale->languageTag() . '.{csv,php,mo}', GLOB_BRACE) ?: array(),
401			glob(WT_DATA_DIR . 'language/' . self::$locale->languageTag() . '.{csv,php,mo}', GLOB_BRACE) ?: array()
402		);
403
404		// Rebuild files after 2 hours
405		$rebuild_cache = time() > $filemtime + 7200;
406		// Rebuild files if any translation file has been updated
407		foreach ($translation_files as $translation_file) {
408			if (filemtime($translation_file) > $filemtime) {
409				$rebuild_cache = true;
410				break;
411			}
412		}
413
414		if ($rebuild_cache) {
415			$translations = array();
416			foreach ($translation_files as $translation_file) {
417				$translation  = new Translation($translation_file);
418				$translations = array_merge($translations, $translation->asArray());
419			}
420			file_put_contents($cache_file, '<' . '?php return ' . var_export($translations, true) . ';');
421		} else {
422			$translations = include $cache_file;
423		}
424
425		// Create a translator
426		self::$translator = new Translator($translations, self::$locale->pluralRule());
427
428		// Alphabetic sorting sequence (upper-case letters), used by webtrees to sort strings
429		list(, self::$alphabet_upper) = explode('=', self::$translator->translate('ALPHABET_upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ'));
430		// Alphabetic sorting sequence (lower-case letters), used by webtrees to sort strings
431		list(, self::$alphabet_lower) = explode('=', self::$translator->translate('ALPHABET_lower=abcdefghijklmnopqrstuvwxyz'));
432
433		self::$list_separator = /* I18N: This punctuation is used to separate lists of items */ self::translate(', ');
434
435		return self::$locale->languageTag();
436	}
437
438	/**
439	 * All locales for which a translation file exists.
440	 *
441	 * @return LocaleInterface[]
442	 */
443	public static function installedLocales() {
444		$locales = array();
445		foreach (glob(WT_ROOT . 'language/*.mo') as $file) {
446			try {
447				$locales[] = Locale::create(basename($file, '.mo'));
448			} catch (\Exception $ex) {
449				// Not a recognised locale
450			}
451		}
452		usort($locales, '\Fisharebest\Localization\Locale::compare');
453
454		return $locales;
455	}
456
457	/**
458	 * Return the endonym for a given language - as per http://cldr.unicode.org/
459	 *
460	 * @param string $locale
461	 *
462	 * @return string
463	 */
464	public static function languageName($locale) {
465		return Locale::create($locale)->endonym();
466	}
467
468	/**
469	 * Return the script used by a given language
470	 *
471	 * @param string $locale
472	 *
473	 * @return string
474	 */
475	public static function languageScript($locale) {
476		return Locale::create($locale)->script()->code();
477	}
478
479	/**
480	 * Translate a number into the local representation.
481	 *
482	 * e.g. 12345.67 becomes
483	 * en: 12,345.67
484	 * fr: 12 345,67
485	 * de: 12.345,67
486	 *
487	 * @param float $n
488	 * @param int   $precision
489	 *
490	 * @return string
491	 */
492	public static function number($n, $precision = 0) {
493		return self::$locale->number(round($n, $precision));
494	}
495
496	/**
497	 * Translate a fraction into a percentage.
498	 *
499	 * e.g. 0.123 becomes
500	 * en: 12.3%
501	 * fr: 12,3 %
502	 * de: 12,3%
503	 *
504	 * @param float $n
505	 * @param int   $precision
506	 *
507	 * @return string
508	 */
509	public static function percentage($n, $precision = 0) {
510		return self::$locale->percent(round($n, $precision + 2));
511	}
512
513	/**
514	 * Translate a plural string
515	 *
516	 * echo self::plural('There is an error', 'There are errors', $num_errors);
517	 * echo self::plural('There is one error', 'There are %s errors', $num_errors);
518	 * echo self::plural('There is %1$s %2$s cat', 'There are %1$s %2$s cats', $num, $num, $colour);
519	 *
520	 * @return string
521	 */
522	public static function plural(/* var_args */) {
523		$args    = func_get_args();
524		$args[0] = self::$translator->translatePlural($args[0], $args[1], (int) $args[2]);
525		unset($args[1], $args[2]);
526
527		return self::substitutePlaceholders($args);
528	}
529
530	/**
531	 * UTF8 version of PHP::strrev()
532	 *
533	 * Reverse RTL text for third-party libraries such as GD2 and googlechart.
534	 *
535	 * These do not support UTF8 text direction, so we must mimic it for them.
536	 *
537	 * Numbers are always rendered LTR, even in RTL text.
538	 * The visual direction of characters such as parentheses should be reversed.
539	 *
540	 * @param string $text Text to be reversed
541	 *
542	 * @return string
543	 */
544	public static function reverseText($text) {
545		// Remove HTML markup - we can't display it and it is LTR.
546		$text = Filter::unescapeHtml($text);
547
548		// LTR text doesn't need reversing
549		if (self::scriptDirection(self::textScript($text)) === 'ltr') {
550			return $text;
551		}
552
553		// Mirrored characters
554		$text = strtr($text, self::$mirror_characters);
555
556		$reversed = '';
557		$digits   = '';
558		while ($text != '') {
559			$letter = mb_substr($text, 0, 1);
560			$text   = mb_substr($text, 1);
561			if (strpos(self::DIGITS, $letter) !== false) {
562				$digits .= $letter;
563			} else {
564				$reversed = $letter . $digits . $reversed;
565				$digits   = '';
566			}
567		}
568
569		return $digits . $reversed;
570	}
571
572	/**
573	 * Return the direction (ltr or rtl) for a given script
574	 *
575	 * The PHP/intl library does not provde this information, so we need
576	 * our own lookup table.
577	 *
578	 * @param string $script
579	 *
580	 * @return string
581	 */
582	public static function scriptDirection($script) {
583		switch ($script) {
584		case 'Arab':
585		case 'Hebr':
586		case 'Mong':
587		case 'Thaa':
588			return 'rtl';
589		default:
590			return 'ltr';
591		}
592	}
593
594	/**
595	 * UTF8 version of PHP::strcasecmp()
596	 *
597	 * Perform a case-insensitive comparison of two strings, using rules from the current locale
598	 *
599	 * @param string $string1
600	 * @param string $string2
601	 *
602	 * @return int
603	 */
604	public static function strcasecmp($string1, $string2) {
605		$strpos1 = 0;
606		$strpos2 = 0;
607		$strlen1 = strlen($string1);
608		$strlen2 = strlen($string2);
609		while ($strpos1 < $strlen1 && $strpos2 < $strlen2) {
610			$byte1 = ord($string1[$strpos1]);
611			$byte2 = ord($string2[$strpos2]);
612			if (($byte1 & 0xE0) === 0xC0) {
613				$chr1 = $string1[$strpos1++] . $string1[$strpos1++];
614			} elseif (($byte1 & 0xF0) === 0xE0) {
615				$chr1 = $string1[$strpos1++] . $string1[$strpos1++] . $string1[$strpos1++];
616			} else {
617				$chr1 = $string1[$strpos1++];
618			}
619			if (($byte2 & 0xE0) === 0xC0) {
620				$chr2 = $string2[$strpos2++] . $string2[$strpos2++];
621			} elseif (($byte2 & 0xF0) === 0xE0) {
622				$chr2 = $string2[$strpos2++] . $string2[$strpos2++] . $string2[$strpos2++];
623			} else {
624				$chr2 = $string2[$strpos2++];
625			}
626			if ($chr1 === $chr2) {
627				continue;
628			}
629			// Try the local alphabet first
630			$offset1 = strpos(self::$alphabet_lower, $chr1);
631			if ($offset1 === false) {
632				$offset1 = strpos(self::$alphabet_upper, $chr1);
633			}
634			$offset2 = strpos(self::$alphabet_lower, $chr2);
635			if ($offset2 === false) {
636				$offset2 = strpos(self::$alphabet_upper, $chr2);
637			}
638			if ($offset1 !== false && $offset2 !== false) {
639				if ($offset1 === $offset2) {
640					continue;
641				} else {
642					return $offset1 - $offset2;
643				}
644			}
645			// Try the global alphabet next
646			$offset1 = strpos(self::ALPHABET_LOWER, $chr1);
647			if ($offset1 === false) {
648				$offset1 = strpos(self::ALPHABET_UPPER, $chr1);
649			}
650			$offset2 = strpos(self::ALPHABET_LOWER, $chr2);
651			if ($offset2 === false) {
652				$offset2 = strpos(self::ALPHABET_UPPER, $chr2);
653			}
654			if ($offset1 !== false && $offset2 !== false) {
655				if ($offset1 === $offset2) {
656					continue;
657				} else {
658					return $offset1 - $offset2;
659				}
660			}
661			// Just compare by unicode order
662			return strcmp($chr1, $chr2);
663		}
664		// Shortest string comes first.
665		return ($strlen1 - $strpos1) - ($strlen2 - $strpos2);
666	}
667
668	/**
669	 * UTF8 version of PHP::strtolower()
670	 *
671	 * Convert a string to lower case, using the rules from the current locale
672	 *
673	 * @param string $string
674	 *
675	 * @return string
676	 */
677	public static function strtolower($string) {
678		if (self::$locale->language()->code() === 'tr' || self::$locale->language()->code() === 'az') {
679			return TurkishUtf8::strtolower($string);
680		} else {
681			return mb_strtolower($string);
682		}
683	}
684
685	/**
686	 * UTF8 version of PHP::strtoupper()
687	 *
688	 * Convert a string to upper case, using the rules from the current locale
689	 *
690	 * @param string $string
691	 *
692	 * @return string
693	 */
694	public static function strtoupper($string) {
695		if (self::$locale->language()->code() === 'tr' || self::$locale->language()->code() === 'az') {
696			return TurkishUtf8::strtoupper($string);
697		} else {
698			return mb_strtoupper($string);
699		}
700	}
701
702	/**
703	 * Substitute any "%s" placeholders in a translated string.
704	 * This also allows us to have translated strings that contain
705	 * "%" characters, which can't be passed to sprintf.
706	 *
707	 * @param string[] $args translated string plus optional parameters
708	 *
709	 * @return string
710	 */
711	private static function substitutePlaceholders(array $args) {
712		if (count($args) > 1) {
713			return call_user_func_array('sprintf', $args);
714		} else {
715			return $args[0];
716		}
717	}
718
719	/**
720	 * Identify the script used for a piece of text
721	 *
722	 * @param $string
723	 *
724	 * @return string
725	 */
726	public static function textScript($string) {
727		$string = strip_tags($string); // otherwise HTML tags show up as latin
728		$string = html_entity_decode($string, ENT_QUOTES, 'UTF-8'); // otherwise HTML entities show up as latin
729		$string = str_replace(array('@N.N.', '@P.N.'), '', $string); // otherwise unknown names show up as latin
730		$pos    = 0;
731		$strlen = strlen($string);
732		while ($pos < $strlen) {
733			// get the Unicode Code Point for the character at position $pos
734			$byte1 = ord($string[$pos]);
735			if ($byte1 < 0x80) {
736				$code_point = $byte1;
737				$chrlen     = 1;
738			} elseif ($byte1 < 0xC0) {
739				// Invalid continuation character
740				return 'Latn';
741			} elseif ($byte1 < 0xE0) {
742				$code_point = (($byte1 & 0x1F) << 6) + (ord($string[$pos + 1]) & 0x3F);
743				$chrlen     = 2;
744			} elseif ($byte1 < 0xF0) {
745				$code_point = (($byte1 & 0x0F) << 12) + ((ord($string[$pos + 1]) & 0x3F) << 6) + (ord($string[$pos + 2]) & 0x3F);
746				$chrlen     = 3;
747			} elseif ($byte1 < 0xF8) {
748				$code_point = (($byte1 & 0x07) << 24) + ((ord($string[$pos + 1]) & 0x3F) << 12) + ((ord($string[$pos + 2]) & 0x3F) << 6) + (ord($string[$pos + 3]) & 0x3F);
749				$chrlen     = 3;
750			} else {
751				// Invalid UTF
752				return 'Latn';
753			}
754
755			foreach (self::$scripts as $range) {
756				if ($code_point >= $range[1] && $code_point <= $range[2]) {
757					return $range[0];
758				}
759			}
760			// Not a recognised script.  Maybe punctuation, spacing, etc.  Keep looking.
761			$pos += $chrlen;
762		}
763
764		return 'Latn';
765	}
766
767	/**
768	 * Convert a number of seconds into a relative time.  For example, 630 => "10 hours, 30 minutes ago"
769	 *
770	 * @param int $seconds
771	 *
772	 * @return string
773	 */
774	public static function timeAgo($seconds) {
775		$minute = 60;
776		$hour   = 60 * $minute;
777		$day    = 24 * $hour;
778		$month  = 30 * $day;
779		$year   = 365 * $day;
780
781		if ($seconds > $year) {
782			$years = (int) ($seconds / $year);
783
784			return self::plural('%s year ago', '%s years ago', $years, self::number($years));
785		} elseif ($seconds > $month) {
786			$months = (int) ($seconds / $month);
787
788			return self::plural('%s month ago', '%s months ago', $months, self::number($months));
789		} elseif ($seconds > $day) {
790			$days = (int) ($seconds / $day);
791
792			return self::plural('%s day ago', '%s days ago', $days, self::number($days));
793		} elseif ($seconds > $hour) {
794			$hours = (int) ($seconds / $hour);
795
796			return self::plural('%s hour ago', '%s hours ago', $hours, self::number($hours));
797		} elseif ($seconds > $minute) {
798			$minutes = (int) ($seconds / $minute);
799
800			return self::plural('%s minute ago', '%s minutes ago', $minutes, self::number($minutes));
801		} else {
802			return self::plural('%s second ago', '%s seconds ago', $seconds, self::number($seconds));
803		}
804	}
805
806	/**
807	 * What format is used to display dates in the current locale?
808	 *
809	 * @return string
810	 */
811	public static function timeFormat() {
812		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');
813	}
814
815	/**
816	 * Translate a string, and then substitute placeholders
817	 *
818	 * echo I18N::translate('Hello World!');
819	 * echo I18N::translate('The %s sat on the mat', 'cat');
820	 *
821	 * @return string
822	 */
823	public static function translate(/* var_args */) {
824		$args    = func_get_args();
825		$args[0] = self::$translator->translate($args[0]);
826
827		return self::substitutePlaceholders($args);
828	}
829
830	/**
831	 * Context sensitive version of translate.
832	 *
833	 * echo I18N::translateContext('NOMINATIVE', 'January');
834	 * echo I18N::translateContext('GENITIVE',   'January');
835	 *
836	 * @return string
837	 */
838	public static function translateContext(/* var_args */) {
839		$args    = func_get_args();
840		$args[0] = self::$translator->translateContext($args[0], $args[1]);
841		unset($args[1]);
842
843		return self::substitutePlaceholders($args);
844	}
845
846	/**
847	 * What is the last day of the weekend.
848	 *
849	 * @return int Sunday=0, Monday=1, etc.
850	 */
851	public static function weekendEnd() {
852		return self::$locale->territory()->weekendEnd();
853	}
854
855	/**
856	 * What is the first day of the weekend.
857	 *
858	 * @return int Sunday=0, Monday=1, etc.
859	 */
860	public static function weekendStart() {
861		return self::$locale->territory()->weekendStart();
862	}
863
864	/**
865	 * Which calendar prefered in this locale?
866	 *
867	 * @return CalendarInterface
868	 */
869	public static function defaultCalendar() {
870		switch (self::$locale->languageTag()) {
871			case 'ar':
872				return new ArabicCalendar;
873			case 'fa':
874				return new PersianCalendar;
875			case 'he':
876			case 'yi':
877				return new JewishCalendar;
878			default:
879				return new GregorianCalendar;
880		}
881	}
882}
883