Skip to content

Commit

Permalink
Added gettext accessor, and pulled out a few strings in sails new.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikermcneil committed Jan 16, 2014
1 parent ed9926b commit 7b9adb3
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 12 deletions.
21 changes: 10 additions & 11 deletions bin/sails-new.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');


Expand Down Expand Up @@ -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) );
}
}));
};


7 changes: 6 additions & 1 deletion locales/en.js
Original file line number Diff line number Diff line change
@@ -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 `'
},
Expand Down
76 changes: 76 additions & 0 deletions locales/gettext.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 7b9adb3

Please sign in to comment.