From c14d06893073f54a9a91ac29251265c5bb600ea4 Mon Sep 17 00:00:00 2001 From: Khaled Mohamed Date: Fri, 30 Jun 2017 22:06:42 -0400 Subject: [PATCH] feat(language-support): To be able to translate only supported languges in google or yandex service --- lib/translate-json-object.js | 9 +++++++++ lib/util/constant.js | 4 ++++ lib/util/valid-lang.js | 15 +++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 lib/util/constant.js create mode 100644 lib/util/valid-lang.js diff --git a/lib/translate-json-object.js b/lib/translate-json-object.js index f4eea46..6bc8fe1 100644 --- a/lib/translate-json-object.js +++ b/lib/translate-json-object.js @@ -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'; @@ -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 @@ -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'); } @@ -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); diff --git a/lib/util/constant.js b/lib/util/constant.js new file mode 100644 index 0000000..b3d69bb --- /dev/null +++ b/lib/util/constant.js @@ -0,0 +1,4 @@ +module.exports = { + GOOGLE_NAME: 'Google', + YANDEX_NAME: 'Yandex' +}; diff --git a/lib/util/valid-lang.js b/lib/util/valid-lang.js new file mode 100644 index 0000000..af52eea --- /dev/null +++ b/lib/util/valid-lang.js @@ -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; + } +};