summaryrefslogtreecommitdiffstats
path: root/intl/l10n/test/mochitest/localization/test_formatMessages.html
blob: 6e5a9f3f417a6f663e4e5fa4c9b4bdcdd26c3897 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test Localization.prototype.formatMessages API</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
  <script type="application/javascript">
  "use strict";
  const mockSource = L10nFileSource.createMock("test", "app", ["en-US"], "/localization/{locale}/", [
    {
      path: "/localization/en-US/mock.ftl",
      source: `
key1 = Value
  .title = Title 1
  .accesskey = K
key2 =
  .label = This is a label for { $user }
`
    }
  ]);
  let registry = new L10nRegistry({
    bundleOptions: {
      useIsolating: false
    }
  });
  registry.registerSources([mockSource]);

  function getAttributeByName(attributes, name) {
    return attributes.find(attr => attr.name === name);
  }

  (async () => {
    SimpleTest.waitForExplicitFinish();

    const loc = new Localization(
      ['mock.ftl'],
      false,
      registry,
    );

    {
      // Simple mix works.
      let msgs = await loc.formatMessages([
        {id: "key1"},
        {id: "key2", args: { user: "Amy"}},
      ]);
      {
        is(msgs[0].value, "Value");
        let attr0 = getAttributeByName(msgs[0].attributes, "title");
        is(attr0.name, "title");
        is(attr0.value, "Title 1");
        let attr1 = getAttributeByName(msgs[0].attributes, "accesskey");
        is(attr1.name, "accesskey");
        is(attr1.value, "K");
      }

      {
        is(msgs[1].value, null);
        let attr0 = getAttributeByName(msgs[1].attributes, "label");
        is(attr0.name, "label");
        is(attr0.value, "This is a label for Amy");
      }
    }

    {
      // Missing arguments cause exception in automation.
      try {
        let msgs = await loc.formatMessages([
          {id: "key1"},
          {id: "key2"},
        ]);
        ok(false, "Missing argument didn't cause an exception.");
      } catch (e) {
        is(e.message,
          "[fluent][resolver] errors in en-US/key2: Resolver error: Unknown variable: $user",
          "Missing key causes an exception.");
      }
    }

    {
      // Missing keys cause exception in automation.
      try {
        let msgs = await loc.formatMessages([
          { id: "key1" },
          { id: "key4" },
        ]);
        ok(false, "Missing key didn't cause an exception.");
      } catch (e) {
        is(e.message,
           "[fluent] Missing message in locale en-US: key4",
           "Missing key causes an exception.");
      }
    }
    SimpleTest.finish();
  })();
  </script>
</head>
<body>
</body>
</html>