translator.ts 663 B

12345678910111213141516171819202122232425
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. export type TranslationMap = Map<string, string>
  3. export class Translator {
  4. private translationMap: TranslationMap = new Map()
  5. setTranslationMap(translationMap: TranslationMap) {
  6. this.translationMap = translationMap
  7. }
  8. translate(source: string, ...args: Array<number | string>): string {
  9. let target = this.translationMap.get(source) || source
  10. args.forEach((arg) => {
  11. if (arg != null) target = target.replace('%s', arg.toString())
  12. })
  13. return target
  14. }
  15. lookup(source: string): string | undefined {
  16. return this.translationMap.get(source)
  17. }
  18. }