xref: /webtrees/app/I18N.php (revision 9797fe2e6d6eb3d34559dcf546f47c58bc50777b)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2016 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 Exception;
19use Fisharebest\ExtCalendar\ArabicCalendar;
20use Fisharebest\ExtCalendar\CalendarInterface;
21use Fisharebest\ExtCalendar\GregorianCalendar;
22use Fisharebest\ExtCalendar\JewishCalendar;
23use Fisharebest\ExtCalendar\PersianCalendar;
24use Fisharebest\Localization\Locale;
25use Fisharebest\Localization\Locale\LocaleEnUs;
26use Fisharebest\Localization\Locale\LocaleInterface;
27use Fisharebest\Localization\Translation;
28use Fisharebest\Localization\Translator;
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: A button label, first page */ self::translate('first') . '",' .
223			'  "last":     "' . /* I18N: A button label, last page */ self::translate('last') . '",' .
224			'  "next":     "' . /* I18N: A button label, next page */ self::translate('next') . '",' .
225			'  "previous": "' . /* I18N: A 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		mb_internal_encoding('UTF-8');
357
358		if ($code !== null) {
359			// Create the specified locale
360			self::$locale = Locale::create($code);
361		} else {
362			// Negotiate a locale, but if we can't then use a failsafe
363			self::$locale = new LocaleEnUs;
364			if (Session::has('locale')) {
365				// Previously used
366				self::$locale = Locale::create(Session::get('locale'));
367			} else {
368				// Browser negotiation
369				$default_locale = new LocaleEnUs;
370				try {
371					if ($WT_TREE) {
372						$default_locale = Locale::create($WT_TREE->getPreference('LANGUAGE'));
373					}
374				} catch (\Exception $ex) {
375				}
376				self::$locale = Locale::httpAcceptLanguage($_SERVER, self::installedLocales(), $default_locale);
377			}
378		}
379
380		$cache_dir  = WT_DATA_DIR . 'cache/';
381		$cache_file = $cache_dir . 'language-' . self::$locale->languageTag() . '-cache.php';
382		if (file_exists($cache_file)) {
383			$filemtime = filemtime($cache_file);
384		} else {
385			$filemtime = 0;
386		}
387
388		// Load the translation file(s)
389		// Note that glob() returns false instead of an empty array when open_basedir_restriction
390		// is in force and no files are found. See PHP bug #47358.
391		if (defined('GLOB_BRACE')) {
392			$translation_files = array_merge(
393				array(WT_ROOT . 'language/' . self::$locale->languageTag() . '.mo'),
394				glob(WT_MODULES_DIR . '*/language/' . self::$locale->languageTag() . '.{csv,php,mo}', GLOB_BRACE) ?: array(),
395				glob(WT_DATA_DIR . 'language/' . self::$locale->languageTag() . '.{csv,php,mo}', GLOB_BRACE) ?: array()
396			);
397		} else {
398			// Some servers do not have GLOB_BRACE - see http://php.net/manual/en/function.glob.php
399			$translation_files = array_merge(
400				array(WT_ROOT . 'language/' . self::$locale->languageTag() . '.mo'),
401				glob(WT_MODULES_DIR . '*/language/' . self::$locale->languageTag() . '.csv') ?: array(),
402				glob(WT_MODULES_DIR . '*/language/' . self::$locale->languageTag() . '.php') ?: array(),
403				glob(WT_MODULES_DIR . '*/language/' . self::$locale->languageTag() . '.mo') ?: array(),
404				glob(WT_DATA_DIR . 'language/' . self::$locale->languageTag() . '.csv') ?: array(),
405				glob(WT_DATA_DIR . 'language/' . self::$locale->languageTag() . '.php') ?: array(),
406				glob(WT_DATA_DIR . 'language/' . self::$locale->languageTag() . '.mo') ?: array()
407			);
408		}
409		// Rebuild files after one hour
410		$rebuild_cache = time() > $filemtime + 3600;
411		// Rebuild files if any translation file has been updated
412		foreach ($translation_files as $translation_file) {
413			if (filemtime($translation_file) > $filemtime) {
414				$rebuild_cache = true;
415				break;
416			}
417		}
418
419		if ($rebuild_cache) {
420			$translations = array();
421			foreach ($translation_files as $translation_file) {
422				$translation  = new Translation($translation_file);
423				$translations = array_merge($translations, $translation->asArray());
424			}
425			try {
426				File::mkdir($cache_dir);
427				file_put_contents($cache_file, '<?php return ' . var_export($translations, true) . ';');
428			} catch (Exception $ex) {
429				// During setup, we may not have been able to create it.
430			}
431		} else {
432			$translations = include $cache_file;
433		}
434
435		// Create a translator
436		self::$translator = new Translator($translations, self::$locale->pluralRule());
437
438		// Alphabetic sorting sequence (upper-case letters), used by webtrees to sort strings
439		list(, self::$alphabet_upper) = explode('=', self::$translator->translate('ALPHABET_upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ'));
440		// Alphabetic sorting sequence (lower-case letters), used by webtrees to sort strings
441		list(, self::$alphabet_lower) = explode('=', self::$translator->translate('ALPHABET_lower=abcdefghijklmnopqrstuvwxyz'));
442
443		self::$list_separator = /* I18N: This punctuation is used to separate lists of items */ self::translate(', ');
444
445		return self::$locale->languageTag();
446	}
447
448	/**
449	 * All locales for which a translation file exists.
450	 *
451	 * @return LocaleInterface[]
452	 */
453	public static function installedLocales() {
454		$locales = array();
455		foreach (glob(WT_ROOT . 'language/*.mo') as $file) {
456			try {
457				$locales[] = Locale::create(basename($file, '.mo'));
458			} catch (\Exception $ex) {
459				// Not a recognised locale
460			}
461		}
462		usort($locales, '\Fisharebest\Localization\Locale::compare');
463
464		return $locales;
465	}
466
467	/**
468	 * Return the endonym for a given language - as per http://cldr.unicode.org/
469	 *
470	 * @param string $locale
471	 *
472	 * @return string
473	 */
474	public static function languageName($locale) {
475		return Locale::create($locale)->endonym();
476	}
477
478	/**
479	 * Return the script used by a given language
480	 *
481	 * @param string $locale
482	 *
483	 * @return string
484	 */
485	public static function languageScript($locale) {
486		return Locale::create($locale)->script()->code();
487	}
488
489	/**
490	 * Translate a number into the local representation.
491	 *
492	 * e.g. 12345.67 becomes
493	 * en: 12,345.67
494	 * fr: 12 345,67
495	 * de: 12.345,67
496	 *
497	 * @param float $n
498	 * @param int   $precision
499	 *
500	 * @return string
501	 */
502	public static function number($n, $precision = 0) {
503		return self::$locale->number(round($n, $precision));
504	}
505
506	/**
507	 * Translate a fraction into a percentage.
508	 *
509	 * e.g. 0.123 becomes
510	 * en: 12.3%
511	 * fr: 12,3 %
512	 * de: 12,3%
513	 *
514	 * @param float $n
515	 * @param int   $precision
516	 *
517	 * @return string
518	 */
519	public static function percentage($n, $precision = 0) {
520		return self::$locale->percent(round($n, $precision + 2));
521	}
522
523	/**
524	 * Translate a plural string
525	 *
526	 * echo self::plural('There is an error', 'There are errors', $num_errors);
527	 * echo self::plural('There is one error', 'There are %s errors', $num_errors);
528	 * echo self::plural('There is %1$s %2$s cat', 'There are %1$s %2$s cats', $num, $num, $colour);
529	 *
530	 * @return string
531	 */
532	public static function plural(/* var_args */) {
533		$args    = func_get_args();
534		$args[0] = self::$translator->translatePlural($args[0], $args[1], (int) $args[2]);
535		unset($args[1], $args[2]);
536
537		return self::substitutePlaceholders($args);
538	}
539
540	/**
541	 * UTF8 version of PHP::strrev()
542	 *
543	 * Reverse RTL text for third-party libraries such as GD2 and googlechart.
544	 *
545	 * These do not support UTF8 text direction, so we must mimic it for them.
546	 *
547	 * Numbers are always rendered LTR, even in RTL text.
548	 * The visual direction of characters such as parentheses should be reversed.
549	 *
550	 * @param string $text Text to be reversed
551	 *
552	 * @return string
553	 */
554	public static function reverseText($text) {
555		// Remove HTML markup - we can't display it and it is LTR.
556		$text = Filter::unescapeHtml($text);
557
558		// LTR text doesn't need reversing
559		if (self::scriptDirection(self::textScript($text)) === 'ltr') {
560			return $text;
561		}
562
563		// Mirrored characters
564		$text = strtr($text, self::$mirror_characters);
565
566		$reversed = '';
567		$digits   = '';
568		while ($text != '') {
569			$letter = mb_substr($text, 0, 1);
570			$text   = mb_substr($text, 1);
571			if (strpos(self::DIGITS, $letter) !== false) {
572				$digits .= $letter;
573			} else {
574				$reversed = $letter . $digits . $reversed;
575				$digits   = '';
576			}
577		}
578
579		return $digits . $reversed;
580	}
581
582	/**
583	 * Return the direction (ltr or rtl) for a given script
584	 *
585	 * The PHP/intl library does not provde this information, so we need
586	 * our own lookup table.
587	 *
588	 * @param string $script
589	 *
590	 * @return string
591	 */
592	public static function scriptDirection($script) {
593		switch ($script) {
594		case 'Arab':
595		case 'Hebr':
596		case 'Mong':
597		case 'Thaa':
598			return 'rtl';
599		default:
600			return 'ltr';
601		}
602	}
603
604	/**
605	 * UTF8 version of PHP::strcasecmp()
606	 *
607	 * Perform a case-insensitive comparison of two strings, using rules from the current locale
608	 *
609	 * @param string $string1
610	 * @param string $string2
611	 *
612	 * @return int
613	 */
614	public static function strcasecmp($string1, $string2) {
615		$strpos1 = 0;
616		$strpos2 = 0;
617		$strlen1 = strlen($string1);
618		$strlen2 = strlen($string2);
619		while ($strpos1 < $strlen1 && $strpos2 < $strlen2) {
620			$byte1 = ord($string1[$strpos1]);
621			$byte2 = ord($string2[$strpos2]);
622			if (($byte1 & 0xE0) === 0xC0) {
623				$chr1 = $string1[$strpos1++] . $string1[$strpos1++];
624			} elseif (($byte1 & 0xF0) === 0xE0) {
625				$chr1 = $string1[$strpos1++] . $string1[$strpos1++] . $string1[$strpos1++];
626			} else {
627				$chr1 = $string1[$strpos1++];
628			}
629			if (($byte2 & 0xE0) === 0xC0) {
630				$chr2 = $string2[$strpos2++] . $string2[$strpos2++];
631			} elseif (($byte2 & 0xF0) === 0xE0) {
632				$chr2 = $string2[$strpos2++] . $string2[$strpos2++] . $string2[$strpos2++];
633			} else {
634				$chr2 = $string2[$strpos2++];
635			}
636			if ($chr1 === $chr2) {
637				continue;
638			}
639			// Try the local alphabet first
640			$offset1 = strpos(self::$alphabet_lower, $chr1);
641			if ($offset1 === false) {
642				$offset1 = strpos(self::$alphabet_upper, $chr1);
643			}
644			$offset2 = strpos(self::$alphabet_lower, $chr2);
645			if ($offset2 === false) {
646				$offset2 = strpos(self::$alphabet_upper, $chr2);
647			}
648			if ($offset1 !== false && $offset2 !== false) {
649				if ($offset1 === $offset2) {
650					continue;
651				} else {
652					return $offset1 - $offset2;
653				}
654			}
655			// Try the global alphabet next
656			$offset1 = strpos(self::ALPHABET_LOWER, $chr1);
657			if ($offset1 === false) {
658				$offset1 = strpos(self::ALPHABET_UPPER, $chr1);
659			}
660			$offset2 = strpos(self::ALPHABET_LOWER, $chr2);
661			if ($offset2 === false) {
662				$offset2 = strpos(self::ALPHABET_UPPER, $chr2);
663			}
664			if ($offset1 !== false && $offset2 !== false) {
665				if ($offset1 === $offset2) {
666					continue;
667				} else {
668					return $offset1 - $offset2;
669				}
670			}
671			// Just compare by unicode order
672			return strcmp($chr1, $chr2);
673		}
674		// Shortest string comes first.
675		return ($strlen1 - $strpos1) - ($strlen2 - $strpos2);
676	}
677
678	/**
679	 * UTF8 version of PHP::strtolower()
680	 *
681	 * Convert a string to lower case, using the rules from the current locale
682	 *
683	 * @param string $string
684	 *
685	 * @return string
686	 */
687	public static function strtolower($string) {
688		if (self::$locale->language()->code() === 'tr' || self::$locale->language()->code() === 'az') {
689			$string = strtr($string, array('I' => 'ı', 'İ' => 'i'));
690		}
691
692		return mb_strtolower($string);
693	}
694
695	/**
696	 * UTF8 version of PHP::strtoupper()
697	 *
698	 * Convert a string to upper case, using the rules from the current locale
699	 *
700	 * @param string $string
701	 *
702	 * @return string
703	 */
704	public static function strtoupper($string) {
705		if (self::$locale->language()->code() === 'tr' || self::$locale->language()->code() === 'az') {
706			$string = strtr($string, array('ı' => 'I', 'i' => 'İ'));
707		}
708
709		return mb_strtoupper($string);
710	}
711
712	/**
713	 * Substitute any "%s" placeholders in a translated string.
714	 * This also allows us to have translated strings that contain
715	 * "%" characters, which can't be passed to sprintf.
716	 *
717	 * @param string[] $args translated string plus optional parameters
718	 *
719	 * @return string
720	 */
721	private static function substitutePlaceholders(array $args) {
722		if (count($args) > 1) {
723			return call_user_func_array('sprintf', $args);
724		} else {
725			return $args[0];
726		}
727	}
728
729	/**
730	 * Identify the script used for a piece of text
731	 *
732	 * @param $string
733	 *
734	 * @return string
735	 */
736	public static function textScript($string) {
737		$string = strip_tags($string); // otherwise HTML tags show up as latin
738		$string = html_entity_decode($string, ENT_QUOTES, 'UTF-8'); // otherwise HTML entities show up as latin
739		$string = str_replace(array('@N.N.', '@P.N.'), '', $string); // otherwise unknown names show up as latin
740		$pos    = 0;
741		$strlen = strlen($string);
742		while ($pos < $strlen) {
743			// get the Unicode Code Point for the character at position $pos
744			$byte1 = ord($string[$pos]);
745			if ($byte1 < 0x80) {
746				$code_point = $byte1;
747				$chrlen     = 1;
748			} elseif ($byte1 < 0xC0) {
749				// Invalid continuation character
750				return 'Latn';
751			} elseif ($byte1 < 0xE0) {
752				$code_point = (($byte1 & 0x1F) << 6) + (ord($string[$pos + 1]) & 0x3F);
753				$chrlen     = 2;
754			} elseif ($byte1 < 0xF0) {
755				$code_point = (($byte1 & 0x0F) << 12) + ((ord($string[$pos + 1]) & 0x3F) << 6) + (ord($string[$pos + 2]) & 0x3F);
756				$chrlen     = 3;
757			} elseif ($byte1 < 0xF8) {
758				$code_point = (($byte1 & 0x07) << 24) + ((ord($string[$pos + 1]) & 0x3F) << 12) + ((ord($string[$pos + 2]) & 0x3F) << 6) + (ord($string[$pos + 3]) & 0x3F);
759				$chrlen     = 3;
760			} else {
761				// Invalid UTF
762				return 'Latn';
763			}
764
765			foreach (self::$scripts as $range) {
766				if ($code_point >= $range[1] && $code_point <= $range[2]) {
767					return $range[0];
768				}
769			}
770			// Not a recognised script. Maybe punctuation, spacing, etc. Keep looking.
771			$pos += $chrlen;
772		}
773
774		return 'Latn';
775	}
776
777	/**
778	 * Convert a number of seconds into a relative time. For example, 630 => "10 hours, 30 minutes ago"
779	 *
780	 * @param int $seconds
781	 *
782	 * @return string
783	 */
784	public static function timeAgo($seconds) {
785		$minute = 60;
786		$hour   = 60 * $minute;
787		$day    = 24 * $hour;
788		$month  = 30 * $day;
789		$year   = 365 * $day;
790
791		if ($seconds > $year) {
792			$years = (int) ($seconds / $year);
793
794			return self::plural('%s year ago', '%s years ago', $years, self::number($years));
795		} elseif ($seconds > $month) {
796			$months = (int) ($seconds / $month);
797
798			return self::plural('%s month ago', '%s months ago', $months, self::number($months));
799		} elseif ($seconds > $day) {
800			$days = (int) ($seconds / $day);
801
802			return self::plural('%s day ago', '%s days ago', $days, self::number($days));
803		} elseif ($seconds > $hour) {
804			$hours = (int) ($seconds / $hour);
805
806			return self::plural('%s hour ago', '%s hours ago', $hours, self::number($hours));
807		} elseif ($seconds > $minute) {
808			$minutes = (int) ($seconds / $minute);
809
810			return self::plural('%s minute ago', '%s minutes ago', $minutes, self::number($minutes));
811		} else {
812			return self::plural('%s second ago', '%s seconds ago', $seconds, self::number($seconds));
813		}
814	}
815
816	/**
817	 * What format is used to display dates in the current locale?
818	 *
819	 * @return string
820	 */
821	public static function timeFormat() {
822		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');
823	}
824
825	/**
826	 * Translate a string, and then substitute placeholders
827	 *
828	 * echo I18N::translate('Hello World!');
829	 * echo I18N::translate('The %s sat on the mat', 'cat');
830	 *
831	 * @return string
832	 */
833	public static function translate(/* var_args */) {
834		$args    = func_get_args();
835		$args[0] = self::$translator->translate($args[0]);
836
837		return self::substitutePlaceholders($args);
838	}
839
840	/**
841	 * Context sensitive version of translate.
842	 *
843	 * echo I18N::translateContext('NOMINATIVE', 'January');
844	 * echo I18N::translateContext('GENITIVE', 'January');
845	 *
846	 * @return string
847	 */
848	public static function translateContext(/* var_args */) {
849		$args    = func_get_args();
850		$args[0] = self::$translator->translateContext($args[0], $args[1]);
851		unset($args[1]);
852
853		return self::substitutePlaceholders($args);
854	}
855
856	/**
857	 * What is the last day of the weekend.
858	 *
859	 * @return int Sunday=0, Monday=1, etc.
860	 */
861	public static function weekendEnd() {
862		return self::$locale->territory()->weekendEnd();
863	}
864
865	/**
866	 * What is the first day of the weekend.
867	 *
868	 * @return int Sunday=0, Monday=1, etc.
869	 */
870	public static function weekendStart() {
871		return self::$locale->territory()->weekendStart();
872	}
873
874	/**
875	 * Which calendar prefered in this locale?
876	 *
877	 * @return CalendarInterface
878	 */
879	public static function defaultCalendar() {
880		switch (self::$locale->languageTag()) {
881		case 'ar':
882			return new ArabicCalendar;
883		case 'fa':
884			return new PersianCalendar;
885		case 'he':
886		case 'yi':
887			return new JewishCalendar;
888		default:
889			return new GregorianCalendar;
890		}
891	}
892}
893