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