1 | const Translator = require('./Translator')
|
2 |
|
3 | const english = {
|
4 | strings: {
|
5 | chooseFile: 'Choose a file',
|
6 | youHaveChosen: 'You have chosen: %{fileName}',
|
7 | filesChosen: {
|
8 | 0: '%{smart_count} file selected',
|
9 | 1: '%{smart_count} files selected',
|
10 | },
|
11 | pluralize (n) {
|
12 | if (n === 1) {
|
13 | return 0
|
14 | }
|
15 | return 1
|
16 | },
|
17 | },
|
18 | }
|
19 |
|
20 | const russian = {
|
21 | strings: {
|
22 | chooseFile: 'Выберите файл',
|
23 | youHaveChosen: 'Вы выбрали: %{file_name}',
|
24 | filesChosen: {
|
25 | 0: 'Выбран %{smart_count} файл',
|
26 | 1: 'Выбрано %{smart_count} файла',
|
27 | 2: 'Выбрано %{smart_count} файлов',
|
28 | },
|
29 | },
|
30 | pluralize (n) {
|
31 | if (n % 10 === 1 && n % 100 !== 11) {
|
32 | return 0
|
33 | }
|
34 |
|
35 | if (n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20)) {
|
36 | return 1
|
37 | }
|
38 |
|
39 | return 2
|
40 | },
|
41 | }
|
42 |
|
43 | describe('Translator', () => {
|
44 | describe('translate', () => {
|
45 | it('should translate a string', () => {
|
46 | const translator = new Translator(russian)
|
47 | expect(translator.translate('chooseFile')).toEqual('Выберите файл')
|
48 | })
|
49 |
|
50 | it('should translate a string with non-string elements', () => {
|
51 | const translator = new Translator({
|
52 | strings: {
|
53 | test: 'Hello %{who}!',
|
54 | test2: 'Hello %{who}',
|
55 | },
|
56 | })
|
57 |
|
58 | const who = Symbol('who')
|
59 | expect(translator.translateArray('test', { who })).toEqual(['Hello ', who, '!'])
|
60 |
|
61 | expect(translator.translateArray('test2', { who })).toEqual(['Hello ', who])
|
62 | })
|
63 | })
|
64 |
|
65 | describe('translation strings inheritance / overriding', () => {
|
66 | const launguagePackLoadedInCore = english
|
67 | const defaultStrings = {
|
68 | strings: {
|
69 | youHaveChosen: 'You have chosen 123: %{fileName}',
|
70 | },
|
71 | }
|
72 | const userSuppliedStrings = {
|
73 | strings: {
|
74 | youHaveChosen: 'Beep boop: %{fileName}',
|
75 | },
|
76 | }
|
77 |
|
78 | it('should prioritize language pack strings from Core over default', () => {
|
79 | const translator = new Translator([defaultStrings, launguagePackLoadedInCore])
|
80 | expect(
|
81 | translator.translate('youHaveChosen', { fileName: 'img.jpg' })
|
82 | ).toEqual('You have chosen: img.jpg')
|
83 | })
|
84 |
|
85 | it('should prioritize user-supplied strings over language pack from Core', () => {
|
86 | const translator = new Translator([defaultStrings, launguagePackLoadedInCore, userSuppliedStrings])
|
87 | expect(
|
88 | translator.translate('youHaveChosen', { fileName: 'img.jpg' })
|
89 | ).toEqual('Beep boop: img.jpg')
|
90 | })
|
91 | })
|
92 |
|
93 | describe('interpolation', () => {
|
94 | it('should interpolate a string', () => {
|
95 | const translator = new Translator(english)
|
96 | expect(
|
97 | translator.translate('youHaveChosen', { fileName: 'img.jpg' })
|
98 | ).toEqual('You have chosen: img.jpg')
|
99 | })
|
100 | })
|
101 |
|
102 | describe('pluralization', () => {
|
103 | it('should translate a string', () => {
|
104 | const translator = new Translator(russian)
|
105 | expect(
|
106 | translator.translate('filesChosen', { smart_count: 18 })
|
107 | ).toEqual('Выбрано 18 файлов')
|
108 |
|
109 | expect(
|
110 | translator.translate('filesChosen', { smart_count: 1 })
|
111 | ).toEqual('Выбран 1 файл')
|
112 |
|
113 | expect(
|
114 | translator.translate('filesChosen', { smart_count: 0 })
|
115 | ).toEqual('Выбрано 0 файлов')
|
116 | })
|
117 |
|
118 | it('should support strings without plural forms', () => {
|
119 | const translator = new Translator({
|
120 | strings: {
|
121 | theAmount: 'het aantal is %{smart_count}',
|
122 | },
|
123 | pluralize: () => 0,
|
124 | })
|
125 |
|
126 | expect(
|
127 | translator.translate('theAmount', { smart_count: 0 })
|
128 | ).toEqual('het aantal is 0')
|
129 | expect(
|
130 | translator.translate('theAmount', { smart_count: 1 })
|
131 | ).toEqual('het aantal is 1')
|
132 | expect(
|
133 | translator.translate('theAmount', { smart_count: 1202530 })
|
134 | ).toEqual('het aantal is 1202530')
|
135 | })
|
136 |
|
137 | it('should error when using a plural form without %{smart_count}', () => {
|
138 | const translator = new Translator({
|
139 | strings: {
|
140 | test: {
|
141 | 0: 'A test',
|
142 | 1: '%{smart_count} tests',
|
143 | },
|
144 | },
|
145 | })
|
146 |
|
147 | expect(() => {
|
148 | translator.translate('test')
|
149 | }).toThrow('Attempted to use a string with plural forms, but no value was given for %{smart_count}')
|
150 | })
|
151 | })
|
152 | })
|