From 6d142c959632542cde76cb708ff40322330e9292 Mon Sep 17 00:00:00 2001 From: David Cheung Date: Thu, 21 Jul 2016 17:13:23 -0400 Subject: [PATCH] Globalize strings to end user --- .gitignore | 2 ++ index.js | 2 ++ intl/en/messages.json | 21 +++++++++++++++++++++ lib/bundler.js | 3 ++- lib/plugin-base.js | 5 +++-- lib/plugins/application.js | 3 ++- lib/plugins/boot-script.js | 3 ++- lib/plugins/middleware.js | 17 +++++++++-------- lib/plugins/mixin.js | 5 +++-- lib/plugins/model.js | 7 ++++--- lib/utils.js | 13 +++++++------ package.json | 1 + 12 files changed, 58 insertions(+), 24 deletions(-) create mode 100644 intl/en/messages.json diff --git a/.gitignore b/.gitignore index 6e610ce..b66e16b 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,5 @@ generated-instructions*.json checkstyle.xml loopback-boot-*.tgz /test/sandbox/ +intl/* +!intl/en/* \ No newline at end of file diff --git a/index.js b/index.js index f2e7eab..4169fc6 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,8 @@ var PluginBase = require('./lib/plugin-base'); var Bootstrapper = require('./lib/bootstrapper').Bootstrapper; var addInstructionsToBrowserify = require('./lib/bundler'); +var SG = require('strong-globalize'); +SG.SetRootDir(__dirname); /** * Initialize an application from an options object or diff --git a/intl/en/messages.json b/intl/en/messages.json new file mode 100644 index 0000000..eb26b8d --- /dev/null +++ b/intl/en/messages.json @@ -0,0 +1,21 @@ +{ + "ae47060728bbbccb3b6279bfce9895e8": "Discarding middleware instructions, loopback client does not support middleware.", + "69746d336c89bf4bb371a6c2fe56304d": "{0} does not resolve to a valid value, returned as {1}. \"{2}\" must be resolvable in Environment variable or by app.get().", + "cdbbcf09fd7f613f3a3e4eacba0e614f": "WARNING: Main config file \"{0}.json\" is missing", + "3a7049e42006e8bc19e0f4fc8df63b6b": "The `app` is powered by an incompatible loopback version {0}. Supported versions: {1}", + "70654dc6eb565613a33344efed3de998": "Failed loading boot script: {0}\n{1}", + "0c8892b57ac28e5bdeb28f6d4eeace0f": "Middleware must be an array", + "33e14d1e42c43970490614c1fa38dbd3": "Middleware phases must be an array", + "35c9468dd1fc15ae5ee90a4801467772": "The phase \"{0}\" is not defined in the main config.", + "75f38e979d1caf27338aba1122e42884": "Middleware factory must be a function", + "e422b92bace89e40433df300053440c1": "The middleware \"{0}\" in phase \"{1}\"is not defined in the main config.", + "fdc23df1bd0fe55fe3faabcc89ff60f3": "Middleware \"{0}\" not found: {1}", + "34319676975b1abf107da7a056abb434": "Invalid normalization format - \"{0}\"", + "4d052d84c8620730afd4a30832f11724": "Cannot configure unknown model {0}", + "5b0d7d21c4b507336a96242c7f29b788": "The data in model-config.json is in the unsupported 1.x format.", + "6037512314fac9d12af6c654a3804823": "Built-in model {0} should have been defined", + "1e5fea50eef843cbffd1d438494912c8": "Cannot resolve path \"{0}\"", + "6447e6b342a2c51ab0bc53b3cbdf3742": "Ordering conflict: cannot add \"{0}\" after \"{1}\", because the opposite order was already specified", + "978a25819e71602cad691dbe7ba17592": "{0} config must be a valid JSON object", + "be2dcdab7aa493ed8d77287eb45cfec8": "cannot require directory contents without directory name" +} diff --git a/lib/bundler.js b/lib/bundler.js index a2f3b1e..7cc5de3 100644 --- a/lib/bundler.js +++ b/lib/bundler.js @@ -7,6 +7,7 @@ var fs = require('fs'); var path = require('path'); var commondir = require('commondir'); var cloneDeep = require('lodash').cloneDeep; +var g = require('strong-globalize')(); /** * Add boot instructions to a browserify bundler. @@ -99,7 +100,7 @@ function bundleInstructions(context, bundler) { var hasMiddleware = instructions.middleware.phases.length || instructions.middleware.middleware.length; if (hasMiddleware) { - console.warn( + g.warn( 'Discarding middleware instructions,' + ' loopback client does not support middleware.'); } diff --git a/lib/plugin-base.js b/lib/plugin-base.js index 05ea50f..c6b9708 100644 --- a/lib/plugin-base.js +++ b/lib/plugin-base.js @@ -8,6 +8,7 @@ var path = require('path'); var debug = require('debug')('loopback:boot:plugin'); var assert = require('assert'); var _ = require('lodash'); +var g = require('strong-globalize')(); module.exports = PluginBase; @@ -89,7 +90,7 @@ PluginBase.prototype.findConfigFiles = function(rootDir, env, name, exts) { var master = ifExists(name + '.json'); if (!master && (ifExistsWithAnyExt(name + '.local') || ifExistsWithAnyExt(name + '.' + env))) { - console.warn('WARNING: Main config file "' + name + '.json" is missing'); + g.warn('WARNING: Main config file "%s.json" is missing', name); } if (!master) return []; @@ -280,7 +281,7 @@ function getConfigVariable(app, param, useEnvVars) { // it will now return `undefined`, for the use case of // dynamic datasources url:`undefined` to fallback to other parameters configVariable = undefined; - console.warn('%s does not resolve to a valid value, returned as %s. ' + + g.warn('%s does not resolve to a valid value, returned as %s. ' + '"%s" must be resolvable in Environment variable or by app.get().', param, configVariable, varName); debug('Dynamic Configuration: Cannot resolve variable for `%s`, ' + diff --git a/lib/plugins/application.js b/lib/plugins/application.js index 9e12343..73d5af6 100644 --- a/lib/plugins/application.js +++ b/lib/plugins/application.js @@ -6,6 +6,7 @@ var util = require('util'); var assert = require('assert'); var semver = require('semver'); +var g = require('strong-globalize')(); var format = require('util').format; var PluginBase = require('../plugin-base'); @@ -28,7 +29,7 @@ function assertLoopBackVersion(app) { // while loopback-boot treats pre-releases the same way as regular versions var version = (loopback.version || '1.0.0').replace(/-.*$/, ''); if (!semver.satisfies(version, RANGE)) { - var msg = format( + var msg = g.f( 'The `app` is powered by an incompatible loopback version %s. ' + 'Supported versions: %s', loopback.version || '(unknown)', diff --git a/lib/plugins/boot-script.js b/lib/plugins/boot-script.js index 898d5c3..ef34114 100644 --- a/lib/plugins/boot-script.js +++ b/lib/plugins/boot-script.js @@ -10,6 +10,7 @@ var async = require('async'); var debug = require('debug')('loopback:boot:script'); var PluginBase = require('../plugin-base'); var _ = require('lodash'); +var g = require('strong-globalize')(); module.exports = function(options) { return new Script(options); @@ -67,7 +68,7 @@ function runScripts(app, list, callback) { }); } } catch (err) { - console.error('Failed loading boot script: %s\n%s', filepath, err.stack); + g.error('Failed loading boot script: %s\n%s', filepath, err.stack); throw err; } }); diff --git a/lib/plugins/middleware.js b/lib/plugins/middleware.js index ab15e12..79220e7 100644 --- a/lib/plugins/middleware.js +++ b/lib/plugins/middleware.js @@ -10,6 +10,7 @@ var _ = require('lodash'); var cloneDeepWith = _.cloneDeepWith; var cloneDeep = _.cloneDeep; var debug = require('debug')('loopback:boot:middleware'); +var g = require('strong-globalize')(); var PluginBase = require('../plugin-base'); var utils = require('../utils'); @@ -31,10 +32,10 @@ Middleware.prototype.merge = function(target, config, fileName) { if (phase in target) { err = this.mergePhaseConfig(target[phase], config[phase], phase); } else { - err = 'The phase "' + phase + '" is not defined in the main config.'; + err = g.f('The phase "%s" is not defined in the main config.', phase); } if (err) - throw new Error('Cannot apply ' + fileName + ': ' + err); + throw new Error(g.ft('Cannot apply %s: %s', fileName, err)); } }; @@ -68,8 +69,8 @@ Middleware.prototype.mergePhaseConfig = function(target, config, phase) { err = this.mergeObjects(targetMiddleware, configMiddleware); } } else { - err = 'The middleware "' + mw + '" in phase "' + phase + '"' + - 'is not defined in the main config.'; + err = g.f('The middleware "%s" in phase "%s"' + + 'is not defined in the main config.', mw, phase); } if (err) return err; } @@ -93,7 +94,7 @@ Middleware.prototype.buildInstructions = function(context, rootDir, config) { // if a non-optional middleware is not resolvable, it will throw // at resolveAppPath() and not reach here if (!resolved.sourceFile) { - return console.log('Middleware "%s" not found: %s', + return g.log('Middleware "%s" not found: %s', middleware, resolved.optional ); @@ -234,11 +235,11 @@ Middleware.prototype.start = function(context) { // Phases can be empty var phases = instructions.phases || []; assert(Array.isArray(phases), - 'Middleware phases must be an array'); + g.f('Middleware phases must be an array')); var middleware = instructions.middleware; assert(Array.isArray(middleware), - 'Middleware must be an array'); + g.f('Middleware must be an array')); debug('Defining middleware phases %j', phases); app.defineMiddlewarePhases(phases); @@ -251,7 +252,7 @@ Middleware.prototype.start = function(context) { factory = factory[data.fragment].bind(factory); } assert(typeof factory === 'function', - 'Middleware factory must be a function'); + g.f('Middleware factory must be a function')); data.config = self.getUpdatedConfigObject(context, data.config, { useEnvVars: true }); app.middlewareFromConfig(factory, data.config); diff --git a/lib/plugins/mixin.js b/lib/plugins/mixin.js index 5fe7fa6..c2866ba 100644 --- a/lib/plugins/mixin.js +++ b/lib/plugins/mixin.js @@ -9,6 +9,7 @@ var path = require('path'); var PluginBase = require('../plugin-base'); var _ = require('lodash'); var debug = require('debug')('loopback:boot:mixin'); +var g = require('strong-globalize')(); var utils = require('../utils'); var tryResolveAppPath = utils.tryResolveAppPath; @@ -162,8 +163,8 @@ function normalizeMixinName(str, options) { return normalization(str); } - var err = new Error('Invalid normalization format - "' + - normalization + '"'); + var err = new Error(g.f('Invalid normalization format - "%s"', + normalization)); err.code = 'INVALID_NORMALIZATION_FORMAT'; throw err; } diff --git a/lib/plugins/model.js b/lib/plugins/model.js index f9c249a..8bdad69 100644 --- a/lib/plugins/model.js +++ b/lib/plugins/model.js @@ -8,6 +8,7 @@ var util = require('util'); var PluginBase = require('../plugin-base'); var path = require('path'); var debug = require('debug')('loopback:boot:model'); +var g = require('strong-globalize')(); var _ = require('lodash'); var toposort = require('toposort'); var utils = require('../utils'); @@ -235,7 +236,7 @@ function assertIsValidModelConfig(config) { if (unsupported) { throw new Error( - 'The data in model-config.json is in the unsupported 1.x format.'); + g.f('The data in model-config.json is in the unsupported 1.x format.')); } } } @@ -267,12 +268,12 @@ Model.prototype.start = function(context) { if (!data.definition) { model = registry.getModel(name); if (!model) { - throw new Error('Cannot configure unknown model ' + name); + throw new Error(g.f('Cannot configure unknown model %s', name)); } debug('Configuring existing model %s', name); } else if (isBuiltinLoopBackModel(app, data)) { model = registry.getModel(name); - assert(model, 'Built-in model ' + name + ' should have been defined'); + assert(model, g.f('Built-in model %s should have been defined', name)); debug('Configuring built-in LoopBack model %s', name); } else { debug('Creating new model %s %j', name, data.definition); diff --git a/lib/utils.js b/lib/utils.js index fcf27ff..14de2a6 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -7,6 +7,7 @@ var debug = require('debug')('loopback:boot'); var path = require('path'); var Module = require('module'); var fs = require('fs'); +var g = require('strong-globalize')(); var assert = require('assert'); var _ = require('lodash'); @@ -30,7 +31,7 @@ var FILE_EXTENSION_JSON = exports.FILE_EXTENSION_JSON = '.json'; */ function findScripts(dir, extensions) { - assert(dir, 'cannot require directory contents without directory name'); + assert(dir, g.t('cannot require directory contents without directory name')); var files = tryReadDir(dir); extensions = extensions || _.keys(require.extensions); @@ -141,7 +142,7 @@ function fixFileExtension(filepath, files, onlyScriptsExportingFunction) { function resolveAppPath(rootDir, relativePath, resolveOptions) { var resolvedPath = tryResolveAppPath(rootDir, relativePath, resolveOptions); if (resolvedPath === undefined && !resolveOptions.optional) { - var err = new Error('Cannot resolve path "' + relativePath + '"'); + var err = new Error(g.f('Cannot resolve path "%s"', relativePath)); err.code = 'PATH_NOT_FOUND'; throw err; } @@ -241,7 +242,7 @@ function tryResolveAppPath(rootDir, relativePath, resolveOptions) { function assertIsValidConfig(name, config) { if (config) { assert(typeof config === 'object', - name + ' config must be a valid JSON object'); + g.f('%s config must be a valid JSON object', name)); } } @@ -313,9 +314,9 @@ function mergePhaseNameLists(currentNames, namesToMerge) { // A new phase - try to add it after the last one, // unless it was already registered if (targetArray.indexOf(valueToAdd) !== -1) { - throw new Error('Ordering conflict: cannot add "' + valueToAdd + - '" after "' + previousValue + '", because the opposite order was ' + - ' already specified'); + throw new Error(g.f('Ordering conflict: cannot add "%s" after "%s",' + + ' because the opposite order was already specified', + valueToAdd, previousValue)); } var previousIx = targetArray.indexOf(previousValue); targetArray.splice(previousIx + 1, 0, valueToAdd); diff --git a/package.json b/package.json index f0c807c..9cc9bc9 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "debug": "^2.2.0", "lodash": "^4.13.1", "semver": "^5.1.0", + "strong-globalize": "^2.5.5", "toposort": "^1.0.0" }, "devDependencies": {