Skip to content

Commit

Permalink
feat: add support to translations
Browse files Browse the repository at this point in the history
  • Loading branch information
gerard2perez committed Apr 21, 2017
1 parent b643732 commit f6480cf
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"cross-spawn": "^5.0.1",
"fs-extra": "^2.0.0",
"glob": "^7.0.5",
"google-translate-api": "^2.2.2",
"gulp-refresh": "^1.1.0",
"imagemin": "^5.2.1",
"imagemin-mozjpeg": "^6.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const setupAssets = async function setupAssets () {
};
const setupOthers = async function setupOthers () {
await utils.mkdir(Project('controllers'));
await utils.mkdir(Project('locals'));
await utils.mkdir(Project('locales'));
await utils.mkdir(Project('seeds'));
await utils.mkdir(Project('models'));
await utils.mkdir(Project('public'));
Expand Down
58 changes: 58 additions & 0 deletions src/commands/translate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'colors';
import Command from 'cmd-line/lib/Command';
import * as fs from 'fs-extra';
import lang from '../support/languages';

export default (new Command(__filename, 'Translate your localization files'))
.Args('?to', '?from')
.Options(
['-l', '--list', 'Show a list of languages'],
['-g', '--generate', 'indicate to genereate the translation file.']
)
.Action(async function (to, from, options) {
from = from || configuration.server.localization.locales[0];
if (options.list) {
for (const key of Object.keys(lang)) {
console.log(key + ' ' + lang[key]);
}
return 0;
}
if (!from) {
console.log('No source locale, please check your configuration');
return 1;
}
if (!lang[to]) {
console.log('No target locale, please check your configuration');
return 1;
}
if (from === to) {
console.log('cannot translate to the same language');
return 0;
}
const GET = require('../functions/get').default;
async function translate (text, from, to) {
let r;
r = JSON.parse(await GET(`http://api.mymemory.translated.net/get?q=${encodeURI(text)}&langpair=${from}|${to}`, null, null));
if (r.responseData.translatedText.indexOf('YOU USED ALL AVAILABLE FREE TRANSLATIONS') > -1) {
r = await GET(`http://translate.google.com/translate_a/single?client=ctx&sl=${from}&tl=${to}&hl=es&dt=t&q=${encodeURI(text)}`);
return r[0][0][0];
} else if (r.responseStatus === 200) {
return r.responseData.translatedText.replace(new RegExp(`\b${from}$`, 'g'), to);
} else if (r.responseStatus === 403) {
return null;
} else {
console.log(lang[to], r.responseData);
return text;
}
}
let translation = fs.readJSONSync(ProyPath(configuration.server.localization.directory, `${from}.js`));
let newLang = {};
for (const key of Object.keys(translation)) {
try {
newLang[key] = await translate(translation[key], from, to);
} catch (ex) {
console.log(ex);
}
}
fs.writeFileSync(ProyPath(configuration.server.localization.directory, `${to}.js`), JSON.stringify(newLang, 4, 4));
});
1 change: 1 addition & 0 deletions src/deamon.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export default function StartKoatonServer (resolve, reject, EmberPids, nginx = f
'commands',
'koaton_modules',
'views',
'downloads',
'config/bundles.js',
'*.tmp',
'*.json',
Expand Down
31 changes: 31 additions & 0 deletions src/functions/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const http = require('http');
function GET (hostname) {
return new Promise(function (resolve, reject) {
let [protocol, host] = hostname.split('//');
let url = host.split('/');
host = url[0];
url = url.splice(1, host.length).join('/');
let request = http.request({
protocol: protocol,
hostname: host,
port: 80,
path: `/${url}`,
agent: false // create a new agent just for this one request
});
request.on('response', (response) => {
let result = '';
response.on('data', (chunk) => {
result += chunk.toString();
});
response.on('error', (err) => {
reject(err);
});
response.on('end', () => {
resolve(result);
});
});
request.end();
});
}

export default GET;
106 changes: 106 additions & 0 deletions src/support/Languages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
export default {
'af': 'Afrikaans',
'sq': 'Albanian',
'am': 'Amharic',
'ar': 'Arabic',
'hy': 'Armenian',
'az': 'Azerbaijani',
'eu': 'Basque',
'be': 'Belarusian',
'bn': 'Bengali',
'bo': 'Tibetan',
'bs': 'Bosnian',
'br': 'Breton',
'bg': 'Bulgarian',
'my': 'Burmese',
'ca': 'Catalan; Valencian',
'cs': 'Czech',
'ch': 'Chamorro',
'zh': 'Chinese',
'cy': 'Welsh',
'da': 'Danish',
'de': 'German',
'nl': 'Dutch; Flemish',
'dz': 'Dzongkha',
'el': 'Greek, Modern (1453-)',
'en': 'English',
'eo': 'Esperanto',
'et': 'Estonian',
'fo': 'Faroese',
'fa': 'Persian',
'fi': 'Finnish',
'fr': 'French',
'ka': 'Georgian',
'ga': 'Irish',
'gl': 'Galician',
'gu': 'Gujarati',
'ht': 'Haitian; Haitian Creole',
'he': 'Hebrew',
'hi': 'Hindi',
'hr': 'Croatian',
'hu': 'Hungarian',
'is': 'Icelandic',
'id': 'Indonesian',
'it': 'Italian',
'ja': 'Japanese',
'kl': 'Kalaallisut; Greenlandic',
'kn': 'Kannada',
'kk': 'Kazakh',
'km': 'Central Khmer',
'rw': 'Kinyarwanda',
'ko': 'Korean',
'ku': 'Kurdish',
'lo': 'Lao',
'la': 'Latin',
'lv': 'Latvian',
'lt': 'Lithuanian',
'lb': 'Luxembourgish; Letzeburgesch',
'mk': 'Macedonian',
'ml': 'Malayalam',
'mi': 'Maori',
'mr': 'Marathi',
'mg': 'Malagasy',
'mn': 'Mongolian',
'ne': 'Nepali',
'nn': 'Norwegian Nynorsk; Nynorsk, Norwegian',
'nb': 'Bokmål, Norwegian; Norwegian Bokmål',
'no': 'Norwegian',
'ny': 'Chichewa; Chewa; Nyanja',
'pa': 'Panjabi; Punjabi',
'pl': 'Polish',
'pt': 'Portuguese',
'ps': 'Pushto; Pashto',
'qu': 'Quechua',
'rm': 'Romansh',
'ro': 'Romanian; Moldavian; Moldovan',
'rn': 'Rundi',
'ru': 'Russian',
'si': 'Sinhala; Sinhalese',
'sk': 'Slovak',
'sl': 'Slovenian',
'sm': 'Samoan',
'sn': 'Shona',
'so': 'Somali',
'st': 'Sotho, Southern',
'es': 'Spanish; Castilian',
'sr': 'Serbian',
'sw': 'Swahili',
'sv': 'Swedish',
'ta': 'Tamil',
'te': 'Telugu',
'tl': 'Tagalog',
'th': 'Thai',
'ti': 'Tigrinya',
'to': 'Tonga (Tonga Islands)',
'tk': 'Turkmen',
'tr': 'Turkish',
'uk': 'Ukrainian',
'ur': 'Urdu',
'uz': 'Uzbek',
'vi': 'Vietnamese',
'wo': 'Wolof',
'xh': 'Xhosa',
'yi': 'Yiddish',
'yo': 'Yoruba',
'zu': 'Zulu'
};
1 change: 1 addition & 0 deletions templates/gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
koaton_modules/
bower_components/
/ember/
.DS_Store
Expand Down
6 changes: 6 additions & 0 deletions templates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"main": "app.js",
"scripts": {
"commit": "git add -A & git-cz",
"convert": "koaton-exporter **/*.es6 ./ -e js",
"assets":"node ./node_modules/koaton/scripts/build.js",
"prestart": "npm run assets",
Expand All @@ -19,6 +20,11 @@
"dependencies": {
"koaton":"^1.3.0"
},
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
},
"devDependencies": {
"commitizen": "^2.8.6",
"cz-conventional-changelog": "^1.2.0",
Expand Down
37 changes: 37 additions & 0 deletions test/configuration/translate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as fs from 'fs-extra';
import TestNode from '../support/TestNode';
import ServerConfiguaration from '../../src/support/Server';
import '../support/array';
import langs from '../../src/support/languages';

let tests = [];
let cmdname = 'koaton translate';

tests.push(new TestNode(cmdname, [undefined, undefined, {
list: true
}], true, true))
.SetUp(() => {
process.chdir('testingapp');
process.env.isproyect = 'true';
require(ProyPath('node_modules', 'koaton/lib/support', 'globals'));
global.scfg = new ServerConfiguaration();
scfg.port = 62626;
global.skipshell = true;
fs.writeFileSync(ProyPath('locales', 'en.js'), JSON.stringify({t001: 'Hello'}, 4, 4));
});
for (const lang of Object.keys(langs)) {
tests.push(new TestNode(cmdname, [lang, undefined, {}], true, true))
.Expect(`translates to ${langs[lang]}`, true, (log) => {
if (lang === 'en') {
return log.indexOf('cannot translate to the same language') > -1;
} else {
let translation = fs.readJSONSync(ProyPath('locales', `${lang}.js`));
return translation.t001 && translation.t001 !== 'Hello';
}
});
}

tests.last.CleanUp(() => {
process.chdir('..');
});
export { cmdname as testname, tests as config };
5 changes: 3 additions & 2 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import configuration from './configuration';
const commands = importindex(ProyPath('/src/commands')).default;
const CommandOrder = [].concat([
// 'new', 'adapter', 'ember',
'model',
'relation'
// 'model',
// 'relation'
'translate',
// 'nginx',
// 'install', 'build', 'seed', 'modulify'
// 'serve'
Expand Down
2 changes: 1 addition & 1 deletion test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
--require src/globals.js
--bail

--slow 0

0 comments on commit f6480cf

Please sign in to comment.