Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
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/. */
5 /**
6 * @namespace Defines useful methods to work with localized content
7 */
8 var l10n = exports;
10 Cu.import("resource://gre/modules/Services.jsm");
12 /**
13 * Retrieve the localized content for a given DTD entity
14 *
15 * @memberOf l10n
16 * @param {String[]} aDTDs Array of URLs for DTD files.
17 * @param {String} aEntityId ID of the entity to get the localized content of.
18 *
19 * @returns {String} Localized content
20 */
21 function getEntity(aDTDs, aEntityId) {
22 // Add xhtml11.dtd to prevent missing entity errors with XHTML files
23 aDTDs.push("resource:///res/dtd/xhtml11.dtd");
25 // Build a string of external entities
26 var references = "";
27 for (i = 0; i < aDTDs.length; i++) {
28 var id = 'dtd' + i;
29 references += '<!ENTITY % ' + id + ' SYSTEM "' + aDTDs[i] + '">%' + id + ';';
30 }
32 var header = '<?xml version="1.0"?><!DOCTYPE elem [' + references + ']>';
33 var element = '<elem id="entity">&' + aEntityId + ';</elem>';
34 var content = header + element;
36 var parser = Cc["@mozilla.org/xmlextras/domparser;1"].
37 createInstance(Ci.nsIDOMParser);
38 var doc = parser.parseFromString(content, 'text/xml');
39 var node = doc.querySelector('elem[id="entity"]');
41 if (!node) {
42 throw new Error("Unkown entity '" + aEntityId + "'");
43 }
45 return node.textContent;
46 }
49 /**
50 * Retrieve the localized content for a given property
51 *
52 * @memberOf l10n
53 * @param {String} aURL URL of the .properties file.
54 * @param {String} aProperty The property to get the value of.
55 *
56 * @returns {String} Value of the requested property
57 */
58 function getProperty(aURL, aProperty) {
59 var bundle = Services.strings.createBundle(aURL);
61 try {
62 return bundle.GetStringFromName(aProperty);
63 } catch (ex) {
64 throw new Error("Unkown property '" + aProperty + "'");
65 }
66 }
69 // Export of functions
70 l10n.getEntity = getEntity;
71 l10n.getProperty = getProperty;