Add algorithm for determining plugin language when retrieving translations

Client would pass languages used in the 'accept-language' header. These
languages would then be compared against the plugin supported languages
and best compared language would be selected.

To be done:
 - Add REST API tests
This commit is contained in:
Martin Hickey 2016-06-28 22:10:39 +01:00
parent ec6d2b1ca2
commit a75faaea1c
3 changed files with 45 additions and 7 deletions

View file

@ -14,6 +14,7 @@
"devDependencies": {
"@elastic/eslint-config-kibana": "0.0.2",
"@elastic/plugin-helpers": "5.0.0-beta1",
"accept-language-parser": "^1.1.2",
"babel-eslint": "4.1.8",
"bluebird": "^3.4.0",
"boom": "^3.2.1",

View file

@ -17,6 +17,7 @@ var getPluginTranslationDetails = function (pluginTranslationPath, translationFi
return callback(null);
};
//TODO(hickeyma): Update to use https://github.com/elastic/kibana/pull/7562
var getRegisteredPluginStoragePath = function (pluginName) {
return TRANSLATION_STORE_PATH + '/' + pluginName;
};

View file

@ -18,6 +18,7 @@ export default function (server) {
getPluginLanguageTranslations(pluginName, languages, function (err, translations) {
if (err) {
reply(Boom.internal(err));
return;
}
reply(translations);
});
@ -33,10 +34,6 @@ function getPluginLanguageTranslations(pluginName, acceptLanguages, cb) {
return cb (err);
}
if (!language) {
language = DEFAULT_LANGUAGE;
}
i18n.getRegisteredPluginLanguageTranslations(pluginName, language, function (err, translationJson) {
if (err) {
return cb (err);
@ -53,9 +50,48 @@ function getPluginSupportedLanguage(pluginName, acceptLanguages, cb) {
return cb (err);
}
//TODO: Algorithm which returns a languages based on the accept languages
//from the client and languages supported by the plugin
return cb (null, null);
var foundLang = false;
var langStr = '';
acceptLanguages.some(function exactMatch(language) {
if (language.region) {
langStr = language.code + '-' + language.region;
} else {
langStr = language.code;
}
if (languages.indexOf(langStr) > -1) {
foundLang = true;
return true;
} else {
return false;
}
});
if (foundLang) {
return cb (null, langStr);
}
acceptLanguages.some(function partialMatch(language) {
langStr = language.code;
languages.some(function (lang) {
if (lang.match('^' + langStr)) {
langStr = lang;
foundLang = true;
return true;
} else {
return false;
}
});
if (foundLang) {
return true;
} else {
return false;
}
});
if (foundLang) {
return cb (null, langStr);
}
return cb (null, DEFAULT_LANGUAGE);
});
}