mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-10 03:45:46 +00:00
Bug 1294186 - Add memoized getter for properties files in l10n.js;r=bgrins
Localization properties files should not change during a session. To allow callers to easily localize strings without having to instanciate LocalizationHelper objects, we should cache the already required properties files. This will for now only be used internally by the LocalizationHelper as well as by the markup localization utility. But we could now use this to provide static localization methods. MozReview-Commit-ID: 85agGNJlIk9 --HG-- extra : rebase_source : 55ef18f371fa474e575ddc15d8e3b89ceca95d6b
This commit is contained in:
parent
fdbd17ca71
commit
fb5906a208
@ -6,6 +6,24 @@
|
||||
const parsePropertiesFile = require("devtools/shared/node-properties/node-properties");
|
||||
const { sprintf } = require("devtools/shared/sprintfjs/sprintf");
|
||||
|
||||
const propertiesMap = {};
|
||||
|
||||
/**
|
||||
* Memoized getter for properties files that ensures a given url is only required and
|
||||
* parsed once.
|
||||
*
|
||||
* @param {String} url
|
||||
* The URL of the properties file to parse.
|
||||
* @return {Object} parsed properties mapped in an object.
|
||||
*/
|
||||
function getProperties(url) {
|
||||
if (!propertiesMap[url]) {
|
||||
propertiesMap[url] = parsePropertiesFile(require(`raw!${url}`));
|
||||
}
|
||||
|
||||
return propertiesMap[url];
|
||||
}
|
||||
|
||||
/**
|
||||
* Localization convenience methods.
|
||||
*
|
||||
@ -17,14 +35,6 @@ function LocalizationHelper(stringBundleName) {
|
||||
}
|
||||
|
||||
LocalizationHelper.prototype = {
|
||||
get properties() {
|
||||
if (!this._properties) {
|
||||
this._properties = parsePropertiesFile(require(`raw!${this.stringBundleName}`));
|
||||
}
|
||||
|
||||
return this._properties;
|
||||
},
|
||||
|
||||
/**
|
||||
* L10N shortcut function.
|
||||
*
|
||||
@ -32,8 +42,9 @@ LocalizationHelper.prototype = {
|
||||
* @return string
|
||||
*/
|
||||
getStr: function (name) {
|
||||
if (name in this.properties) {
|
||||
return this.properties[name];
|
||||
let properties = getProperties(this.stringBundleName);
|
||||
if (name in properties) {
|
||||
return properties[name];
|
||||
}
|
||||
|
||||
throw new Error("No localization found for [" + name + "]");
|
||||
|
Loading…
Reference in New Issue
Block a user