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