UNPKG

ilib

Version:

iLib is a cross-engine library of internationalization (i18n) classes written in pure JS

3 lines 9.78 kB
var ilib=require("./ilib.js"),JSUtils=require("./JSUtils.js"),Locale=function(language,region,variant,script){if(void 0===region&&void 0===variant&&void 0===script){var spec=language||ilib.getLocale();if("string"==typeof spec){for(var parts=spec.split(/[-_]/g),i=0;i<parts.length;i++)Locale._isLanguageCode(parts[i])?this.language=parts[i]:Locale._isRegionCode(parts[i])?this.region=parts[i]:Locale._isScriptCode(parts[i])?this.script=parts[i]:this.variant=parts[i];this.language=this.language||void 0,this.region=this.region||void 0,this.script=this.script||void 0,this.variant=this.variant||void 0}else"object"==typeof spec&&(this.language=spec.language||void 0,this.region=spec.region||void 0,this.script=spec.script||void 0,this.variant=spec.variant||void 0)}else language&&"string"==typeof language?(language=language.trim(),this.language=0<language.length?language.toLowerCase():void 0):this.language=void 0,region&&"string"==typeof region?(region=region.trim(),this.region=0<region.length?region.toUpperCase():void 0):this.region=void 0,variant&&"string"==typeof variant?(variant=variant.trim(),this.variant=0<variant.length?variant:void 0):this.variant=void 0,script&&"string"==typeof script?(script=script.trim(),this.script=0<script.length?script:void 0):this.script=void 0;this._genSpec()};Locale.a2toa3regmap={AF:"AFG",AX:"ALA",AL:"ALB",DZ:"DZA",AS:"ASM",AD:"AND",AO:"AGO",AI:"AIA",AQ:"ATA",AG:"ATG",AR:"ARG",AM:"ARM",AW:"ABW",AU:"AUS",AT:"AUT",AZ:"AZE",BS:"BHS",BH:"BHR",BD:"BGD",BB:"BRB",BY:"BLR",BE:"BEL",BZ:"BLZ",BJ:"BEN",BM:"BMU",BT:"BTN",BO:"BOL",BQ:"BES",BA:"BIH",BW:"BWA",BV:"BVT",BR:"BRA",IO:"IOT",BN:"BRN",BG:"BGR",BF:"BFA",BI:"BDI",KH:"KHM",CM:"CMR",CA:"CAN",CV:"CPV",KY:"CYM",CF:"CAF",TD:"TCD",CL:"CHL",CN:"CHN",CX:"CXR",CC:"CCK",CO:"COL",KM:"COM",CG:"COG",CD:"COD",CK:"COK",CR:"CRI",CI:"CIV",HR:"HRV",CU:"CUB",CW:"CUW",CY:"CYP",CZ:"CZE",DK:"DNK",DJ:"DJI",DM:"DMA",DO:"DOM",EC:"ECU",EG:"EGY",SV:"SLV",GQ:"GNQ",ER:"ERI",EE:"EST",ET:"ETH",FK:"FLK",FO:"FRO",FJ:"FJI",FI:"FIN",FR:"FRA",GF:"GUF",PF:"PYF",TF:"ATF",GA:"GAB",GM:"GMB",GE:"GEO",DE:"DEU",GH:"GHA",GI:"GIB",GR:"GRC",GL:"GRL",GD:"GRD",GP:"GLP",GU:"GUM",GT:"GTM",GG:"GGY",GN:"GIN",GW:"GNB",GY:"GUY",HT:"HTI",HM:"HMD",VA:"VAT",HN:"HND",HK:"HKG",HU:"HUN",IS:"ISL",IN:"IND",ID:"IDN",IR:"IRN",IQ:"IRQ",IE:"IRL",IM:"IMN",IL:"ISR",IT:"ITA",JM:"JAM",JP:"JPN",JE:"JEY",JO:"JOR",KZ:"KAZ",KE:"KEN",KI:"KIR",KP:"PRK",KR:"KOR",KW:"KWT",KG:"KGZ",LA:"LAO",LV:"LVA",LB:"LBN",LS:"LSO",LR:"LBR",LY:"LBY",LI:"LIE",LT:"LTU",LU:"LUX",MO:"MAC",MK:"MKD",MG:"MDG",MW:"MWI",MY:"MYS",MV:"MDV",ML:"MLI",MT:"MLT",MH:"MHL",MQ:"MTQ",MR:"MRT",MU:"MUS",YT:"MYT",MX:"MEX",FM:"FSM",MD:"MDA",MC:"MCO",MN:"MNG",ME:"MNE",MS:"MSR",MA:"MAR",MZ:"MOZ",MM:"MMR",NA:"NAM",NR:"NRU",NP:"NPL",NL:"NLD",NC:"NCL",NZ:"NZL",NI:"NIC",NE:"NER",NG:"NGA",NU:"NIU",NF:"NFK",MP:"MNP",NO:"NOR",OM:"OMN",PK:"PAK",PW:"PLW",PS:"PSE",PA:"PAN",PG:"PNG",PY:"PRY",PE:"PER",PH:"PHL",PN:"PCN",PL:"POL",PT:"PRT",PR:"PRI",QA:"QAT",RE:"REU",RO:"ROU",RU:"RUS",RW:"RWA",BL:"BLM",SH:"SHN",KN:"KNA",LC:"LCA",MF:"MAF",PM:"SPM",VC:"VCT",WS:"WSM",SM:"SMR",ST:"STP",SA:"SAU",SN:"SEN",RS:"SRB",SC:"SYC",SL:"SLE",SG:"SGP",SX:"SXM",SK:"SVK",SI:"SVN",SB:"SLB",SO:"SOM",ZA:"ZAF",GS:"SGS",SS:"SSD",ES:"ESP",LK:"LKA",SD:"SDN",SR:"SUR",SJ:"SJM",SZ:"SWZ",SE:"SWE",CH:"CHE",SY:"SYR",TW:"TWN",TJ:"TJK",TZ:"TZA",TH:"THA",TL:"TLS",TG:"TGO",TK:"TKL",TO:"TON",TT:"TTO",TN:"TUN",TR:"TUR",TM:"TKM",TC:"TCA",TV:"TUV",UG:"UGA",UA:"UKR",AE:"ARE",GB:"GBR",US:"USA",UM:"UMI",UY:"URY",UZ:"UZB",VU:"VUT",VE:"VEN",VN:"VNM",VG:"VGB",VI:"VIR",WF:"WLF",EH:"ESH",YE:"YEM",ZM:"ZMB",ZW:"ZWE"},Locale.a1toa3langmap={ab:"abk",aa:"aar",af:"afr",ak:"aka",sq:"sqi",am:"amh",ar:"ara",an:"arg",hy:"hye","as":"asm",av:"ava",ae:"ave",ay:"aym",az:"aze",bm:"bam",ba:"bak",eu:"eus",be:"bel",bn:"ben",bh:"bih",bi:"bis",bs:"bos",br:"bre",bg:"bul",my:"mya",ca:"cat",ch:"cha",ce:"che",ny:"nya",zh:"zho",cv:"chv",kw:"cor",co:"cos",cr:"cre",hr:"hrv",cs:"ces",da:"dan",dv:"div",nl:"nld",dz:"dzo",en:"eng",eo:"epo",et:"est",ee:"ewe",fo:"fao",fj:"fij",fi:"fin",fr:"fra",ff:"ful",gl:"glg",ka:"kat",de:"deu",el:"ell",gn:"grn",gu:"guj",ht:"hat",ha:"hau",he:"heb",hz:"her",hi:"hin",ho:"hmo",hu:"hun",ia:"ina",id:"ind",ie:"ile",ga:"gle",ig:"ibo",ik:"ipk",io:"ido",is:"isl",it:"ita",iu:"iku",ja:"jpn",jv:"jav",kl:"kal",kn:"kan",kr:"kau",ks:"kas",kk:"kaz",km:"khm",ki:"kik",rw:"kin",ky:"kir",kv:"kom",kg:"kon",ko:"kor",ku:"kur",kj:"kua",la:"lat",lb:"ltz",lg:"lug",li:"lim",ln:"lin",lo:"lao",lt:"lit",lu:"lub",lv:"lav",gv:"glv",mk:"mkd",mg:"mlg",ms:"msa",ml:"mal",mt:"mlt",mi:"mri",mr:"mar",mh:"mah",mn:"mon",na:"nau",nv:"nav",nb:"nob",nd:"nde",ne:"nep",ng:"ndo",nn:"nno",no:"nor",ii:"iii",nr:"nbl",oc:"oci",oj:"oji",cu:"chu",om:"orm",or:"ori",os:"oss",pa:"pan",pi:"pli",fa:"fas",pl:"pol",ps:"pus",pt:"por",qu:"que",rm:"roh",rn:"run",ro:"ron",ru:"rus",sa:"san",sc:"srd",sd:"snd",se:"sme",sm:"smo",sg:"sag",sr:"srp",gd:"gla",sn:"sna",si:"sin",sk:"slk",sl:"slv",so:"som",st:"sot",es:"spa",su:"sun",sw:"swa",ss:"ssw",sv:"swe",ta:"tam",te:"tel",tg:"tgk",th:"tha",ti:"tir",bo:"bod",tk:"tuk",tl:"tgl",tn:"tsn",to:"ton",tr:"tur",ts:"tso",tt:"tat",tw:"twi",ty:"tah",ug:"uig",uk:"ukr",ur:"urd",uz:"uzb",ve:"ven",vi:"vie",vo:"vol",wa:"wln",cy:"cym",wo:"wol",fy:"fry",xh:"xho",yi:"yid",yo:"yor",za:"zha",zu:"zul"},Locale.iso15924=["Adlm","Afak","Aghb","Ahom","Arab","Aran","Armi","Armn","Avst","Bali","Bamu","Bass","Batk","Beng","Bhks","Blis","Bopo","Brah","Brai","Bugi","Buhd","Cakm","Cans","Cari","Cham","Cher","Chrs","Cirt","Copt","Cpmn","Cprt","Cyrl","Cyrs","Deva","Diak","Dogr","Dsrt","Dupl","Egyd","Egyh","Egyp","Elba","Elym","Ethi","Geok","Geor","Glag","Gong","Gonm","Goth","Gran","Grek","Gujr","Guru","Hanb","Hang","Hani","Hano","Hans","Hant","Hatr","Hebr","Hira","Hluw","Hmng","Hmnp","Hrkt","Hung","Inds","Ital","Jamo","Java","Jpan","Jurc","Kali","Kana","Khar","Khmr","Khoj","Kitl","Kits","Knda","Kore","Kpel","Kthi","Lana","Laoo","Latf","Latg","Latn","Leke","Lepc","Limb","Lina","Linb","Lisu","Loma","Lyci","Lydi","Mahj","Maka","Mand","Mani","Marc","Maya","Medf","Mend","Merc","Mero","Mlym","Modi","Mong","Moon","Mroo","Mtei","Mult","Mymr","Nand","Narb","Nbat","Newa","Nkdb","Nkgb","Nkoo","Nshu","Ogam","Olck","Orkh","Orya","Osge","Osma","Palm","Pauc","Perm","Phag","Phli","Phlp","Phlv","Phnx","Plrd","Piqd","Prti","Qaaa","Qabx","Rjng","Rohg","Roro","Runr","Samr","Sara","Sarb","Saur","Sgnw","Shaw","Shrd","Shui","Sidd","Sind","Sinh","Sogd","Sogo","Sora","Soyo","Sund","Sylo","Syrc","Syre","Syrj","Syrn","Tagb","Takr","Tale","Talu","Taml","Tang","Tavt","Telu","Teng","Tfng","Tglg","Thaa","Thai","Tibt","Tirh","Toto","Ugar","Vaii","Visp","Wara","Wcho","Wole","Xpeo","Xsux","Yezi","Yiii","Zanb","Zinh","Zmth","Zsye","Zsym","Zxxx","Zyyy","Zzzz"],Locale._notLower=function(str){str=str.charCodeAt(0);return str<97||122<str},Locale._notUpper=function(str){str=str.charCodeAt(0);return str<65||90<str},Locale._notDigit=function(str){str=str.charCodeAt(0);return str<48||57<str},Locale._isLanguageCode=function(str){if(void 0===str||str.length<2||3<str.length)return!1;for(var i=0;i<str.length;i++)if(Locale._notLower(str.charAt(i)))return!1;return!0},Locale._isRegionCode=function(str){var i;if(void 0===str||str.length<2||3<str.length)return!1;if(2===str.length){for(i=0;i<str.length;i++)if(Locale._notUpper(str.charAt(i)))return!1}else for(i=0;i<str.length;i++)if(Locale._notDigit(str.charAt(i)))return!1;return!0},Locale._isScriptCode=function(str){if(void 0===str||4!==str.length||Locale._notUpper(str.charAt(0)))return!1;for(var i=1;i<4;i++)if(Locale._notLower(str.charAt(i)))return!1;return!0},Locale.regionAlpha2ToAlpha3=function(alpha2){return Locale.a2toa3regmap[alpha2]||alpha2},Locale.languageAlpha1ToAlpha3=function(alpha1){return Locale.a1toa3langmap[alpha1]||alpha1},Locale.prototype={_genSpec:function(){this.spec=this.language||"",this.script&&(0<this.spec.length&&(this.spec+="-"),this.spec+=this.script),this.region&&(0<this.spec.length&&(this.spec+="-"),this.spec+=this.region),this.variant&&(0<this.spec.length&&(this.spec+="-"),this.spec+=this.variant)},getLanguage:function(){return this.language},getLanguageAlpha3:function(){return Locale.languageAlpha1ToAlpha3(this.language)},getRegion:function(){return this.region},getRegionAlpha3:function(){return Locale.regionAlpha2ToAlpha3(this.region)},getScript:function(){return this.script},getVariant:function(){return this.variant},getSpec:function(){return this.spec||this._genSpec(),this.spec},getLangSpec:function(){var spec=this.language;return spec&&this.script&&(spec+="-"+this.script),spec||""},toString:function(){return this.getSpec()},equals:function(other){return this.language===other.language&&this.region===other.region&&this.script===other.script&&this.variant===other.variant},isPseudo:function(){return-1<JSUtils.indexOf(ilib.pseudoLocales,this.spec)},isValid:function(){return!!(this.language||this.script||this.region)&&!(!(!this.language||Locale._isLanguageCode(this.language)&&Locale.a1toa3langmap[this.language])||this.script&&!(Locale._isScriptCode(this.script)&&-1<Locale.iso15924.indexOf(this.script))||!(!this.region||Locale._isRegionCode(this.region)&&Locale.a2toa3regmap[this.region]))}},Locale.locales=[], // !macro localelist Locale.getAvailableLocales=function(sync,onLoad){var locales=[];return Locale.locales.length||"function"!=typeof ilib._load.listAvailableFiles?(locales=Locale.locales,onLoad&&"function"==typeof onLoad&&onLoad(locales)):ilib._load.listAvailableFiles(sync=void 0===sync?!0:sync,function(manifest){if(manifest)for(var dir in manifest)for(var filelist=manifest[dir],i=0;i<filelist.length;i++)15<filelist[i].length&&"localeinfo.json"===filelist[i].substr(-15)&&locales.push(filelist[i].substring(0,filelist[i].length-16).replace(/\//g,"-"));onLoad&&"function"==typeof onLoad&&onLoad(locales)}),locales},module.exports=Locale;