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