Add support for configurably including strings

* Add json-loader to load JSON files via webpack
* Add a JSON file for string overrides
* Add a getter method to BaseString for an easier and more clearly
defined approach to accessing string values
This commit is contained in:
gconsidine
2017-07-12 15:40:14 -04:00
parent 87101d18d2
commit 594c4d6ce2
5 changed files with 3766 additions and 13 deletions

View File

@@ -1,16 +1,61 @@
import defaults from '../../assets/default.strings.json';
let i18n;
function BaseStringService (namespace) {
let t = i18n._;
const ERROR_NO_NAMESPACE = t('BaseString cannot be extended without providing a namespace');
const ERROR_NO_STRING = t('No string exists with this name');
if (!namespace) {
throw new Error(ERROR_NO_NAMESPACE);
}
this.t = t;
this[namespace] = {};
/*
* These strings are globally relevant and configured to give priority to values in
* default.strings.json and fall back to defaults defined inline.
*/
this.BRAND_NAME = defaults.BRAND_NAME || 'AWX';
/*
* Globally relevant strings should be defined here to avoid duplication of content across the
* the project.
*/
this.CANCEL = t('CANCEL');
this.SAVE = t('SAVE');
this.OK = t('OK');
}
/**
* This getter searches the extending class' namespace first for a match then falls back to
* the more globally relevant strings defined here. Strings with with dots as delimeters are
* supported to give flexibility to extending classes to nest strings as necessary.
*
* If no match is found, an error is thrown to alert the developer immediately instead of
* failing silently.
*/
this.get = name => {
let keys = name.split('.');
let value;
keys.forEach(key => {
if (!value) {
value = this[namespace][key] || this[key];
} else {
value = value[key];
}
if (!value) {
throw new Error(ERROR_NO_STRING);
}
});
return value;
};
}
function BaseStringServiceLoader (_i18n_) {
i18n = _i18n_;