{"version":3,"file":"ngx-translate-messageformat-compiler.mjs","sources":["../../src/lib/message-format-config.ts","../../src/lib/translate-message-format-compiler.ts","../../src/lib/translate-message-format-debug-compiler.ts","../../src/public-api.ts","../../src/ngx-translate-messageformat-compiler.ts"],"sourcesContent":["import { InjectionToken } from \"@angular/core\";\nimport { CustomFormatter } from \"@messageformat/core\";\n\nexport const MESSAGE_FORMAT_CONFIG = new InjectionToken<MessageFormatConfig>(\n  \"MESSAGE_FORMAT_CONFIG\",\n);\n\nexport interface MessageFormatConfig {\n  biDiSupport?: boolean;\n  formatters?: {\n    [key: string]: CustomFormatter;\n  };\n  strictNumberSign?: boolean;\n  currency?: string;\n  strictPluralKeys?: boolean;\n  throwOnError?: boolean;\n  fallbackPrefix?: string;\n}\n\nexport const defaultConfig: MessageFormatConfig = {\n  biDiSupport: false,\n  formatters: {},\n  strictNumberSign: false,\n  currency: \"USD\",\n  strictPluralKeys: true,\n  throwOnError: false,\n  fallbackPrefix: undefined,\n};\n","import { Inject, Injectable, Optional } from \"@angular/core\";\nimport { TranslateCompiler } from \"@ngx-translate/core\";\nimport MessageFormat, {\n  MessageFormatOptions,\n  MessageFunction,\n} from \"@messageformat/core\";\nimport {\n  defaultConfig,\n  MessageFormatConfig,\n  MESSAGE_FORMAT_CONFIG,\n} from \"./message-format-config\";\n\nexport type CompilationResult = MessageFunction<\"string\"> | string;\n\n/**\n * This compiler expects ICU syntax and compiles the expressions with messageformat.js\n */\n@Injectable()\nexport class TranslateMessageFormatCompiler extends TranslateCompiler {\n  private mfCache = new Map<string, MessageFormat>();\n  private messageFormatOptions: MessageFormatOptions<\"string\">;\n  private throwOnError: boolean;\n  private fallbackPrefix?: string;\n\n  constructor(\n    @Optional()\n    @Inject(MESSAGE_FORMAT_CONFIG)\n    config?: MessageFormatConfig,\n  ) {\n    super();\n\n    const {\n      formatters: customFormatters,\n      biDiSupport,\n      strictNumberSign: strict,\n      currency,\n      strictPluralKeys,\n      throwOnError,\n      fallbackPrefix,\n    } = {\n      ...defaultConfig,\n      ...config,\n    };\n\n    this.messageFormatOptions = {\n      customFormatters,\n      biDiSupport,\n      strict,\n      currency,\n      strictPluralKeys,\n    };\n    this.throwOnError = !!throwOnError;\n    this.fallbackPrefix = fallbackPrefix;\n  }\n\n  public compile<Result extends CompilationResult = MessageFunction<\"string\">>(\n    value: string,\n    lang: string,\n  ): Result {\n    if (this.fallbackPrefix && value.startsWith(this.fallbackPrefix)) {\n      return value.slice(this.fallbackPrefix.length) as Result;\n    }\n\n    let result: MessageFunction<\"string\">;\n\n    try {\n      result = this.getMessageFormatInstance(lang).compile(value);\n    } catch (err) {\n      if (this.throwOnError) {\n        throw err;\n      }\n\n      console.error(err);\n      console.error(\n        `[ngx-translate-messageformat-compiler] Could not compile message for lang '${lang}': '${value}'`,\n      );\n      result = compileFallback(value, lang);\n    }\n\n    if (!this.throwOnError) {\n      result = wrapInterpolationFunction(result, value);\n    }\n\n    return result as Result;\n  }\n\n  public compileTranslations(translations: any, lang: string): any {\n    if (typeof translations === \"string\") {\n      return this.compile(translations, lang);\n    }\n\n    return Object.keys(translations).reduce<{ [key: string]: any }>(\n      (acc, key) => {\n        const value = translations[key];\n        acc[key] = this.compileTranslations(value, lang);\n        return acc;\n      },\n      {},\n    );\n  }\n\n  private getMessageFormatInstance(locale: string): MessageFormat {\n    if (!this.mfCache.has(locale)) {\n      this.mfCache.set(\n        locale,\n        new MessageFormat<\"string\">(locale, this.messageFormatOptions),\n      );\n    }\n\n    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n    return this.mfCache.get(locale)!;\n  }\n}\n\nfunction wrapInterpolationFunction(\n  fn: MessageFunction<\"string\">,\n  message: string,\n): MessageFunction<\"string\"> {\n  return (params: any) => {\n    let result: string = message;\n\n    try {\n      result = fn(params);\n    } catch (err) {\n      console.error(err);\n      console.error(\n        `[ngx-translate-messageformat-compiler] Could not interpolate '${message}' with params '${params}'`,\n      );\n    }\n\n    return result;\n  };\n}\n\nfunction compileFallback(\n  message: string,\n  lang: string,\n): MessageFunction<\"string\"> {\n  return () => {\n    console.warn(\n      `[ngx-translate-messageformat-compiler] Falling back to original invalid message: '${message}' ('${lang}')`,\n    );\n\n    return String(message);\n  };\n}\n","import { Injectable } from \"@angular/core\";\nimport { MessageFunction } from \"@messageformat/core\";\nimport {\n  CompilationResult,\n  TranslateMessageFormatCompiler,\n} from \"./translate-message-format-compiler\";\n\n/* eslint-disable-next-line  no-console */\nconst log = (...message: string[]) => console.log(tag, ...message);\nconst tag = \"[TranslateMessageFormatCompiler]\";\n\n@Injectable()\nexport class TranslateMessageFormatDebugCompiler extends TranslateMessageFormatCompiler {\n  public compile<Result extends CompilationResult = MessageFunction<\"string\">>(\n    value: string,\n    lang: string,\n  ): Result {\n    log(`COMPILE (${lang})`, value);\n    const interpolationFn = super.compile(value, lang);\n\n    return isFunction(interpolationFn)\n      ? (this.wrap(interpolationFn, value) as Result)\n      : (value as Result);\n  }\n\n  public compileTranslations(value: any, lang: string): any {\n    log(`COMPILE (${lang})`, value);\n    return super.compileTranslations(value, lang);\n  }\n\n  private wrap(\n    fn: MessageFunction<\"string\">,\n    reference: string,\n  ): MessageFunction<\"string\"> {\n    return (params: any) => {\n      log(\"INTERPOLATE\", reference, params);\n      return fn(params);\n    };\n  }\n}\n\nfunction isFunction(value: any): value is (...params: unknown[]) => unknown {\n  return typeof value === \"function\";\n}\n","/*\n * Public API Surface of ngx-translate-messageformat-compiler\n */\n\nexport * from \"./lib/message-format-config\";\nexport * from \"./lib/translate-message-format-compiler\";\nexport * from \"./lib/translate-message-format-debug-compiler\";\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;;;MAGa,qBAAqB,GAAG,IAAI,cAAc,CACrD,uBAAuB;AAeZ,MAAA,aAAa,GAAwB;AAChD,IAAA,WAAW,EAAE,KAAK;AAClB,IAAA,UAAU,EAAE,EAAE;AACd,IAAA,gBAAgB,EAAE,KAAK;AACvB,IAAA,QAAQ,EA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