UNPKG

ilib

Version:

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

1 lines 7.52 kB
var ilib=require("./ilib.js");var JSUtils=require("./JSUtils.js");var Locale=function(e,i,n,t){if(typeof i==="undefined"){var a=e||ilib.getLocale();if(typeof a==="string"){var r=a.split("-");for(var o=0;o<r.length;o++){if(Locale._isLanguageCode(r[o])){this.language=r[o]}else if(Locale._isRegionCode(r[o])){this.region=r[o]}else if(Locale._isScriptCode(r[o])){this.script=r[o]}else{this.variant=r[o]}}this.language=this.language||undefined;this.region=this.region||undefined;this.script=this.script||undefined;this.variant=this.variant||undefined}else if(typeof a==="object"){this.language=a.language||undefined;this.region=a.region||undefined;this.script=a.script||undefined;this.variant=a.variant||undefined}}else{if(e){e=e.trim();this.language=e.length>0?e.toLowerCase():undefined}else{this.language=undefined}if(i){i=i.trim();this.region=i.length>0?i.toUpperCase():undefined}else{this.region=undefined}if(n){n=n.trim();this.variant=n.length>0?n:undefined}else{this.variant=undefined}if(t){t=t.trim();this.script=t.length>0?t:undefined}else{this.script=undefined}}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",az:"azb",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._notLower=function(e){var i=e.charCodeAt(0);return i<97||i>122};Locale._notUpper=function(e){var i=e.charCodeAt(0);return i<65||i>90};Locale._notDigit=function(e){var i=e.charCodeAt(0);return i<48||i>57};Locale._isLanguageCode=function(e){if(typeof e==="undefined"||e.length<2||e.length>3){return false}for(var i=0;i<e.length;i++){if(Locale._notLower(e.charAt(i))){return false}}return true};Locale._isRegionCode=function(e){if(typeof e==="undefined"||e.length<2||e.length>3){return false}if(e.length===2){for(var i=0;i<e.length;i++){if(Locale._notUpper(e.charAt(i))){return false}}}else{for(var i=0;i<e.length;i++){if(Locale._notDigit(e.charAt(i))){return false}}}return true};Locale._isScriptCode=function(e){if(typeof e==="undefined"||e.length!==4||Locale._notUpper(e.charAt(0))){return false}for(var i=1;i<4;i++){if(Locale._notLower(e.charAt(i))){return false}}return true};Locale.regionAlpha2ToAlpha3=function(e){return Locale.a2toa3regmap[e]||e};Locale.languageAlpha1ToAlpha3=function(e){return Locale.a1toa3langmap[e]||e};Locale.prototype={_genSpec:function(){this.spec=this.language||"";if(this.script){if(this.spec.length>0){this.spec+="-"}this.spec+=this.script}if(this.region){if(this.spec.length>0){this.spec+="-"}this.spec+=this.region}if(this.variant){if(this.spec.length>0){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},toString:function(){return this.getSpec()},equals:function(e){return this.language===e.language&&this.region===e.region&&this.script===e.script&&this.variant===e.variant},isPseudo:function(){return JSUtils.indexOf(ilib.pseudoLocales,this.spec)>-1}};Locale.locales=[];Locale.getAvailableLocales=function(e,i){var n=[];if(Locale.locales.length||typeof ilib._load.listAvailableFiles!=="function"){n=Locale.locales;if(i&&typeof i==="function"){i(n)}}else{if(typeof e==="undefined"){e=true}ilib._load.listAvailableFiles(e,function(e){if(e){for(var t in e){var a=e[t];for(var r=0;r<a.length;r++){if(a[r].length>15&&a[r].substr(-15)==="localeinfo.json"){n.push(a[r].substring(0,a[r].length-16).replace(/\//g,"-"))}}}}if(i&&typeof i==="function"){i(n)}})}return n};module.exports=Locale;