i18n.spec.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { defineComponent, nextTick } from 'vue'
  3. import { renderComponent } from '#tests/support/components/index.ts'
  4. import { i18n } from '#shared/i18n.ts'
  5. const Example = defineComponent({
  6. name: 'Example',
  7. props: {
  8. text: {
  9. type: String,
  10. required: true,
  11. },
  12. },
  13. data: () => ({ i18n }),
  14. template: '<div>{{ i18n.t(text) }}</div>',
  15. })
  16. const populateTranslationMap = () => {
  17. const map = new Map([
  18. ['yes', 'ja'],
  19. ['Hello world!', 'Hallo Welt!'],
  20. ['The second component.', 'Die zweite Komponente.'],
  21. [
  22. 'String with 3 placeholders: %s %s %s',
  23. 'Zeichenkette mit 3 Platzhaltern: %s %s %s',
  24. ],
  25. ['FORMAT_DATE', 'dd/mm/yyyy'],
  26. ['FORMAT_DATETIME', 'dd/mm/yyyy HH:MM:SS'],
  27. ])
  28. i18n.setTranslationMap(map)
  29. }
  30. describe('i18n', () => {
  31. afterEach(() => {
  32. i18n.setTranslationMap(new Map())
  33. })
  34. describe('in empty state', () => {
  35. it('translates to source value', () => {
  36. expect(i18n.t('unknown string')).toBe('unknown string')
  37. expect(i18n.t('yes')).toBe('yes')
  38. })
  39. })
  40. describe('with translation data', () => {
  41. beforeEach(() => {
  42. populateTranslationMap()
  43. })
  44. it('translates known strings', () => {
  45. expect(i18n.t('yes')).toBe('ja')
  46. })
  47. it('handles placeholders correctly', () => {
  48. expect(i18n.t('String with 3 placeholders: %s %s %s', 1, 2)).toBe(
  49. 'Zeichenkette mit 3 Platzhaltern: 1 2 %s',
  50. )
  51. })
  52. it('translates dates', () => {
  53. expect(i18n.date('2021-04-09T10:11:12Z')).toBe('09/04/2021')
  54. expect(i18n.dateTime('2021-04-09T10:11:12Z')).toBe('09/04/2021 10:11:12')
  55. expect(i18n.relativeDateTime(new Date().toISOString())).toBe('just now')
  56. })
  57. it('returns date/time format information', () => {
  58. expect(i18n.getTimeFormatType()).toBe('24hour')
  59. expect(i18n.getDateFormat()).toBe('dd/mm/yyyy')
  60. expect(i18n.getDateTimeFormat()).toBe('dd/mm/yyyy HH:MM:SS')
  61. })
  62. it('updates (reactive) translations automatically', async () => {
  63. const { container } = renderComponent(Example, {
  64. props: {
  65. text: 'Hello world!',
  66. },
  67. global: {
  68. mocks: {
  69. i18n,
  70. },
  71. },
  72. })
  73. expect(container).toHaveTextContent('Hallo Welt!')
  74. i18n.setTranslationMap(new Map())
  75. await nextTick()
  76. expect(container).toHaveTextContent('Hello world!')
  77. })
  78. })
  79. })