UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 1.88 kB
import*as IntlMessageFormat from"../intl-messageformat/intl-messageformat.js";const EMPTY_VALUES_OBJECT={};export class RegisteredFileStrings{filename;stringStructure;localizedMessages;localizedStringSet;constructor(e,t,r){this.filename=e,this.stringStructure=t,this.localizedMessages=r}getLocalizedStringSetFor(e){if(this.localizedStringSet)return this.localizedStringSet;const t=this.localizedMessages.get(e);if(!t)throw new Error(`No locale data registered for '${e}'`);return this.localizedStringSet=new LocalizedStringSet(this.filename,this.stringStructure,e,t),this.localizedStringSet}}export class LocalizedStringSet{filename;stringStructure;localizedMessages;cachedSimpleStrings=new Map;cachedMessageFormatters=new Map;localeForFormatter;constructor(e,t,r,s){this.filename=e,this.stringStructure=t,this.localizedMessages=s,this.localeForFormatter="en-XA"===r||"en-XL"===r?"de-DE":r}getLocalizedString(e,t=EMPTY_VALUES_OBJECT){return t===EMPTY_VALUES_OBJECT||0===Object.keys(t).length?this.getSimpleLocalizedString(e):this.getFormattedLocalizedString(e,t)}getMessageFormatterFor(e){const t=Object.keys(this.stringStructure).find((t=>this.stringStructure[t]===e));if(!t)throw new Error(`Unable to locate '${e}' in UIStrings object`);const r=`${this.filename} | ${t}`,s=this.localizedMessages[r],i=s?s.message:e;return new IntlMessageFormat.IntlMessageFormat(i,this.localeForFormatter,void 0,{ignoreTag:!0})}getSimpleLocalizedString(e){const t=this.cachedSimpleStrings.get(e);if(t)return t;const r=this.getMessageFormatterFor(e).format();return this.cachedSimpleStrings.set(e,r),r}getFormattedLocalizedString(e,t){let r=this.cachedMessageFormatters.get(e);r||(r=this.getMessageFormatterFor(e),this.cachedMessageFormatters.set(e,r));try{return r.format(t)}catch(r){return new IntlMessageFormat.IntlMessageFormat(e,this.localeForFormatter,void 0,{ignoreTag:!0}).format(t)}}}