xref: /webtrees/app/I18N.php (revision 76ab908c83a7ad14130d09d30c1d81543922dff3)
1<?php
2namespace Fisharebest\Webtrees;
3
4/**
5 * webtrees: online genealogy
6 * Copyright (C) 2015 webtrees development team
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19use Fisharebest\Localization\Locale;
20use Patchwork\TurkishUtf8;
21use Zend_Cache;
22use Zend_Cache_Core;
23use Zend_Registry;
24use Zend_Translate;
25
26/**
27 * Class I18N - Functions to support internationalization (i18n) functionality.
28 */
29class I18N {
30	/** @var Locale The current locale (e.g. LocaleEnGb) */
31	private static $locale;
32
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 MySQL collation sequence used by this language, typically utf8_unicode_ci */
94	public static $collation;
95
96	/** @var string Punctuation used to separate list items, typically a comma */
97	public static $list_separator;
98
99	/** @var Zend_Cache_Core */
100	private static $cache;
101
102	/** @var Zend_Translate */
103	private static $translation_adapter;
104
105	/**
106	 * Initialise the translation adapter with a locale setting.
107	 *
108	 * @param string|null $locale If no locale specified, choose one automatically
109	 *
110	 * @return string $string
111	 */
112	public static function init($locale = null) {
113		global $WT_SESSION, $WT_TREE;
114
115		// The translation libraries only work with a cache.
116		$cache_options = array(
117			'automatic_serialization' => true,
118			'cache_id_prefix'         => md5(WT_BASE_URL),
119		);
120
121		if (ini_get('apc.enabled')) {
122			self::$cache = Zend_Cache::factory('Core', 'Apc', $cache_options, array());
123		} elseif (File::mkdir(WT_DATA_DIR . 'cache')) {
124			self::$cache = Zend_Cache::factory('Core', 'File', $cache_options, array('cache_dir' => WT_DATA_DIR . 'cache'));
125		} else {
126			self::$cache = Zend_Cache::factory('Core', 'Zend_Cache_Backend_BlackHole', $cache_options, array(), false, true);
127		}
128
129		Zend_Translate::setCache(self::$cache);
130
131
132		$installed_locales = array();
133		foreach (self::installedLocales() as $installed_locale) {
134			$installed_locales[$installed_locale->languageTag()] = $installed_locale->endonym();
135		}
136
137		if (is_null($locale) || !array_key_exists($locale, $installed_locales)) {
138			// Automatic locale selection.
139			if (array_key_exists(Filter::get('lang'), $installed_locales)) {
140				// Requested in the URL?
141				$locale = Filter::get('lang');
142			} elseif (array_key_exists($WT_SESSION->locale, $installed_locales)) {
143				// Rembered from a previous visit?
144				$locale = $WT_SESSION->locale;
145			} else {
146				// Browser preference takes priority over gedcom default
147				if (empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
148					$prefs = array();
149				} else {
150					$prefs = explode(',', str_replace(' ', '', $_SERVER['HTTP_ACCEPT_LANGUAGE']));
151				}
152				if ($WT_TREE) {
153					// Add the tree’s default language as a low-priority
154					$locale  = $WT_TREE->getPreference('LANGUAGE');
155					$prefs[] = $locale . ';q=0.2';
156				}
157				$prefs2 = array();
158				foreach ($prefs as $pref) {
159					list($l, $q) = explode(';q=', $pref . ';q=1.0');
160					$l = preg_replace_callback(
161						'/_[a-z][a-z]$/',
162						function($x) { return strtoupper($x[0]); },
163						str_replace('-', '_', $l)
164					); // en-gb => en_GB
165					if (array_key_exists($l, $prefs2)) {
166						$prefs2[$l] = max((float) $q, $prefs2[$l]);
167					} else {
168						$prefs2[$l] = (float) $q;
169					}
170				}
171				// Ensure there is a fallback.
172				if (!array_key_exists('en-US', $prefs2)) {
173					$prefs2['en-US'] = 0.01;
174				}
175				arsort($prefs2);
176				foreach (array_keys($prefs2) as $pref) {
177					if (array_key_exists($pref, $installed_locales)) {
178						$locale = $pref;
179						break;
180					}
181				}
182			}
183		}
184
185		// Load the translation file
186		self::$translation_adapter = new Zend_Translate('gettext', WT_ROOT . 'language/' . $locale . '.mo', $locale);
187
188		// Deprecated - some custom modules use this to add translations
189		Zend_Registry::set('Zend_Translate', self::$translation_adapter);
190
191		// Load any local user translations
192		if (is_dir(WT_DATA_DIR . 'language')) {
193			if (file_exists(WT_DATA_DIR . 'language/' . $locale . '.mo')) {
194				self::addTranslation(
195					new Zend_Translate('gettext', WT_DATA_DIR . 'language/' . $locale . '.mo', $locale)
196				);
197			}
198			if (file_exists(WT_DATA_DIR . 'language/' . $locale . '.php')) {
199				self::addTranslation(
200					new Zend_Translate('array', WT_DATA_DIR . 'language/' . $locale . '.php', $locale)
201				);
202			}
203			if (file_exists(WT_DATA_DIR . 'language/' . $locale . '.csv')) {
204				self::addTranslation(
205					new Zend_Translate('csv', WT_DATA_DIR . 'language/' . $locale . '.csv', $locale)
206				);
207			}
208		}
209
210		// Extract language settings from the translation file
211		global $DATE_FORMAT; // I18N: This is the format string for full dates.  See http://php.net/date for codes
212		$DATE_FORMAT = self::noop('%j %F %Y');
213
214		global $TIME_FORMAT; // I18N: This is the format string for the time-of-day.  See http://php.net/date for codes
215		$TIME_FORMAT = self::noop('%H:%i:%s');
216
217		// Alphabetic sorting sequence (upper-case letters), used by webtrees to sort strings
218		list(, self::$alphabet_upper) = explode('=', self::noop('ALPHABET_upper=ABCDEFGHIJKLMNOPQRSTUVWXYZ'));
219		// Alphabetic sorting sequence (lower-case letters), used by webtrees to sort strings
220		list(, self::$alphabet_lower) = explode('=', self::noop('ALPHABET_lower=abcdefghijklmnopqrstuvwxyz'));
221
222		global $WEEK_START; // I18N: This is the first day of the week on calendars. 0=Sunday, 1=Monday...
223		list(, $WEEK_START) = explode('=', self::noop('WEEK_START=0'));
224
225
226		// Save the current locale, and some attributes of it
227		self::$locale = Locale::create($locale);
228
229		self::$list_separator = /* I18N: This punctuation is used to separate lists of items */ self::translate(', ');
230		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');
231
232		return self::$locale->languageTag();
233	}
234
235	/**
236	 * Add a translation file
237	 *
238	 * @param Zend_Translate $translation
239	 */
240	public static function addTranslation(Zend_Translate $translation) {
241		self::$translation_adapter->getAdapter()->addTranslation(array('content' => $translation));
242	}
243
244	/**
245	 * The prefered locales for this site, or a default list if no preference.
246	 *
247	 * @return Locale[]
248	 */
249	public static function activeLocales() {
250		$code_list = Site::getPreference('LANGUAGES');
251
252		if ($code_list) {
253			$codes = explode(',', $code_list);
254		} else {
255			$codes = array(
256				'ar', 'bg', 'bs', 'ca', 'cs', 'da', 'de', 'el', 'en-GB', 'en-US', 'es',
257				'et', 'fi', 'fr', 'he', 'hr', 'hu', 'is', 'it', 'ka', 'lt', 'mr', 'nb',
258				'nl', 'nn', 'pl', 'pt', 'ru', 'sk', 'sv', 'tr', 'uk', 'vi', 'zh-Hans',
259			);
260		}
261
262		$locales = array();
263		foreach ($codes as $code) {
264			if (file_exists(WT_ROOT . 'language/' . $code . '.mo')) {
265				try {
266					$locales[] = Locale::create($code);
267				} catch (\Exception $ex) {
268					// No such locale exists?
269				}
270			}
271		}
272		usort($locales, '\Fisharebest\Localization\Locale::compare');
273
274		return $locales;
275	}
276
277	/**
278	 * All locales for which a translation file exists.
279	 *
280	 * @return Locale[]
281	 */
282	public static function installedLocales() {
283		$locales = array();
284		foreach (glob(WT_ROOT . 'language/*.mo') as $file) {
285			try {
286				$locales[] = Locale::create(basename($file, '.mo'));
287			} catch (\Exception $ex) {
288				// No such locale exists?
289			}
290		}
291		usort($locales, '\Fisharebest\Localization\Locale::compare');
292
293		return $locales;
294	}
295
296	/**
297	 * Generate i18n markup for the <html> tag, e.g. lang="ar" dir="rtl"
298	 *
299	 * @return string
300	 */
301	public static function htmlAttributes() {
302		return self::$locale->htmlAttributes();
303	}
304
305	/**
306	 * Translate a number into the local representation.
307	 *
308	 * e.g. 12345.67 becomes
309	 * en: 12,345.67
310	 * fr: 12 345,67
311	 * de: 12.345,67
312	 *
313	 * @param float   $n
314	 * @param integer $precision
315	 *
316	 * @return string
317	 */
318	public static function number($n, $precision = 0) {
319		return self::$locale->number(round($n, $precision));
320	}
321
322	/**
323	 * Convert the digits 0-9 into the local script
324	 *
325	 * Used for years, etc., where we do not want thousands-separators, decimals, etc.
326	 *
327	 * @param integer $n
328	 *
329	 * @return string
330	 */
331	public static function digits($n) {
332		return self::$locale->digits($n);
333	}
334
335	/**
336	 * Translate a fraction into a percentage.
337	 *
338	 * e.g. 0.123 becomes
339	 * en: 12.3%
340	 * fr: 12,3 %
341	 * de: 12,3%
342	 *
343	 * @param float   $n
344	 * @param integer $precision
345	 *
346	 * @return string
347	 */
348	public static function percentage($n, $precision = 0) {
349		return
350			/* 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. */
351			self::translate('%s%%', self::number($n * 100.0, $precision));
352	}
353
354	/**
355	 * What is the direction of the current locale
356	 *
357	 * @return string "ltr" or "rtl"
358	 */
359	public static function direction() {
360		return self::$locale->direction();
361	}
362
363	/**
364	 * Translate a string, and then substitute placeholders
365	 *
366	 * echo I18N::translate('Hello World!');
367	 * echo I18N::translate('The %s sat on the mat', 'cat');
368	 *
369	 * @return string
370	 */
371	public static function translate(/* var_args */) {
372		$args = func_get_args();
373		$args[0] = self::$translation_adapter->getAdapter()->_($args[0]);
374
375		return call_user_func_array('sprintf', $args);
376	}
377
378	/**
379	 * Context sensitive version of translate.
380	 *
381	 * echo I18N::translate_c('NOMINATIVE', 'January');
382	 * echo I18N::translate_c('GENITIVE',   'January');
383	 *
384	 * @return string
385	 */
386	public static function translateContext(/* var_args */) {
387		$args = func_get_args();
388		$msgid = $args[0] . "\x04" . $args[1];
389		$msgtxt = self::$translation_adapter->getAdapter()->_($msgid);
390		if ($msgtxt === $msgid) {
391			$msgtxt = $args[1];
392		}
393		$args[0] = $msgtxt;
394		unset($args[1]);
395
396		return call_user_func_array('sprintf', $args);
397	}
398
399	/**
400	 * Similar to translate, but do perform "no operation" on it.
401	 *
402	 * This is necessary to fetch a format string (containing % characters) without
403	 * performing sustitution of arguments.
404	 *
405	 * @param string $string
406	 *
407	 * @return string
408	 */
409	public static function noop($string) {
410		return self::$translation_adapter->getAdapter()->_($string);
411	}
412
413	/**
414	 * Translate a plural string
415	 *
416	 * echo self::plural('There is an error', 'There are errors', $num_errors);
417	 * echo self::plural('There is one error', 'There are %s errors', $num_errors);
418	 * echo self::plural('There is $1$s %2$s cat', 'There are $1$s %2$s cats', $num, $num, $colour);
419	 *
420	 * @return string
421	 */
422	public static function plural(/* var_args */) {
423		$args = func_get_args();
424		$string = self::$translation_adapter->getAdapter()->plural($args[0], $args[1], $args[2]);
425		array_splice($args, 0, 3, array($string));
426
427		return call_user_func_array('sprintf', $args);
428	}
429
430	/**
431	 * Convert a GEDCOM age string into translated_text
432	 *
433	 * NB: The import function will have normalised this, so we don't need
434	 * to worry about badly formatted strings
435	 * NOTE: this function is not yet complete - eventually it will replace get_age_at_event()
436	 *
437	 * @param $string
438	 *
439	 * @return string
440	 */
441	public static function gedcomAge($string) {
442		switch ($string) {
443		case 'STILLBORN':
444			// I18N: Description of an individual’s age at an event.  For example, Died 14 Jan 1900 (stillborn)
445			return self::translate('(stillborn)');
446		case 'INFANT':
447			// I18N: Description of an individual’s age at an event.  For example, Died 14 Jan 1900 (in infancy)
448			return self::translate('(in infancy)');
449		case 'CHILD':
450			// I18N: Description of an individual’s age at an event.  For example, Died 14 Jan 1900 (in childhood)
451			return self::translate('(in childhood)');
452		}
453		$age = array();
454		if (preg_match('/(\d+)y/', $string, $match)) {
455			// I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
456			$years = $match[1];
457			$age[] = self::plural('%s year', '%s years', $years, self::number($years));
458		} else {
459			$years = -1;
460		}
461		if (preg_match('/(\d+)m/', $string, $match)) {
462			// I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
463			$age[] = self::plural('%s month', '%s months', $match[1], self::number($match[1]));
464		}
465		if (preg_match('/(\d+)w/', $string, $match)) {
466			// I18N: Part of an age string. e.g. 7 weeks and 3 days
467			$age[] = self::plural('%s week', '%s weeks', $match[1], self::number($match[1]));
468		}
469		if (preg_match('/(\d+)d/', $string, $match)) {
470			// I18N: Part of an age string. e.g. 5 years, 4 months and 3 days
471			$age[] = self::plural('%s day', '%s days', $match[1], self::number($match[1]));
472		}
473		// If an age is just a number of years, only show the number
474		if (count($age) === 1 && $years >= 0) {
475			$age = $years;
476		}
477		if ($age) {
478			if (!substr_compare($string, '<', 0, 1)) {
479				// I18N: Description of an individual’s age at an event.  For example, Died 14 Jan 1900 (aged less than 21 years)
480				return self::translate('(aged less than %s)', $age);
481			} elseif (!substr_compare($string, '>', 0, 1)) {
482				// I18N: Description of an individual’s age at an event.  For example, Died 14 Jan 1900 (aged more than 21 years)
483				return self::translate('(aged more than %s)', $age);
484			} else {
485				// I18N: Description of an individual’s age at an event.  For example, Died 14 Jan 1900 (aged 43 years)
486				return self::translate('(aged %s)', $age);
487			}
488		} else {
489			// Not a valid string?
490			return self::translate('(aged %s)', $string);
491		}
492	}
493
494	/**
495	 * Convert a number of seconds into a relative time.  For example, 630 => "10 hours, 30 minutes ago"
496	 *
497	 * @param integer $seconds
498	 *
499	 * @return string
500	 */
501	public static function timeAgo($seconds) {
502		$minute = 60;
503		$hour   = 60 * $minute;
504		$day    = 24 * $hour;
505		$month  = 30 * $day;
506		$year   = 365 * $day;
507
508		if ($seconds > $year) {
509			$years = (int) ($seconds / $year);
510			return self::plural('%s year ago', '%s years ago', $years, self::number($years));
511		} elseif ($seconds > $month) {
512			$months = (int) ($seconds / $month);
513			return self::plural('%s month ago', '%s months ago', $months, self::number($months));
514		} elseif ($seconds > $day) {
515			$days = (int) ($seconds / $day);
516			return self::plural('%s day ago', '%s days ago', $days, self::number($days));
517		} elseif ($seconds > $hour) {
518			$hours = (int) ($seconds / $hour);
519			return self::plural('%s hour ago', '%s hours ago', $hours, self::number($hours));
520		} elseif ($seconds > $minute) {
521			$minutes = (int) ($seconds / $minute);
522			return self::plural('%s minute ago', '%s minutes ago', $minutes, self::number($minutes));
523		} else {
524			return self::plural('%s second ago', '%s seconds ago', $seconds, self::number($seconds));
525		}
526	}
527
528	/**
529	 * Return the endonym for a given language - as per http://cldr.unicode.org/
530	 *
531	 * @param string $locale
532	 *
533	 * @return string
534	 */
535	public static function languageName($locale) {
536		return Locale::create($locale)->endonym();
537	}
538
539	/**
540	 * Return the script used by a given language
541	 *
542	 * @param string $locale
543	 *
544	 * @return string
545	 */
546	public static function languageScript($locale) {
547		return Locale::create($locale)->script()->code();
548	}
549
550	/**
551	 * Identify the script used for a piece of text
552	 *
553	 * @param $string
554	 *
555	 * @return string
556	 */
557	public static function textScript($string) {
558		$string = strip_tags($string); // otherwise HTML tags show up as latin
559		$string = html_entity_decode($string, ENT_QUOTES, 'UTF-8'); // otherwise HTML entities show up as latin
560		$string = str_replace(array('@N.N.', '@P.N.'), '', $string); // otherwise unknown names show up as latin
561		$pos = 0;
562		$strlen = strlen($string);
563		while ($pos < $strlen) {
564			// get the Unicode Code Point for the character at position $pos
565			$byte1 = ord($string[$pos]);
566			if ($byte1 < 0x80) {
567				$code_point = $byte1;
568				$chrlen = 1;
569			} elseif ($byte1 < 0xC0) {
570				// Invalid continuation character
571				return 'Latn';
572			} elseif ($byte1 < 0xE0) {
573				$code_point = (($byte1 & 0x1F) << 6) + (ord($string[$pos + 1]) & 0x3F);
574				$chrlen = 2;
575			} elseif ($byte1 < 0xF0) {
576				$code_point = (($byte1 & 0x0F) << 12) + ((ord($string[$pos + 1]) & 0x3F) << 6) + (ord($string[$pos + 2]) & 0x3F);
577				$chrlen = 3;
578			} elseif ($byte1 < 0xF8) {
579				$code_point = (($byte1 & 0x07) << 24) + ((ord($string[$pos + 1]) & 0x3F) << 12) + ((ord($string[$pos + 2]) & 0x3F) << 6) + (ord($string[$pos + 3]) & 0x3F);
580				$chrlen = 3;
581 			} else {
582				// Invalid UTF
583				return 'Latn';
584			}
585
586			foreach (self::$scripts as $range) {
587				if ($code_point >= $range[1] && $code_point <= $range[2]) {
588					return $range[0];
589				}
590			}
591			// Not a recognised script.  Maybe punctuation, spacing, etc.  Keep looking.
592			$pos += $chrlen;
593		}
594
595		return 'Latn';
596	}
597
598	/**
599	 * Return the direction (ltr or rtl) for a given script
600	 *
601	 * The PHP/intl library does not provde this information, so we need
602	 * our own lookup table.
603	 *
604	 * @param string $script
605	 *
606	 * @return string
607	 */
608	public static function scriptDirection($script) {
609		switch ($script) {
610		case 'Arab':
611		case 'Hebr':
612		case 'Mong':
613		case 'Thaa':
614			return 'rtl';
615		default:
616			return 'ltr';
617		}
618	}
619
620	/**
621	 * UTF8 version of PHP::strtoupper()
622	 *
623	 * Convert a string to upper case, using the rules from the current locale
624	 *
625	 * @param string $string
626	 *
627	 * @return string
628	 */
629	public static function strtoupper($string) {
630		if (self::$locale->language()->code() === 'tr' || self::$locale->language()->code() === 'az') {
631			return TurkishUtf8::strtoupper($string);
632		} else {
633			return mb_strtoupper($string);
634		}
635	}
636
637	/**
638	 * UTF8 version of PHP::strtolower()
639	 *
640	 * Convert a string to lower case, using the rules from the current locale
641	 *
642	 * @param string $string
643	 *
644	 * @return string
645	 */
646	public static function strtolower($string) {
647		if (self::$locale->language()->code() === 'tr' || self::$locale->language()->code() === 'az') {
648			return TurkishUtf8::strtolower($string);
649		} else {
650			return mb_strtolower($string);
651		}
652	}
653
654	/**
655	 * UTF8 version of PHP::strcasecmp()
656	 *
657	 * Perform a case-insensitive comparison of two strings, using rules from the current locale
658	 *
659	 * @param string $string1
660	 * @param string $string2
661	 *
662	 * @return integer
663	 */
664	public static function strcasecmp($string1, $string2) {
665		$strpos1 = 0;
666		$strpos2 = 0;
667		$strlen1 = strlen($string1);
668		$strlen2 = strlen($string2);
669		while ($strpos1 < $strlen1 && $strpos2 < $strlen2) {
670			$byte1 = ord($string1[$strpos1]);
671			$byte2 = ord($string2[$strpos2]);
672			if (($byte1 & 0xE0) === 0xC0) {
673				$chr1 = $string1[$strpos1++] . $string1[$strpos1++];
674			} elseif (($byte1 & 0xF0) === 0xE0) {
675				$chr1 = $string1[$strpos1++] . $string1[$strpos1++] . $string1[$strpos1++];
676			} else {
677				$chr1 = $string1[$strpos1++];
678			}
679			if (($byte2 & 0xE0) === 0xC0) {
680				$chr2 = $string2[$strpos2++] . $string2[$strpos2++];
681			} elseif (($byte2 & 0xF0) === 0xE0) {
682				$chr2 = $string2[$strpos2++] . $string2[$strpos2++] . $string2[$strpos2++];
683			} else {
684				$chr2 = $string2[$strpos2++];
685			}
686			if ($chr1 === $chr2) {
687				continue;
688			}
689			// Try the local alphabet first
690			$offset1 = strpos(self::$alphabet_lower, $chr1);
691			if ($offset1 === false) {
692				$offset1 = strpos(self::$alphabet_upper, $chr1);
693			}
694			$offset2 = strpos(self::$alphabet_lower, $chr2);
695			if ($offset2 === false) {
696				$offset2 = strpos(self::$alphabet_upper, $chr2);
697			}
698			if ($offset1 !== false && $offset2 !== false) {
699				if ($offset1 === $offset2) {
700					continue;
701				} else {
702					return $offset1 - $offset2;
703				}
704			}
705			// Try the global alphabet next
706			$offset1 = strpos(self::ALPHABET_LOWER, $chr1);
707			if ($offset1 === false) {
708				$offset1 = strpos(self::ALPHABET_UPPER, $chr1);
709			}
710			$offset2 = strpos(self::ALPHABET_LOWER, $chr2);
711			if ($offset2 === false) {
712				$offset2 = strpos(self::ALPHABET_UPPER, $chr2);
713			}
714			if ($offset1 !== false && $offset2 !== false) {
715				if ($offset1 === $offset2) {
716					continue;
717				} else {
718					return $offset1 - $offset2;
719				}
720			}
721			// Just compare by unicode order
722			return strcmp($chr1, $chr2);
723		}
724		// Shortest string comes first.
725		return ($strlen1 - $strpos1) - ($strlen2 - $strpos2);
726	}
727
728	/**
729	 * UTF8 version of PHP::strrev()
730	 *
731	 * Reverse RTL text for third-party libraries such as GD2 and googlechart.
732	 *
733	 * These do not support UTF8 text direction, so we must mimic it for them.
734	 *
735	 * Numbers are always rendered LTR, even in RTL text.
736	 * The visual direction of characters such as parentheses should be reversed.
737	 *
738	 * @param string $text Text to be reversed
739	 *
740	 * @return string
741	 */
742	public static function reverseText($text) {
743		// Remove HTML markup - we can't display it and it is LTR.
744		$text = Filter::unescapeHtml($text);
745
746		// LTR text doesn't need reversing
747		if (self::scriptDirection(self::textScript($text)) === 'ltr') {
748			return $text;
749		}
750
751		// Mirrored characters
752		$text = strtr($text, self::$mirror_characters);
753
754		$reversed = '';
755		$digits = '';
756		while ($text != '') {
757			$letter = mb_substr($text, 0, 1);
758			$text = mb_substr($text, 1);
759			if (strpos(self::DIGITS, $letter) !== false) {
760				$digits .= $letter;
761			} else {
762				$reversed = $letter . $digits . $reversed;
763				$digits = '';
764			}
765		}
766
767		return $digits . $reversed;
768	}
769
770	/**
771	 * Generate consistent I18N for datatables.js
772	 *
773	 * @param array|null $lengths An optional array of page lengths
774	 *
775	 * @return string
776	 */
777	public static function datatablesI18N(array $lengths = null) {
778		if ($lengths === null) {
779			$lengths = array(10, 20, 30, 50, 100, -1);
780		}
781
782		$length_menu = '';
783		foreach ($lengths as $length) {
784			$length_menu .=
785				'<option value="' . $length . '">' .
786				($length === -1 ? /* I18N: listbox option, e.g. “10,25,50,100,all” */ self::translate('All') : self::number($length)) .
787				'</option>';
788		}
789		$length_menu = '<select>' . $length_menu . '</select>';
790		$length_menu = /* I18N: Display %s [records per page], %s is a placeholder for listbox containing numeric options */ self::translate('Display %s', $length_menu);
791
792		$digits = self::$locale->digits('0123456789');
793		if ($digits === '0123456789') {
794			$callback = '';
795		} else {
796			$callback = ',
797				"infoCallback": function(oSettings, iStart, iEnd, iMax, iTotal, sPre) {
798					return sPre
799						.replace(/0/g, "' . mb_substr($digits, 0, 1) . '")
800						.replace(/1/g, "' . mb_substr($digits, 1, 1) . '")
801						.replace(/2/g, "' . mb_substr($digits, 2, 1) . '")
802						.replace(/3/g, "' . mb_substr($digits, 3, 1) . '")
803						.replace(/4/g, "' . mb_substr($digits, 4, 1) . '")
804						.replace(/5/g, "' . mb_substr($digits, 5, 1) . '")
805						.replace(/6/g, "' . mb_substr($digits, 6, 1) . '")
806						.replace(/7/g, "' . mb_substr($digits, 7, 1) . '")
807						.replace(/8/g, "' . mb_substr($digits, 8, 1) . '")
808						.replace(/9/g, "' . mb_substr($digits, 9, 1) . '");
809				},
810				"formatNumber": function(iIn) {
811					return String(iIn)
812						.replace(/0/g, "' . mb_substr($digits, 0, 1) . '")
813						.replace(/1/g, "' . mb_substr($digits, 1, 1) . '")
814						.replace(/2/g, "' . mb_substr($digits, 2, 1) . '")
815						.replace(/3/g, "' . mb_substr($digits, 3, 1) . '")
816						.replace(/4/g, "' . mb_substr($digits, 4, 1) . '")
817						.replace(/5/g, "' . mb_substr($digits, 5, 1) . '")
818						.replace(/6/g, "' . mb_substr($digits, 6, 1) . '")
819						.replace(/7/g, "' . mb_substr($digits, 7, 1) . '")
820						.replace(/8/g, "' . mb_substr($digits, 8, 1) . '")
821						.replace(/9/g, "' . mb_substr($digits, 9, 1) . '");
822				}
823			';
824		}
825
826		return
827			'"language": {' .
828			' "paginate": {' .
829			'  "first":    "' . /* I18N: button label, first page    */ self::translate('first') . '",' .
830			'  "last":     "' . /* I18N: button label, last page     */ self::translate('last') . '",' .
831			'  "next":     "' . /* I18N: button label, next page     */ self::translate('next') . '",' .
832			'  "previous": "' . /* I18N: button label, previous page */ self::translate('previous') . '"' .
833			' },' .
834			' "emptyTable":     "' . self::translate('No records to display') . '",' .
835			' "info":           "' . /* I18N: %s are placeholders for numbers */ self::translate('Showing %1$s to %2$s of %3$s', '_START_', '_END_', '_TOTAL_') . '",' .
836			' "infoEmpty":      "' . self::translate('Showing %1$s to %2$s of %3$s', 0, 0, 0) . '",' .
837			' "infoFiltered":   "' . /* I18N: %s is a placeholder for a number */ self::translate('(filtered from %s total entries)', '_MAX_') . '",' .
838			' "infoPostfix":    "",' .
839			' "lengthMenu":     "' . Filter::escapeJs($length_menu) . '",' .
840			' "loadingRecords": "' . self::translate('Loading…') . '",' .
841			' "processing":     "' . self::translate('Loading…') . '",' .
842			' "search":         "' . self::translate('Filter') . '",' .
843			' "url":            "",' .
844			' "zeroRecords":    "' . self::translate('No records to display') . '"' .
845			'}' .
846			$callback;
847	}
848}
849