|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 "use strict"; |
|
5 |
|
6 const prefs = require("sdk/preferences/service"); |
|
7 const { Loader } = require('sdk/test/loader'); |
|
8 const { resolveURI } = require('toolkit/loader'); |
|
9 const { rootURI } = require("@loader/options"); |
|
10 const { usingJSON } = require('sdk/l10n/json/core'); |
|
11 |
|
12 const PREF_MATCH_OS_LOCALE = "intl.locale.matchOS"; |
|
13 const PREF_SELECTED_LOCALE = "general.useragent.locale"; |
|
14 |
|
15 function setLocale(locale) { |
|
16 prefs.set(PREF_MATCH_OS_LOCALE, false); |
|
17 prefs.set(PREF_SELECTED_LOCALE, locale); |
|
18 } |
|
19 |
|
20 function resetLocale() { |
|
21 prefs.reset(PREF_MATCH_OS_LOCALE); |
|
22 prefs.reset(PREF_SELECTED_LOCALE); |
|
23 } |
|
24 |
|
25 function definePseudo(loader, id, exports) { |
|
26 let uri = resolveURI(id, loader.mapping); |
|
27 loader.modules[uri] = { exports: exports }; |
|
28 } |
|
29 |
|
30 function createTest(locale, testFunction) { |
|
31 return function (assert, done) { |
|
32 let loader = Loader(module); |
|
33 // Change the locale before loading new l10n modules in order to load |
|
34 // the right .json file |
|
35 setLocale(locale); |
|
36 // Initialize main l10n module in order to load new locale files |
|
37 loader.require("sdk/l10n/loader"). |
|
38 load(rootURI). |
|
39 then(function success(data) { |
|
40 definePseudo(loader, '@l10n/data', data); |
|
41 // Execute the given test function |
|
42 try { |
|
43 testFunction(assert, loader, function onDone() { |
|
44 loader.unload(); |
|
45 resetLocale(); |
|
46 done(); |
|
47 }); |
|
48 } |
|
49 catch(e) { |
|
50 console.exception(e); |
|
51 } |
|
52 }, |
|
53 function failure(error) { |
|
54 assert.fail("Unable to load locales: " + error); |
|
55 }); |
|
56 }; |
|
57 } |
|
58 |
|
59 exports.testExactMatching = createTest("fr-FR", function(assert, loader, done) { |
|
60 let _ = loader.require("sdk/l10n").get; |
|
61 assert.equal(_("Not translated"), "Not translated", |
|
62 "Key not translated"); |
|
63 assert.equal(_("Translated"), "Oui", |
|
64 "Simple key translated"); |
|
65 |
|
66 // Placeholders |
|
67 assert.equal(_("placeholderString", "works"), "Placeholder works", |
|
68 "Value with placeholder"); |
|
69 assert.equal(_("Placeholder %s", "works"), "Placeholder works", |
|
70 "Key without value but with placeholder"); |
|
71 assert.equal(_("Placeholders %2s %1s %s.", "working", "are", "correctly"), |
|
72 "Placeholders are working correctly.", |
|
73 "Multiple placeholders"); |
|
74 |
|
75 // Plurals |
|
76 assert.equal(_("downloadsCount", 0), |
|
77 "0 téléchargement", |
|
78 "PluralForm form 'one' for 0 in french"); |
|
79 assert.equal(_("downloadsCount", 1), |
|
80 "1 téléchargement", |
|
81 "PluralForm form 'one' for 1 in french"); |
|
82 assert.equal(_("downloadsCount", 2), |
|
83 "2 téléchargements", |
|
84 "PluralForm form 'other' for n > 1 in french"); |
|
85 |
|
86 done(); |
|
87 }); |
|
88 |
|
89 exports.testHtmlLocalization = createTest("en-GB", function(assert, loader, done) { |
|
90 |
|
91 // Ensure initing html component that watch document creations |
|
92 // Note that this module is automatically initialized in |
|
93 // cuddlefish.js:Loader.main in regular addons. But it isn't for unit tests. |
|
94 let loaderHtmlL10n = loader.require("sdk/l10n/html"); |
|
95 loaderHtmlL10n.enable(); |
|
96 |
|
97 let uri = require("sdk/self").data.url("test-localization.html"); |
|
98 let worker = loader.require("sdk/page-worker").Page({ |
|
99 contentURL: uri, |
|
100 contentScript: "new " + function ContentScriptScope() { |
|
101 let nodes = document.body.querySelectorAll("*[data-l10n-id]"); |
|
102 self.postMessage([nodes[0].innerHTML, |
|
103 nodes[1].innerHTML, |
|
104 nodes[2].innerHTML, |
|
105 nodes[3].innerHTML]); |
|
106 }, |
|
107 onMessage: function (data) { |
|
108 assert.equal( |
|
109 data[0], |
|
110 "Kept as-is", |
|
111 "Nodes with unknown id in .properties are kept 'as-is'" |
|
112 ); |
|
113 assert.equal(data[1], "Yes", "HTML is translated"); |
|
114 assert.equal( |
|
115 data[2], |
|
116 "no <b>HTML</b> injection", |
|
117 "Content from .properties is text content; HTML can't be injected." |
|
118 ); |
|
119 assert.equal(data[3], "Yes", "Multiple elements with same data-l10n-id are accepted."); |
|
120 |
|
121 done(); |
|
122 } |
|
123 }); |
|
124 |
|
125 }); |
|
126 |
|
127 exports.testEnUsLocaleName = createTest("en-US", function(assert, loader, done) { |
|
128 let _ = loader.require("sdk/l10n").get; |
|
129 |
|
130 assert.equal(_("Not translated"), "Not translated", |
|
131 "String w/o translation is kept as-is"); |
|
132 assert.equal(_("Translated"), "Yes", |
|
133 "String with translation is correctly translated"); |
|
134 |
|
135 // Check Unicode char escaping sequences |
|
136 assert.equal(_("unicodeEscape"), " @ ", |
|
137 "Unicode escaped sequances are correctly converted"); |
|
138 |
|
139 // Check plural forms regular matching |
|
140 assert.equal(_("downloadsCount", 0), |
|
141 "0 downloads", |
|
142 "PluralForm form 'other' for 0 in english"); |
|
143 assert.equal(_("downloadsCount", 1), |
|
144 "one download", |
|
145 "PluralForm form 'one' for 1 in english"); |
|
146 assert.equal(_("downloadsCount", 2), |
|
147 "2 downloads", |
|
148 "PluralForm form 'other' for n != 1 in english"); |
|
149 |
|
150 // Check optional plural forms |
|
151 assert.equal(_("pluralTest", 0), |
|
152 "optional zero form", |
|
153 "PluralForm form 'zero' can be optionaly specified. (Isn't mandatory in english)"); |
|
154 assert.equal(_("pluralTest", 1), |
|
155 "fallback to other", |
|
156 "If the specific plural form is missing, we fallback to 'other'"); |
|
157 |
|
158 // Ensure that we can omit specifying the generic key without [other] |
|
159 // key[one] = ... |
|
160 // key[other] = ... # Instead of `key = ...` |
|
161 assert.equal(_("explicitPlural", 1), |
|
162 "one", |
|
163 "PluralForm form can be omitting generic key [i.e. without ...[other] at end of key)"); |
|
164 assert.equal(_("explicitPlural", 10), |
|
165 "other", |
|
166 "PluralForm form can be omitting generic key [i.e. without ...[other] at end of key)"); |
|
167 |
|
168 done(); |
|
169 }); |
|
170 |
|
171 exports.testUsingJSON = function(assert) { |
|
172 assert.equal(usingJSON, true, 'using json'); |
|
173 } |
|
174 |
|
175 exports.testShortLocaleName = createTest("eo", function(assert, loader, done) { |
|
176 let _ = loader.require("sdk/l10n").get; |
|
177 assert.equal(_("Not translated"), "Not translated", |
|
178 "String w/o translation is kept as-is"); |
|
179 assert.equal(_("Translated"), "jes", |
|
180 "String with translation is correctly translated"); |
|
181 |
|
182 done(); |
|
183 }); |
|
184 |
|
185 |
|
186 // Before running tests, disable HTML service which is automatially enabled |
|
187 // in api-utils/addon/runner.js |
|
188 require('sdk/l10n/html').disable(); |
|
189 |
|
190 require("sdk/test/runner").runTestsFromModule(module); |