Martin Hickey 2016-08-19 13:44:39 +01:00
parent db11a2db24
commit 8c39a8d5e9
4 changed files with 32 additions and 54 deletions

View file

@ -60,6 +60,19 @@ function getLocaleFromFileName(fullFileName) {
return locale;
}
/**
* The translation file is registered with i18n plugin. The plugin contains a list of registered translation file paths per language.
* @param {string} absolutePluginTranslationFilePath - Absolute path to the translation file to register.
*/
module.exports.registerTranslations = registerTranslations;
/**
* Return all translations registered for a particular locale. If a translation is unavailable for the locale then default locale is used.
* @param {string} locale - Translation locale to be returned
* @return {Promise} - A Promise object which will contain on resolve a JSON object of all registered translations
*/
module.exports.getRegisteredLocaleTranslations = getRegisteredLocaleTranslations;
/**
* Returns list of all registered locales.
* @return {list} - Registered locales
*/
module.exports.getRegisteredTranslationLocales = getRegisteredTranslationLocales;

View file

@ -1,35 +0,0 @@
import i18n from './i18n';
/*
Manages the locale translations for Kibana. Responsible for loading translated content per locale.
API:
Register translations:
registerTranslations(<absolute_path_to_translation_file>)
The translation file is registered with i18n plugin. The plugin contains a list of registered translation file paths per language.
Fetch the list of currently supported locales:
List getRegisteredTranslationLocales()
Returns a list of all locales as locale codes for which translations are registered
Fetch a specific locale translated content bundle:
Promise getRegisteredLocaleTranslations(<locale_code>)
Returns a Promise object which will contain on resolve a JSON object of all registered translations for the locale code specified
*/
let registerTranslations = function (absolutePluginTranslationFilePath) {
return i18n.registerTranslations(absolutePluginTranslationFilePath);
};
let getRegisteredLocaleTranslations = function (locale) {
return i18n.getRegisteredLocaleTranslations(locale);
};
let getRegisteredTranslationLocales = function () {
return i18n.getRegisteredTranslationLocales();
};
module.exports.registerTranslations = registerTranslations;
module.exports.getRegisteredLocaleTranslations = getRegisteredLocaleTranslations;
module.exports.getRegisteredTranslationLocales = getRegisteredTranslationLocales;

View file

@ -1,7 +1,7 @@
import Promise from 'bluebird';
import { mkdirp as mkdirpNode } from 'mkdirp';
import manageUuid from './server/lib/manage_uuid';
import i18nPlugin from '../i18n/server/i18n/index';
import i18nPlugin from '../i18n/server/i18n/i18n';
import ingest from './server/routes/api/ingest';
import fromRoot from '../../utils/from_root';
import search from './server/routes/api/search';
@ -126,6 +126,5 @@ module.exports = function (kibana) {
function registerCoreTranslations()
{
const corePluginTranslationFile = fromRoot('/src/core_plugins/kibana/i18n/en.json');
console.log('!! Regsiter file: ', corePluginTranslationFile);
i18nPlugin.registerTranslations(corePluginTranslationFile);
}

View file

@ -10,7 +10,7 @@ import UiExports from './ui_exports';
import UiBundle from './ui_bundle';
import UiBundleCollection from './ui_bundle_collection';
import UiBundlerEnv from './ui_bundler_env';
import i18nPlugin from '../core_plugins/i18n/server/i18n/index';
import i18nPlugin from '../core_plugins/i18n/server/i18n/i18n';
import langParser from 'accept-language-parser';
let kibanaTranslations = [];
@ -144,11 +144,19 @@ function translate(key) {
function getTranslationLocale(acceptLanguages) {
let localeStr = '';
let foundLang = false;
if (acceptLanguages === null || acceptLanguages.length <= 0) {
return DEFAULT_LOCALE;
}
localeStr = getTranslationLocaleExactMatch(acceptLanguages);
if (localeStr != null) {
return localeStr;
}
localeStr = getTranslationLocaleBestCaseMatch(acceptLanguages);
}
function getTranslationLocaleExactMatch(acceptLanguages) {
let localeStr = '';
const acceptLangsLen = acceptLanguages.length;
const registeredLocales = i18nPlugin.getRegisteredTranslationLocales();
@ -161,14 +169,16 @@ function getTranslationLocale(acceptLanguages) {
localeStr = language.code;
}
if (registeredLocales.indexOf(localeStr) > -1) {
foundLang = true;
break;
return localeStr;
}
}
if (foundLang) {
return localeStr;
}
return null;
}
function getTranslationLocaleBestCaseMatch(acceptLanguages, registeredLocales) {
let localeStr = '';
const acceptLangsLen = acceptLanguages.length;
const regLangsLen = registeredLocales.length;
for (let indx = 0; indx < acceptLangsLen; indx++) {
const language = acceptLanguages[indx];
@ -176,18 +186,9 @@ function getTranslationLocale(acceptLanguages) {
for (let regIndx = 0; regIndx < regLangsLen; regIndx++) {
const locale = registeredLocales[regIndx];
if (locale.match('^' + locale)) {
localeStr = locale;
foundLang = true;
break;
return locale;
}
}
if (foundLang) {
break;
}
}
if (foundLang) {
return localeStr;
}
return DEFAULT_LOCALE;
}