translator.spec.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { Translator } from '../translator.ts'
  3. describe('Translator', () => {
  4. const t = new Translator()
  5. it('starts with empty state', () => {
  6. expect(t.translate('unknown string')).toBe('unknown string')
  7. expect(t.translate('yes')).toBe('yes')
  8. })
  9. it('keeps unknown strings', () => {
  10. const map = new Map([
  11. ['yes', 'ja'],
  12. [
  13. 'String with 3 placeholders: %s %s %s',
  14. 'Zeichenkette mit 3 Platzhaltern: %s %s %s',
  15. ],
  16. ])
  17. t.setTranslationMap(map)
  18. expect(t.translate('unknown string')).toBe('unknown string')
  19. expect(t.translate('unknown string with placeholder %s')).toBe(
  20. 'unknown string with placeholder %s',
  21. )
  22. })
  23. it('translates known strings', () => {
  24. expect(t.translate('yes')).toBe('ja')
  25. })
  26. it('handles placeholders correctly', () => {
  27. // No arguments.
  28. expect(t.translate('String with 3 placeholders: %s %s %s')).toBe(
  29. 'Zeichenkette mit 3 Platzhaltern: %s %s %s',
  30. )
  31. // Partial arguments.
  32. expect(t.translate('String with 3 placeholders: %s %s %s', 1, '2')).toBe(
  33. 'Zeichenkette mit 3 Platzhaltern: 1 2 %s',
  34. )
  35. // Correct arguments.
  36. expect(
  37. t.translate('String with 3 placeholders: %s %s %s', 1, '2', 'some words'),
  38. ).toBe('Zeichenkette mit 3 Platzhaltern: 1 2 some words')
  39. // Excess arguments.
  40. expect(
  41. t.translate(
  42. 'String with 3 placeholders: %s %s %s',
  43. 1,
  44. '2',
  45. 'some words',
  46. 3,
  47. 4,
  48. ),
  49. ).toBe('Zeichenkette mit 3 Platzhaltern: 1 2 some words')
  50. })
  51. it('lookup() works correctly', () => {
  52. expect(t.lookup('yes')).toBe('ja')
  53. expect(t.lookup('NONEXISTING')).toBe(undefined)
  54. })
  55. })