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

@ -0,0 +1,3 @@
{
}

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_;

File diff suppressed because it is too large Load Diff

View File

@ -63,6 +63,7 @@
"jshint": "^2.9.4",
"jshint-loader": "^0.8.3",
"jshint-stylish": "^2.2.0",
"json-loader": "^0.5.4",
"karma": "^1.4.1",
"karma-chrome-launcher": "^1.0.1",
"karma-coverage": "^1.1.1",

View File

@ -69,19 +69,27 @@ var baseConfig = function() {
new webpack.optimize.CommonsChunkPlugin('vendor', 'tower.vendor.js')
],
module: {
loaders: [{
// disable AMD loading (broken in this lib) and default to CommonJS (not broken)
test: /\.angular-tz-extensions.js$/,
loader: 'imports?define=>false'
}, {
// es6 -> es5
test: /\.js$/,
loader: 'babel-loader',
exclude: /(node_modules)/,
query: {
presets: ['es2015']
loaders: [
{
// disable AMD loading (broken in this lib) and default to CommonJS (not broken)
test: /\.angular-tz-extensions.js$/,
loader: 'imports?define=>false'
},
{
// es6 -> es5
test: /\.js$/,
loader: 'babel-loader',
exclude: /(node_modules)/,
query: {
presets: ['es2015']
}
},
{
test: /\.json$/,
loader: 'json-loader',
exclude: /(node_modules)/
}
}]
]
},
resolve: {
alias: {