]>
Commit | Line | Data |
---|---|---|
2b5f94fa | 1 | const expect = chai.expect; |
8727f598 | 2 | import { l10n } from '../app/localization.js'; |
7279364c | 3 | |
2c5491e1 | 4 | describe('Localization', function () { |
7279364c SM |
5 | "use strict"; |
6 | ||
7 | describe('language selection', function () { | |
2b5f94fa | 8 | let origNavigator; |
7279364c SM |
9 | beforeEach(function () { |
10 | // window.navigator is a protected read-only property in many | |
11 | // environments, so we need to redefine it whilst running these | |
12 | // tests. | |
13 | origNavigator = Object.getOwnPropertyDescriptor(window, "navigator"); | |
7279364c SM |
14 | |
15 | Object.defineProperty(window, "navigator", {value: {}}); | |
16 | if (window.navigator.languages !== undefined) { | |
17 | // Object.defineProperty() doesn't work properly in old | |
18 | // versions of Chrome | |
19 | this.skip(); | |
20 | } | |
21 | ||
22 | window.navigator.languages = []; | |
23 | }); | |
24 | afterEach(function () { | |
eb05b45b PO |
25 | if (origNavigator !== undefined) { |
26 | Object.defineProperty(window, "navigator", origNavigator); | |
27 | } | |
7279364c SM |
28 | }); |
29 | ||
2c5491e1 | 30 | it('should use English by default', function () { |
7279364c SM |
31 | expect(l10n.language).to.equal('en'); |
32 | }); | |
2c5491e1 | 33 | it('should use English if no user language matches', function () { |
7279364c SM |
34 | window.navigator.languages = ["nl", "de"]; |
35 | l10n.setup(["es", "fr"]); | |
36 | expect(l10n.language).to.equal('en'); | |
37 | }); | |
2c5491e1 | 38 | it('should use the most preferred user language', function () { |
7279364c SM |
39 | window.navigator.languages = ["nl", "de", "fr"]; |
40 | l10n.setup(["es", "fr", "de"]); | |
41 | expect(l10n.language).to.equal('de'); | |
42 | }); | |
2c5491e1 | 43 | it('should prefer sub-languages languages', function () { |
7279364c SM |
44 | window.navigator.languages = ["pt-BR"]; |
45 | l10n.setup(["pt", "pt-BR"]); | |
46 | expect(l10n.language).to.equal('pt-BR'); | |
47 | }); | |
2c5491e1 | 48 | it('should fall back to language "parents"', function () { |
7279364c SM |
49 | window.navigator.languages = ["pt-BR"]; |
50 | l10n.setup(["fr", "pt", "de"]); | |
51 | expect(l10n.language).to.equal('pt'); | |
52 | }); | |
2c5491e1 | 53 | it('should not use specific language when user asks for a generic language', function () { |
7279364c SM |
54 | window.navigator.languages = ["pt", "de"]; |
55 | l10n.setup(["fr", "pt-BR", "de"]); | |
56 | expect(l10n.language).to.equal('de'); | |
57 | }); | |
2c5491e1 | 58 | it('should handle underscore as a separator', function () { |
7279364c SM |
59 | window.navigator.languages = ["pt-BR"]; |
60 | l10n.setup(["pt_BR"]); | |
61 | expect(l10n.language).to.equal('pt_BR'); | |
62 | }); | |
2c5491e1 | 63 | it('should handle difference in case', function () { |
7279364c SM |
64 | window.navigator.languages = ["pt-br"]; |
65 | l10n.setup(["pt-BR"]); | |
66 | expect(l10n.language).to.equal('pt-BR'); | |
67 | }); | |
68 | }); | |
69 | }); |