1 | import _ from "lodash"
|
2 | import { assert } from "chai"
|
3 | import * as utils from "../src/utils"
|
4 | import { LocalizedString } from "../src/utils"
|
5 |
|
6 | describe("Localizer", function () {
|
7 | describe("extractLocalizedStrings", function () {
|
8 | it("gets all strings", function () {
|
9 | const obj = {
|
10 | a: [
|
11 | {
|
12 | b: { _base: "en", en: "hello" }
|
13 | },
|
14 | {
|
15 | c: { _base: "es", en: "hello2" }
|
16 | }
|
17 | ],
|
18 | d: "test",
|
19 | e: null
|
20 | }
|
21 | const strs = utils.extractLocalizedStrings(obj)
|
22 |
|
23 | return assert.deepEqual(strs, [
|
24 | { _base: "en", en: "hello" },
|
25 | { _base: "es", en: "hello2" }
|
26 | ])
|
27 | })
|
28 |
|
29 | return it("gets localizedStrings strings", function () {
|
30 | const obj = {
|
31 | localizedStrings: [
|
32 | {
|
33 | b: { _base: "en", en: "hello" }
|
34 | },
|
35 | {
|
36 | c: { _base: "es", en: "hello2" }
|
37 | }
|
38 | ],
|
39 | d: "test",
|
40 | e: null
|
41 | }
|
42 | const strs = utils.extractLocalizedStrings(obj)
|
43 |
|
44 | return assert.deepEqual(strs, [
|
45 | { _base: "en", en: "hello" },
|
46 | { _base: "es", en: "hello2" }
|
47 | ])
|
48 | })
|
49 | })
|
50 |
|
51 | describe("dedupLocalizedStrings", () =>
|
52 | it("removes duplicates by base + string", function () {
|
53 | const strs: LocalizedString[] = [
|
54 | { _base: "en", en: "hello", es: "esp1" },
|
55 | { _base: "es", en: "hello", es: "hello" },
|
56 | { _base: "en", en: "hello2" },
|
57 | { _base: "en", en: "hello", es: "esp2" }
|
58 | ]
|
59 |
|
60 | const strs2 = utils.dedupLocalizedStrings(strs)
|
61 | assert.deepEqual(strs2, [
|
62 | { _base: "en", en: "hello", es: "esp1" },
|
63 | { _base: "es", en: "hello", es: "hello" },
|
64 | { _base: "en", en: "hello2" }
|
65 | ])
|
66 |
|
67 |
|
68 | return assert(strs2[0] === strs[0])
|
69 | }))
|
70 |
|
71 | describe("changeBaseLocale", () =>
|
72 | it("changes base, preserving other strings but overwriting with base", function () {
|
73 | const strs: LocalizedString[] = [
|
74 | { _base: "en", en: "hello", es: "esp1" },
|
75 | { _base: "en", en: "hello", fr: "fr1", es: "esp1" },
|
76 | { _base: "es", en: "hello", fr: "fr1", es: "esp1" },
|
77 | { _base: "fr", fr: "fr2", es: "esp1" },
|
78 | { _base: "es", fr: "fr2", es: "esp1" }
|
79 | ]
|
80 |
|
81 | utils.changeBaseLocale(strs, "en", "fr")
|
82 | return assert.deepEqual(
|
83 | strs,
|
84 | [
|
85 | { _base: "fr", fr: "hello", es: "esp1" },
|
86 | { _base: "fr", fr: "hello", es: "esp1" },
|
87 | { _base: "fr", fr: "hello", es: "esp1" },
|
88 | { _base: "fr", fr: "fr2", es: "esp1" },
|
89 | { _base: "fr", fr: "esp1" }
|
90 | ],
|
91 | JSON.stringify(strs, null, 2)
|
92 | )
|
93 | }))
|
94 |
|
95 | describe("xlsx export/import", function () {
|
96 | beforeEach(function () {
|
97 | this.localizer = utils
|
98 | this.locales = [
|
99 | { code: "en", name: "English" },
|
100 | { code: "es", name: "Espanol" }
|
101 | ]
|
102 | return (this.roundtrip = function (strs: any) {
|
103 | const xlsxFile = this.localizer.exportXlsx(this.locales, strs)
|
104 | return this.localizer.importXlsx(this.locales, xlsxFile)
|
105 | })
|
106 | })
|
107 |
|
108 | it("roundtrips strings", function () {
|
109 | const input = [
|
110 | { _base: "en", en: "hello", es: "hola" },
|
111 | { _base: "es", en: "bye", es: "ciao" }
|
112 | ]
|
113 |
|
114 | const output = this.roundtrip(input)
|
115 | return assert.deepEqual(input, output)
|
116 | })
|
117 |
|
118 | it("preserves special characters", function () {
|
119 | const input = [
|
120 | { _base: "en", en: "<>`'\"!@#$%^&*()_+", es: "hola" },
|
121 | { _base: "es", en: "bye", es: "ciao" }
|
122 | ]
|
123 |
|
124 | const output = this.roundtrip(input)
|
125 | return assert.deepEqual(input, output)
|
126 | })
|
127 |
|
128 | it("preserves unicode characters", function () {
|
129 | const input = [
|
130 | { _base: "en", en: "éא", es: "hola" },
|
131 | { _base: "es", en: "bye ", es: "ciao" }
|
132 | ]
|
133 |
|
134 | const output = this.roundtrip(input)
|
135 | return assert.deepEqual(input, output)
|
136 | })
|
137 |
|
138 | it("preserves enter", function () {
|
139 | const input = [
|
140 | { _base: "en", en: "hello\nhello2", es: "hola" },
|
141 | { _base: "es", en: "bye", es: "ciao" }
|
142 | ]
|
143 |
|
144 | const output = this.roundtrip(input)
|
145 | return assert.deepEqual(input, output)
|
146 | })
|
147 |
|
148 | it("preserves numbers as strings", function () {
|
149 | const input = [
|
150 | { _base: "en", en: "2", es: "3" },
|
151 | { _base: "es", en: "bye", es: "ciao" }
|
152 | ]
|
153 |
|
154 | const output = this.roundtrip(input)
|
155 | return assert.deepEqual(input, output)
|
156 | })
|
157 |
|
158 | return it("returns empty strings", function () {
|
159 | const input = [{ _base: "en", en: "2" }]
|
160 |
|
161 | const output = this.roundtrip(input)
|
162 | return assert.deepEqual(output, [{ _base: "en", en: "2", es: "" }])
|
163 | })
|
164 | })
|
165 |
|
166 | return describe("updateLocalizedStrings", function () {
|
167 | beforeEach(function () {
|
168 | return (this.localizer = utils)
|
169 | })
|
170 |
|
171 | it("updates based on base + string", function () {
|
172 | const strs = [
|
173 | { _base: "en", en: "hello", es: "hola1" },
|
174 | { _base: "en", en: "hello", es: "hola2" },
|
175 | { _base: "es", en: "bye", es: "ciao" }
|
176 | ]
|
177 |
|
178 | const updates = [
|
179 | { _base: "en", en: "hello", es: "hola3" },
|
180 | { _base: "en", en: "bye", es: "ciao2" }
|
181 | ]
|
182 |
|
183 | this.localizer.updateLocalizedStrings(strs, updates)
|
184 |
|
185 | return assert.deepEqual(strs, [
|
186 | { _base: "en", en: "hello", es: "hola3" },
|
187 | { _base: "en", en: "hello", es: "hola3" },
|
188 | { _base: "es", en: "bye", es: "ciao" }
|
189 | ])
|
190 | })
|
191 |
|
192 | it("updates using trimmed string", function () {
|
193 | const strs = [{ _base: "en", en: "hello ", es: "hola1" }]
|
194 |
|
195 | const updates = [{ _base: "en", en: "hello", es: "hola3" }]
|
196 |
|
197 | this.localizer.updateLocalizedStrings(strs, updates)
|
198 |
|
199 | return assert.deepEqual(strs, [{ _base: "en", en: "hello ", es: "hola3" }], JSON.stringify(strs))
|
200 | })
|
201 |
|
202 | it("updates using CR/LF normalized string", function () {
|
203 | const strs = [{ _base: "en", en: "hello\nthere", es: "hola1" }]
|
204 |
|
205 | const updates = [{ _base: "en", en: "hello\r\nthere", es: "hola3" }]
|
206 |
|
207 | this.localizer.updateLocalizedStrings(strs, updates)
|
208 |
|
209 | return assert.deepEqual(strs, [{ _base: "en", en: "hello\nthere", es: "hola3" }], JSON.stringify(strs))
|
210 | })
|
211 |
|
212 | it("leaves unknown strings untouched", function () {
|
213 | const strs = [{ _base: "en", en: "other", es: "otra" }]
|
214 |
|
215 | const updates = [
|
216 | { _base: "en", en: "hello", es: "hola3" },
|
217 | { _base: "en", en: "bye", es: "ciao2" }
|
218 | ]
|
219 |
|
220 | this.localizer.updateLocalizedStrings(strs, updates)
|
221 |
|
222 | return assert.deepEqual(strs, [{ _base: "en", en: "other", es: "otra" }])
|
223 | })
|
224 |
|
225 | it("removes empty strings", function () {
|
226 | const strs: LocalizedString[] = [{ _base: "en", en: "other", es: "otra" }]
|
227 |
|
228 | const updates: LocalizedString[] = [
|
229 | { _base: "en", en: "other", es: "" }
|
230 | ]
|
231 |
|
232 | this.localizer.updateLocalizedStrings(strs, updates)
|
233 |
|
234 | return assert.deepEqual(strs, [{ _base: "en", en: "other" }], JSON.stringify(strs))
|
235 | })
|
236 |
|
237 | return it("leaves unknown languages untouched", function () {
|
238 | const strs = [{ _base: "en", en: "hello", es: "hola1", fr: "bonjour" }]
|
239 |
|
240 | const updates = [{ _base: "en", en: "hello", es: "hola3" }]
|
241 |
|
242 | this.localizer.updateLocalizedStrings(strs, updates)
|
243 |
|
244 | return assert.deepEqual(strs, [{ _base: "en", en: "hello", es: "hola3", fr: "bonjour" }])
|
245 | })
|
246 | })
|
247 | })
|