Skip to content

Commit

Permalink
feat(language-support): To be able to translate only supported langug…
Browse files Browse the repository at this point in the history
…es in google or yandex service
  • Loading branch information
KhaledMohamedP committed Jul 1, 2017
1 parent 325f7af commit c14d068
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/translate-json-object.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var _ = require('lodash');
var Promise = require('promise');
var constant = require('./util/constant');
var isValidLang = require('./util/valid-lang');

var MISSING_TOKEN_ERROR_MESSAGE = 'Please provide a option.googleApiKey, or option.yandexApiKey (token key) via invoking the [init] method';

Expand All @@ -15,6 +17,7 @@ function TranslateJSONObject() {
// The list of promises that should be resolve prior to returning the full `Object translation`
var promises = [];
var destObj = {};
var serviceType;

/**
* init - Initialize the setting of your module instance, it takes a setting object
Expand All @@ -29,8 +32,10 @@ function TranslateJSONObject() {
console.warn(MISSING_TOKEN_ERROR_MESSAGE);
return false;
} else if (setting.yandexApiKey) {
serviceType = constant.YANDEX_NAME;
translateSrv = require('./service/yandex.js');
} else {
serviceType = constant.GOOGLE_NAME;
translateSrv = require('./service/google.js');
}

Expand All @@ -54,6 +59,10 @@ function TranslateJSONObject() {
return Promise.reject('Please provide a language param [type String] e.g. translate(obj, es)');
}

if(!isValidLang(language, serviceType)) {
return Promise.reject(serviceType + ' doesn\'t support the language you specified [' + language + '], please try another language');
}

function recurisveTranslateObject(destObj, srcObj) {
// Loop through the entire object collection
_.forEach(srcObj, loopHandler);
Expand Down
4 changes: 4 additions & 0 deletions lib/util/constant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
GOOGLE_NAME: 'Google',
YANDEX_NAME: 'Yandex'
};
15 changes: 15 additions & 0 deletions lib/util/valid-lang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var constant = require('./constant');

var google = ['af', 'am', 'ar', 'az', 'be', 'bg', 'bn', 'bs', 'ca', 'ceb', 'co', 'cs', 'cy', 'da', 'de', 'el', 'en', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fr', 'fy', 'ga', 'gd', 'gl', 'gu', 'ha', 'haw', 'hi', 'hmn', 'hr', 'ht', 'hu', 'hy', 'id', 'ig', 'is', 'it', 'iw', 'ja', 'jw', 'ka', 'kk', 'km', 'kn', 'ko', 'ku', 'ky', 'la', 'lb', 'lo', 'lt', 'lv', 'ma', 'mg', 'mi', 'mk', 'ml', 'mn', 'mr', 'ms', 'mt', 'my', 'ne', 'nl', 'no', 'ny', 'pl', 'ps', 'pt', 'ro', 'ru', 'sd', 'si', 'sk', 'sl', 'sm', 'sn', 'so', 'sq', 'sr', 'st', 'su', 'sv', 'sw', 'ta', 'te', 'tg', 'th', 'tl', 'tr', 'uk', 'ur', 'uz', 'vi', 'xh', 'yi', 'yo', 'zh-CN', 'zh-TW', 'zu'];
var yandex = ['af', 'am', 'ar', 'az', 'ba', 'be', 'bg', 'bn', 'bs', 'ca', 'ceb', 'cs', 'cy', 'da', 'de', 'el', 'en', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fr', 'ga', 'gd', 'gl', 'gu', 'he', 'hi', 'hr', 'ht', 'hu', 'hy', 'id', 'is', 'it', 'ja', 'jv', 'ka', 'kk', 'km', 'kn', 'ko', 'ky', 'la', 'lb', 'lo', 'lt', 'lv', 'mg', 'mhr', 'mi', 'mk', 'ml', 'mn', 'mr', 'mrj', 'ms', 'mt', 'my', 'ne', 'nl', 'no', 'pa', 'pap', 'pl', 'pt', 'ro', 'ru', 'si', 'sk', 'sl', 'sq', 'sr', 'su', 'sv', 'sw', 'ta', 'te', 'tg', 'th', 'tl', 'tr', 'tt', 'udm', 'uk', 'ur', 'uz', 'vi', 'xh', 'yi', 'zh'];

module.exports = function (lang, service) {
switch (service) {
case constant.GOOGLE_NAME:
return google.indexOf(lang) !== -1;
case constant.YANDEX_NAME:
return yandex.indexOf(lang) !== -1;
default:
return false;
}
};

0 comments on commit c14d068

Please sign in to comment.