From 7b9adb3b49e8499de5dbb3fd796480f0dbf9c7ac Mon Sep 17 00:00:00 2001 From: Mike McNeil Date: Thu, 16 Jan 2014 15:50:03 -0600 Subject: [PATCH] Added gettext accessor, and pulled out a few strings in sails new. --- bin/sails-new.js | 21 ++++++------- locales/en.js | 7 ++++- locales/gettext.js | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 locales/gettext.js diff --git a/bin/sails-new.js b/bin/sails-new.js index 71ed671f3..29cfdc6d4 100644 --- a/bin/sails-new.js +++ b/bin/sails-new.js @@ -9,6 +9,7 @@ var package = require('../package.json') , reportback = require('reportback')() , rc = require('rc') , _ = require('lodash') + , gettext = require('../locales/gettext') , sailsgen = require('sails-generate'); @@ -47,17 +48,15 @@ module.exports = function () { // Peel off the rest of the args scope.args = cliArguments; - // return sailsgen( scope, reportback.extend({ - // error: reportback.log.error, - // success: function() { - // util.format(require('.')) - // 'Created a new sails app `' + scope.appName + '` at ' + scope.appPath + '.' - // reportback.log.info(); - // }, - // missingAppName: function () { - // reportback.log.error('Please choose the name or destination path for your new app.'); - // } - // })); + return sailsgen( scope, reportback.extend({ + error: reportback.log.error, + success: function() { + reportback.log.info( gettext(cli.new.success, [scope.appName, scope.appPath]) ); + }, + missingAppName: function () { + reportback.log.error( gettext(cli.new.missingAppName) ); + } + })); }; diff --git a/locales/en.js b/locales/en.js index f8df79999..7a51691f4 100644 --- a/locales/en.js +++ b/locales/en.js @@ -1,10 +1,15 @@ /** - * English stringfile + * English stringfile for output messages in the Sails runtime & command-line tool. + * * @type {Object} */ module.exports = { cli: { + new: { + success: 'Created a new sails app `%s` at %s.', + missingAppName: 'Please choose the name or destination path for your new app.' + }, invalid: 'Sorry, I don\'t understand what that means.', toGetHelp: 'To get help using the Sails command-line tool, run:\n` $ sails --help `' }, diff --git a/locales/gettext.js b/locales/gettext.js new file mode 100644 index 000000000..d9e4f9313 --- /dev/null +++ b/locales/gettext.js @@ -0,0 +1,76 @@ +/** + * Module dependencies + */ +var util = require('util'); + + +/** + * Look up a string by name, using environment variable + * to detect the appropriate locale. + * If a stringfile doesn't exist, default to `en`. + * + * @api experimental + * + * @address {String} keypath [e.g. 'cli.new.successful'] + * @address {Object} args [ordered scope args for `util.format()`] + * @return {String} + */ +module.exports = function getString (keypath, args) { + + var locale = + process.env.LANGUAGE || + process.env.LC_ALL || + process.env.LC_MESSAGES || + process.env.LANG || + 'en'; + locale = locale.toLowerCase(); + + // Just to be safe, check that there are no '/' + // (in case an attacker attempts to use environment + // variables to load malicious code) + if (locale.match(/[^\/]+/)) return util.format('INVALID LOCALE: %s',locale); + + var stringfile; + try { + stringfile = require('./'+locale); + } + catch(e) { + return util.format( + 'ERROR LOADING LOCALE: '+ + 'Unable to find stringfile for locale `%s`', + locale); + } + + var strtemplate = _deepValue(stringfile, keypath); + if (!strtemplate) return util.format('STRING `%s` NOT DEFINED IN `%s` LOCALE!',keypath, locale); + + return util.format.apply( null, [strtemplate].concat(args || [])); +}; + + + +/** + * @api private + * + * Lookup a value in an object given a keypath. + * + * @param {Object} object [description] + * @param {String} path [description] + * @return {[type]} [description] + */ +function _deepValue (object, path) { + if ('undefined' == typeof object || object === null) { + return null; + } + var val = object; + path = path.split('.'); + while (path.length) { + var part = path.shift(); + if ('undefined' == typeof val[part]) { + return null; + } else { + val = val[part]; + } + } + return val; +} \ No newline at end of file