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