translator.ts 704 B

12345678910111213141516171819202122232425262728
  1. // Copyright (C) 2012-2024 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(
  9. source: string,
  10. ...args: Array<number | string | undefined | null | boolean>
  11. ): string {
  12. let target = this.translationMap.get(source) || source
  13. args.forEach((arg) => {
  14. if (arg != null) target = target.replace('%s', arg.toString())
  15. })
  16. return target
  17. }
  18. lookup(source: string): string | undefined {
  19. return this.translationMap.get(source)
  20. }
  21. }