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