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