test_formatMessages.html (2660B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Test Localization.prototype.formatMessages API</title> 6 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 7 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"> 8 <script type="application/javascript"> 9 "use strict"; 10 const mockSource = L10nFileSource.createMock("test", "app", ["en-US"], "/localization/{locale}/", [ 11 { 12 path: "/localization/en-US/mock.ftl", 13 source: ` 14 key1 = Value 15 .title = Title 1 16 .accesskey = K 17 key2 = 18 .label = This is a label for { $user } 19 ` 20 } 21 ]); 22 let registry = new L10nRegistry({ 23 bundleOptions: { 24 useIsolating: false 25 } 26 }); 27 registry.registerSources([mockSource]); 28 29 function getAttributeByName(attributes, name) { 30 return attributes.find(attr => attr.name === name); 31 } 32 33 (async () => { 34 SimpleTest.waitForExplicitFinish(); 35 36 const loc = new Localization( 37 ['mock.ftl'], 38 false, 39 registry, 40 ); 41 42 { 43 // Simple mix works. 44 let msgs = await loc.formatMessages([ 45 {id: "key1"}, 46 {id: "key2", args: { user: "Amy"}}, 47 ]); 48 { 49 is(msgs[0].value, "Value"); 50 let attr0 = getAttributeByName(msgs[0].attributes, "title"); 51 is(attr0.name, "title"); 52 is(attr0.value, "Title 1"); 53 let attr1 = getAttributeByName(msgs[0].attributes, "accesskey"); 54 is(attr1.name, "accesskey"); 55 is(attr1.value, "K"); 56 } 57 58 { 59 is(msgs[1].value, null); 60 let attr0 = getAttributeByName(msgs[1].attributes, "label"); 61 is(attr0.name, "label"); 62 is(attr0.value, "This is a label for Amy"); 63 } 64 } 65 66 { 67 // Missing arguments cause exception in automation. 68 try { 69 let msgs = await loc.formatMessages([ 70 {id: "key1"}, 71 {id: "key2"}, 72 ]); 73 ok(false, "Missing argument didn't cause an exception."); 74 } catch (e) { 75 is(e.message, 76 "[fluent][resolver] errors in en-US/key2: Resolver error: Unknown variable: $user", 77 "Missing key causes an exception."); 78 } 79 } 80 81 { 82 // Missing keys cause exception in automation. 83 try { 84 let msgs = await loc.formatMessages([ 85 { id: "key1" }, 86 { id: "key4" }, 87 ]); 88 ok(false, "Missing key didn't cause an exception."); 89 } catch (e) { 90 is(e.message, 91 "[fluent] Missing message in locale en-US: key4", 92 "Missing key causes an exception."); 93 } 94 } 95 SimpleTest.finish(); 96 })(); 97 </script> 98 </head> 99 <body> 100 </body> 101 </html>