move i18n and locales to its own folder; abstract it a bit; detect at initialization; add cmd line option

This commit is contained in:
Iuri Matias 2018-05-09 18:43:09 -04:00
parent 51fab56fc5
commit 517a3bf234
7 changed files with 186 additions and 144 deletions

1
.gitignore vendored
View File

@ -24,3 +24,4 @@ test_apps/contracts_app/chains.json
.embark/
NOTES
npm-debug.log
.tern-port

View File

@ -2,18 +2,9 @@ const program = require('commander');
const promptly = require('promptly');
const utils = require('./utils/utils.js');
const Embark = require('../lib/index');
const i18n = require('i18n');
const i18n = require('./i18n/i18n.js');
let embark = new Embark;
i18n.configure({
locales: ['en', 'pt'],
register: global,
//updateFiles: false,
directory: utils.joinPath(__dirname, '/../locales')
});
i18n.setLocale('pt');
class Cmd {
constructor() {
program.version(embark.version);
@ -118,8 +109,10 @@ class Cmd {
.option('--no-color', __('no colors in case it\'s needed for compatbility purposes'))
.option('--logfile [logfile]', __('filename to output logs (default: %s)', 'none'))
.option('--loglevel [loglevel]', __('level of logging to display') + ' ["error", "warn", "info", "debug", "trace"]', /^(error|warn|info|debug|trace)$/i, 'debug')
.option('--locale [locale]', __('language to use (default: en)'))
.description(__('run dapp (default: %s)', 'development'))
.action(function (env, options) {
i18n.setOrDetectLocale(options.locale);
embark.run({
env: env || 'development',
serverPort: options.port,
@ -252,7 +245,6 @@ class Cmd {
});
}
}
module.exports = Cmd;

42
lib/i18n/i18n.js Normal file
View File

@ -0,0 +1,42 @@
const i18n = require('i18n');
const osLocale = require('os-locale');
const path = require('path');
const supported_languages = ['en', 'pt'];
i18n.configure({
locales: supported_languages,
register: global,
//updateFiles: false,
directory: path.join(__dirname, 'locales')
});
function isSupported(locale) {
return (supported_languages.indexOf(locale.substr(0, 2)) >= 0);
}
function setLocale(locale) {
i18n.setLocale(locale.substr(0, 2));
}
function setDefaultLocale() {
osLocale().then(setLocale).catch();
}
function setOrDetectLocale(locale) {
if (locale && !isSupported(locale)) {
console.log("===== locale " + locale + " not supported =====");
}
if (locale) {
return i18n.setLocale(locale.substr(0, 2));
}
setDefaultLocale();
}
setDefaultLocale();
module.exports = {
i18n: i18n,
setOrDetectLocale: setOrDetectLocale
};

View File

@ -69,5 +69,11 @@
"instantiated web3.js object configured to the current environment": "instantiated web3.js object configured to the current environment",
"to immediatly exit (alias: exit)": "to immediatly exit (alias: exit)",
"The web3 object and the interfaces for the deployed contracts and their methods are also available": "The web3 object and the interfaces for the deployed contracts and their methods are also available",
"versions in use": "versions in use"
"versions in use": "versions in use",
"language to use (default: en)": "language to use (default: en)",
"executing": "executing",
"writing file": "writing file",
"errors found while generating": "errors found while generating",
"Looking for documentation? You can find it at": "Looking for documentation? You can find it at",
"Ready": "Ready"
}

264
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -51,6 +51,7 @@
"live-plugin-manager": "https://github.com/iurimatias/live-plugin-manager.git",
"merge": "^1.2.0",
"orbit-db": "^0.17.3",
"os-locale": "^2.1.0",
"parse-json": "^4.0.0",
"promptly": "^2.1.0",
"propose": "0.0.5",