UNPKG

ilib

Version:

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

2 lines 6.13 kB
// !data dateformats sysres var ilib=require("../index.js"),JSUtils=require("./JSUtils.js"),Locale=require("./Locale.js"),LocaleInfo=require("./LocaleInfo.js"),DateFmt=require("./DateFmt.js"),IString=require("./IString.js"),ResBundle=require("./ResBundle.js"),ScriptInfo=require("./ScriptInfo.js"),DurationFmt=function(options){var sync=!0,loadParams=void 0;this.locale=new Locale(),this.length="short",this.style="text",options&&(options.locale&&(this.locale="string"==typeof options.locale?new Locale(options.locale):options.locale),!options.length||"short"!==options.length&&"medium"!==options.length&&"long"!==options.length&&"full"!==options.length||(this.length=options.length),!options.style||"text"!==options.style&&"clock"!==options.style||(this.style=options.style),void 0!==options.sync&&(sync=!!options.sync),"boolean"==typeof options.useNative&&(this.useNative=options.useNative),loadParams=options.loadParams),options=options||{sync:!0},new LocaleInfo(this.locale,{sync:sync,loadParams:loadParams,onLoad:ilib.bind(this,function(li){this.script=li.getScript(),new ResBundle({locale:this.locale,name:"sysres",sync:sync,loadParams:loadParams,onLoad:ilib.bind(this,function(sysres){IString.loadPlurals(sync,this.locale,loadParams,ilib.bind(this,function(){switch("medium"===this.length&&"Latn"!==this.script&&"Grek"!==this.script&&"Cyrl"!==this.script&&(this.length="short"),this.length){case"short":this.components={year:sysres.getString("#{num}y"),month:sysres.getString("#{num}m","durationShortMonths"),week:sysres.getString("#{num}w"),day:sysres.getString("#{num}d"),hour:sysres.getString("#{num}h"),minute:sysres.getString("#{num}m","durationShortMinutes"),second:sysres.getString("#{num}s"),millisecond:sysres.getString("#{num}m","durationShortMillis"),separator:sysres.getString(" ","separatorShort"),finalSeparator:""};break;case"medium":this.components={year:sysres.getString("1#1 yr|#{num} yrs","durationMediumYears"),month:sysres.getString("1#1 mo|#{num} mos"),week:sysres.getString("1#1 wk|#{num} wks","durationMediumWeeks"),day:sysres.getString("1#1 dy|#{num} dys"),hour:sysres.getString("1#1 hr|#{num} hrs","durationMediumHours"),minute:sysres.getString("1#1 mi|#{num} min"),second:sysres.getString("1#1 se|#{num} sec"),millisecond:sysres.getString("#{num} ms","durationMediumMillis"),separator:sysres.getString(" ","separatorMedium"),finalSeparator:""};break;case"long":this.components={year:sysres.getString("1#1 yr|#{num} yrs"),month:sysres.getString("1#1 mon|#{num} mons"),week:sysres.getString("1#1 wk|#{num} wks"),day:sysres.getString("1#1 day|#{num} days","durationLongDays"),hour:sysres.getString("1#1 hr|#{num} hrs"),minute:sysres.getString("1#1 min|#{num} min"),second:sysres.getString("1#1 sec|#{num} sec"),millisecond:sysres.getString("#{num} ms"),separator:sysres.getString(", ","separatorLong"),finalSeparator:""};break;case"full":this.components={year:sysres.getString("1#1 year|#{num} years"),month:sysres.getString("1#1 month|#{num} months"),week:sysres.getString("1#1 week|#{num} weeks"),day:sysres.getString("1#1 day|#{num} days"),hour:sysres.getString("1#1 hour|#{num} hours"),minute:sysres.getString("1#1 minute|#{num} minutes"),second:sysres.getString("1#1 second|#{num} seconds"),millisecond:sysres.getString("1#1 millisecond|#{num} milliseconds"),separator:sysres.getString(", ","separatorFull"),finalSeparator:sysres.getString(" and ","finalSeparatorFull")}}"clock"===this.style?new DateFmt({locale:this.locale,calendar:"gregorian",type:"time",time:"ms",sync:sync,loadParams:loadParams,useNative:this.useNative,onLoad:ilib.bind(this,function(fmtMS){this.timeFmtMS=fmtMS,new DateFmt({locale:this.locale,calendar:"gregorian",type:"time",time:"hm",sync:sync,loadParams:loadParams,useNative:this.useNative,onLoad:ilib.bind(this,function(fmtHM){this.timeFmtHM=fmtHM,new DateFmt({locale:this.locale,calendar:"gregorian",type:"time",time:"hms",sync:sync,loadParams:loadParams,useNative:this.useNative,onLoad:ilib.bind(this,function(fmtHMS){this.timeFmtHMS=fmtHMS,this.timeFmtHM.template=this.timeFmtHM.template.replace(/hh?/,"H"),this.timeFmtHM.templateArr=this.timeFmtHM._tokenize(this.timeFmtHM.template),this.timeFmtHMS.template=this.timeFmtHMS.template.replace(/hh?/,"H"),this.timeFmtHMS.templateArr=this.timeFmtHMS._tokenize(this.timeFmtHMS.template),this._init(this.timeFmtHM.locinfo,options)})})})})})}):this._init(li,options)}))})})})})};DurationFmt.complist={text:["year","month","week","day","hour","minute","second","millisecond"],clock:["year","month","week","day"]},DurationFmt.prototype._mapDigits=function(str){return this.useNative&&this.digits?JSUtils.mapString(str.toString(),this.digits):str},DurationFmt.prototype._init=function(locinfo,options){var digits;new ScriptInfo(locinfo.getScript(),{sync:options.sync,loadParams:options.loadParams,onLoad:ilib.bind(this,function(scriptInfo){this.scriptDirection=scriptInfo.getScriptDirection(),"boolean"==typeof this.useNative?this.useNative&&(digits=locinfo.getNativeDigits())&&(this.digits=digits):"native"===locinfo.getDigitsStyle()&&(digits=locinfo.getNativeDigits())&&(this.useNative=!0,this.digits=digits),"function"==typeof options.onLoad&&options.onLoad(this)})})},DurationFmt.prototype.format=function(components){for(var fmt,secondlast=!0,str="",list=DurationFmt.complist[this.style],i=list.length-1;0<=i;i--)void 0!==components[list[i]]&&0!==components[list[i]]&&(0<str.length&&(str=("full"===this.length&&secondlast?this.components.finalSeparator:this.components.separator)+str,secondlast=!1),str=this.components[list[i]].formatChoice(components[list[i]],{num:this._mapDigits(components[list[i]])})+str);return"clock"===this.style&&(fmt=void 0!==components.hour?void 0!==components.second?this.timeFmtHMS:this.timeFmtHM:this.timeFmtMS,0<str.length&&(str+=this.components.separator),str+=fmt._formatTemplate(components,fmt.templateArr)),"rtl"===this.scriptDirection&&(str="‏"+str),new IString(str)},DurationFmt.prototype.getLocale=function(){return this.locale},DurationFmt.prototype.getLength=function(){return this.length},DurationFmt.prototype.getStyle=function(){return this.style},module.exports=DurationFmt;