blob: 63a35542166baf9f867d3b71189f87e2fd0f798b (
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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* @namespace Defines useful methods to work with localized content
*/
var l10n = exports;
Cu.import("resource://gre/modules/Services.jsm");
/**
* Retrieve the localized content for a given DTD entity
*
* @memberOf l10n
* @param {String[]} aDTDs Array of URLs for DTD files.
* @param {String} aEntityId ID of the entity to get the localized content of.
*
* @returns {String} Localized content
*/
function getEntity(aDTDs, aEntityId) {
// Add xhtml11.dtd to prevent missing entity errors with XHTML files
aDTDs.push("resource:///res/dtd/xhtml11.dtd");
// Build a string of external entities
var references = "";
for (i = 0; i < aDTDs.length; i++) {
var id = 'dtd' + i;
references += '<!ENTITY % ' + id + ' SYSTEM "' + aDTDs[i] + '">%' + id + ';';
}
var header = '<?xml version="1.0"?><!DOCTYPE elem [' + references + ']>';
var element = '<elem id="entity">&' + aEntityId + ';</elem>';
var content = header + element;
var parser = Cc["@mozilla.org/xmlextras/domparser;1"].
createInstance(Ci.nsIDOMParser);
var doc = parser.parseFromString(content, 'text/xml');
var node = doc.querySelector('elem[id="entity"]');
if (!node) {
throw new Error("Unkown entity '" + aEntityId + "'");
}
return node.textContent;
}
/**
* Retrieve the localized content for a given property
*
* @memberOf l10n
* @param {String} aURL URL of the .properties file.
* @param {String} aProperty The property to get the value of.
*
* @returns {String} Value of the requested property
*/
function getProperty(aURL, aProperty) {
var bundle = Services.strings.createBundle(aURL);
try {
return bundle.GetStringFromName(aProperty);
} catch (ex) {
throw new Error("Unkown property '" + aProperty + "'");
}
}
// Export of functions
l10n.getEntity = getEntity;
l10n.getProperty = getProperty;
|