UIString.js 1.4 KB

123456789101112
  1. export function UIString(string,vararg){return String.vsprintf(Common.localize(string),Array.prototype.slice.call(arguments,1));}
  2. export function serializeUIString(string,values=[]){const messageParts=[string];const serializedMessage={messageParts,values};return JSON.stringify(serializedMessage);}
  3. export function deserializeUIString(serializedMessage){if(!serializedMessage){return{};}
  4. return JSON.parse(serializedMessage);}
  5. export function localize(string){return string;}
  6. export class UIStringFormat{constructor(format){this._localizedFormat=localize(format);this._tokenizedFormat=String.tokenizeFormatString(this._localizedFormat,String.standardFormatters);}
  7. static _append(a,b){return a+b;}
  8. format(vararg){return String.format(this._localizedFormat,arguments,String.standardFormatters,'',UIStringFormat._append,this._tokenizedFormat).formattedResult;}}
  9. const _substitutionStrings=new WeakMap();export function ls(strings,vararg){if(typeof strings==='string'){return strings;}
  10. let substitutionString=_substitutionStrings.get(strings);if(!substitutionString){substitutionString=strings.join('%s');_substitutionStrings.set(strings,substitutionString);}
  11. return UIString(substitutionString,...Array.prototype.slice.call(arguments,1));}
  12. self.ls=ls;self.Common=self.Common||{};Common=Common||{};Common.UIStringFormat=UIStringFormat;Common.UIString=UIString;Common.serializeUIString=serializeUIString;Common.deserializeUIString=deserializeUIString;Common.localize=localize;