mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
Update after review comments
Comments: - https://github.com/elastic/kibana/pull/7545#discussion-diff-72890673 - https://github.com/elastic/kibana/pull/7545#discussion-diff-72894762
This commit is contained in:
parent
32d5034d9c
commit
8c7f51ca5d
5 changed files with 73 additions and 73 deletions
|
@ -41,26 +41,26 @@ describe('Test registering translations for test_plugin_1', function () {
|
|||
});
|
||||
|
||||
it('EN translations are registered' , function (done) {
|
||||
const language = 'en';
|
||||
const locale = 'en';
|
||||
const expectedTranslationJsonFile = DATA_PATH + PATH_SEPARATOR +
|
||||
'reference' + PATH_SEPARATOR + pluginName + PATH_SEPARATOR + language +
|
||||
'reference' + PATH_SEPARATOR + pluginName + PATH_SEPARATOR + locale +
|
||||
'.json';
|
||||
const expectedTranslationJson = require(expectedTranslationJsonFile);
|
||||
checkTranslations(language, expectedTranslationJson, done);
|
||||
checkTranslations(locale, expectedTranslationJson, done);
|
||||
});
|
||||
|
||||
it('DE translations are registered' , function (done) {
|
||||
const language = 'de';
|
||||
const locale = 'de';
|
||||
const expectedTranslationJsonFile = DATA_PATH + PATH_SEPARATOR +
|
||||
'reference' + PATH_SEPARATOR + pluginName + PATH_SEPARATOR + language +
|
||||
'reference' + PATH_SEPARATOR + pluginName + PATH_SEPARATOR + locale +
|
||||
'.json';
|
||||
const expectedTranslationJson = require(expectedTranslationJsonFile);
|
||||
checkTranslations(language, expectedTranslationJson, done);
|
||||
checkTranslations(locale, expectedTranslationJson, done);
|
||||
});
|
||||
|
||||
it('Translation languages are registered', function (done) {
|
||||
const expectedLanguages = ['en', 'de'];
|
||||
checkRegisteredLanguages(expectedLanguages, done);
|
||||
it('Translation locales are registered', function (done) {
|
||||
const expectedLocales = ['en', 'de'];
|
||||
checkRegisteredLocales(expectedLocales, done);
|
||||
});
|
||||
|
||||
after(function (done) {
|
||||
|
@ -114,22 +114,22 @@ describe('Test registering translations for test_plugin_1 and test_plugin_2', fu
|
|||
});
|
||||
|
||||
it('EN translations are registered' , function (done) {
|
||||
const language = 'en';
|
||||
const expectedTranslationJsonFile = DATA_PATH + PATH_SEPARATOR + 'reference' + PATH_SEPARATOR + language + '.json';
|
||||
const locale = 'en';
|
||||
const expectedTranslationJsonFile = DATA_PATH + PATH_SEPARATOR + 'reference' + PATH_SEPARATOR + locale + '.json';
|
||||
const expectedTranslationJson = require(expectedTranslationJsonFile);
|
||||
checkTranslations(language, expectedTranslationJson, done);
|
||||
checkTranslations(locale, expectedTranslationJson, done);
|
||||
});
|
||||
|
||||
it('DE translations are registered' , function (done) {
|
||||
const language = 'de';
|
||||
const expectedTranslationJsonFile = DATA_PATH + PATH_SEPARATOR + 'reference' + PATH_SEPARATOR + language + '.json';
|
||||
const locale = 'de';
|
||||
const expectedTranslationJsonFile = DATA_PATH + PATH_SEPARATOR + 'reference' + PATH_SEPARATOR + locale + '.json';
|
||||
const expectedTranslationJson = require(expectedTranslationJsonFile);
|
||||
checkTranslations(language, expectedTranslationJson, done);
|
||||
checkTranslations(locale, expectedTranslationJson, done);
|
||||
});
|
||||
|
||||
it('Translation languages are registered', function (done) {
|
||||
const expectedLanguages = ['en', 'de'];
|
||||
checkRegisteredLanguages(expectedLanguages, done);
|
||||
it('Translation locales are registered', function (done) {
|
||||
const expectedLocales = ['en', 'de'];
|
||||
checkRegisteredLocales(expectedLocales, done);
|
||||
});
|
||||
|
||||
after(function (done) {
|
||||
|
@ -154,10 +154,10 @@ function compareTranslations(actual, expected) {
|
|||
return equal;
|
||||
}
|
||||
|
||||
function checkTranslations(language, expectedTranslations, done) {
|
||||
function checkTranslations(locale, expectedTranslations, done) {
|
||||
let result = true;
|
||||
|
||||
i18n.getRegisteredLanguageTranslations(language).then(function (actualTranslationJson) {
|
||||
i18n.getRegisteredLocaleTranslations(locale).then(function (actualTranslationJson) {
|
||||
if (!compareTranslations(actualTranslationJson, expectedTranslations)) {
|
||||
result = false;
|
||||
}
|
||||
|
@ -178,18 +178,18 @@ function registerTranslations(pluginTranslationFiles) {
|
|||
});
|
||||
}
|
||||
|
||||
function checkRegisteredLanguages(expectedLanguages, done) {
|
||||
function checkRegisteredLocales(expectedLocales, done) {
|
||||
let result = true;
|
||||
|
||||
i18n.getRegisteredTranslationLanguages().then(function (actualLanguages) {
|
||||
if (actualLanguages.length !== expectedLanguages.length) {
|
||||
i18n.getRegisteredTranslationLocales().then(function (actualLocales) {
|
||||
if (actualLocales.length !== expectedLocales.length) {
|
||||
result = false;
|
||||
} else {
|
||||
let index = actualLanguages.length;
|
||||
actualLanguages.sort();
|
||||
expectedLanguages.sort();
|
||||
let index = actualLocales.length;
|
||||
actualLocales.sort();
|
||||
expectedLocales.sort();
|
||||
while (index--) {
|
||||
if (actualLanguages[index] !== expectedLanguages[index]) {
|
||||
if (actualLocales[index] !== expectedLocales[index]) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -19,13 +19,13 @@ const getTranslationStoragePath = function () {
|
|||
return TRANSLATION_STORE_PATH;
|
||||
};
|
||||
|
||||
const getRegisteredTranslationLanguages = function () {
|
||||
const getRegisteredTranslationLocales = function () {
|
||||
let translationFiles = [];
|
||||
let languageList = [];
|
||||
let localeList = [];
|
||||
const translationStorePath = getTranslationStoragePath();
|
||||
|
||||
return getTranslationDetailsFromDirectory(translationStorePath, translationFiles, languageList).then(function () {
|
||||
return languageList;
|
||||
return getTranslationDetailsFromDirectory(translationStorePath, translationFiles, localeList).then(function () {
|
||||
return localeList;
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -37,9 +37,9 @@ const registerTranslations = function (absolutePluginTranslationFilePath) {
|
|||
});
|
||||
};
|
||||
|
||||
const getRegisteredLanguageTranslations = function (language) {
|
||||
const getRegisteredLocaleTranslations = function (locale) {
|
||||
const translationStorePath = getTranslationStoragePath();
|
||||
const translationFileName = language + '.' + TRANSLATION_FILE_EXTENSION;
|
||||
const translationFileName = locale + '.' + TRANSLATION_FILE_EXTENSION;
|
||||
const translationFile = translationStorePath + PATH_SEPARATOR + translationFileName;
|
||||
|
||||
return readFile(translationFile, 'utf8').then(function (translationStr) {
|
||||
|
@ -47,7 +47,7 @@ const getRegisteredLanguageTranslations = function (language) {
|
|||
try {
|
||||
translationJson = JSON.parse(translationStr);
|
||||
} catch (err) {
|
||||
throw new Error('Bad ' + language + ' translation strings. Reason: ' + err);
|
||||
throw new Error('Bad ' + locale + ' translation strings. Reason: ' + err);
|
||||
}
|
||||
return translationJson;
|
||||
}).catch(function (e) {
|
||||
|
@ -89,29 +89,29 @@ function createTranslationDirectory(translationStorePath) {
|
|||
});
|
||||
}
|
||||
|
||||
function getTranslationDetailsFromDirectory(dir, translationFiles, languageList) {
|
||||
function getTranslationDetailsFromDirectory(dir, translationFiles, localeList) {
|
||||
return readdir(dir).then((dirListing) => {
|
||||
return Promise.map(dirListing, (listing) => {
|
||||
const fullPath = path.join(dir, listing);
|
||||
return stat(fullPath).then((stats) => {
|
||||
if (!stats.isDirectory()) {
|
||||
getTranslationDetailsFromFile(fullPath, translationFiles, languageList);
|
||||
getTranslationDetailsFromFile(fullPath, translationFiles, localeList);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getTranslationDetailsFromFile(fullFileName, translationFiles, languageList) {
|
||||
function getTranslationDetailsFromFile(fullFileName, translationFiles, localeList) {
|
||||
const fileName = getFileName(fullFileName);
|
||||
const fileExt = fileName.split('.').pop();
|
||||
|
||||
if (fileName === fileExt) return;
|
||||
if (fileExt !== TRANSLATION_FILE_EXTENSION) return;
|
||||
translationFiles.push(fullFileName);
|
||||
const lang = fileName.substr(0, fileName.lastIndexOf('.'));
|
||||
if (languageList.indexOf(lang) === -1) {
|
||||
languageList.push(lang);
|
||||
const locale = fileName.substr(0, fileName.lastIndexOf('.'));
|
||||
if (localeList.indexOf(locale) === -1) {
|
||||
localeList.push(locale);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -120,6 +120,6 @@ function getFileName(fullPath) {
|
|||
}
|
||||
|
||||
module.exports.registerTranslations = registerTranslations;
|
||||
module.exports.getRegisteredLanguageTranslations = getRegisteredLanguageTranslations;
|
||||
module.exports.getRegisteredLocaleTranslations = getRegisteredLocaleTranslations;
|
||||
module.exports.getTranslationStoragePath = getTranslationStoragePath;
|
||||
module.exports.getRegisteredTranslationLanguages = getRegisteredTranslationLanguages;
|
||||
module.exports.getRegisteredTranslationLocales = getRegisteredTranslationLocales;
|
||||
|
|
|
@ -1,35 +1,35 @@
|
|||
import i18n from './i18n';
|
||||
|
||||
/*
|
||||
Manages the language translations for Kibana. Responsible for loading translated content per language.
|
||||
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 will be bundled into one translation file per language and stored in the Kibana data directory. Returns a Promise object.
|
||||
The translation file will be bundled into one translation file per locale and stored in the Kibana data directory. Returns a Promise object.
|
||||
|
||||
Fetch the list of currently supported languages:
|
||||
Promise getRegisteredTranslationLanguages()
|
||||
Returns a Promise object which will contain on resolve a list of all languages as language codes for which translations are registered
|
||||
Fetch the list of currently supported locales:
|
||||
Promise getRegisteredTranslationLocales()
|
||||
Returns a Promise object which will contain on resolve a list of all locales as locale codes for which translations are registered
|
||||
|
||||
Fetch a specific language translated content bundle:
|
||||
Promise getRegisteredLanguageTranslations(<language_code>)
|
||||
Returns a Promise object which will contain on resolve a JSON object of all registered translations for the language code specified
|
||||
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 getRegisteredLanguageTranslations = function (language) {
|
||||
return i18n.getRegisteredLanguageTranslations(language);
|
||||
let getRegisteredLocaleTranslations = function (locale) {
|
||||
return i18n.getRegisteredLocaleTranslations(locale);
|
||||
};
|
||||
|
||||
let getRegisteredTranslationLanguages = function () {
|
||||
return i18n.getRegisteredTranslationLanguages();
|
||||
let getRegisteredTranslationLocales = function () {
|
||||
return i18n.getRegisteredTranslationLocales();
|
||||
};
|
||||
|
||||
module.exports.registerTranslations = registerTranslations;
|
||||
module.exports.getRegisteredLanguageTranslations = getRegisteredLanguageTranslations;
|
||||
module.exports.getRegisteredTranslationLanguages = getRegisteredTranslationLanguages;
|
||||
module.exports.getRegisteredLocaleTranslations = getRegisteredLocaleTranslations;
|
||||
module.exports.getRegisteredTranslationLocales = getRegisteredTranslationLocales;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
{
|
||||
"CORE-WELCOME_MESSAGE": "is loading. Give me a moment here. I'm loading a whole bunch of code. Don't worry, all this good stuff will be cached up for next time!",
|
||||
"CORE-WELCOME_MESSAGE": "<strong>Kibana</strong> <small>is loading. Give me a moment here. I'm loading a whole bunch of code. Don't worry, all this good stuff will be cached up for next time!</small>",
|
||||
"CORE-WELCOME_ERROR": "Kibana did not load properly. Check the server output for more information."
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ import langParser from 'accept-language-parser';
|
|||
let kibanaTranslations = [];
|
||||
let acceptLanguages = '';
|
||||
|
||||
const DEFAULT_LANGUAGE = 'en';
|
||||
const DEFAULT_LOCALE = 'en';
|
||||
|
||||
export default async (kbnServer, server, config) => {
|
||||
const uiExports = kbnServer.uiExports = new UiExports({
|
||||
|
@ -142,41 +142,41 @@ function translate(key) {
|
|||
return kibanaTranslations[key];
|
||||
}
|
||||
|
||||
async function getTranslationLanguage(acceptLanguages) {
|
||||
let langStr = '';
|
||||
async function getTranslationLocale(acceptLanguages) {
|
||||
let localeStr = '';
|
||||
let foundLang = false;
|
||||
|
||||
if (acceptLanguages === null || acceptLanguages.length <= 0) {
|
||||
return DEFAULT_LANGUAGE;
|
||||
return DEFAULT_LOCALE;
|
||||
}
|
||||
|
||||
const acceptLangsLen = acceptLanguages.length;
|
||||
const registeredLanguages = await i18nPlugin.getRegisteredTranslationLanguages();
|
||||
const registeredLocales = await i18nPlugin.getRegisteredTranslationLocales();
|
||||
|
||||
for (let indx = 0; indx < acceptLangsLen; indx++) {
|
||||
const language = acceptLanguages[indx];
|
||||
if (language.region) {
|
||||
langStr = language.code + '-' + language.region;
|
||||
localeStr = language.code + '-' + language.region;
|
||||
} else {
|
||||
langStr = language.code;
|
||||
localeStr = language.code;
|
||||
}
|
||||
if (registeredLanguages.indexOf(langStr) > -1) {
|
||||
if (registeredLocales.indexOf(localeStr) > -1) {
|
||||
foundLang = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (foundLang) {
|
||||
return langStr;
|
||||
return localeStr;
|
||||
}
|
||||
|
||||
const regLangsLen = registeredLanguages.length;
|
||||
const regLangsLen = registeredLocales.length;
|
||||
for (let indx = 0; indx < acceptLangsLen; indx++) {
|
||||
const language = acceptLanguages[indx];
|
||||
langStr = language.code;
|
||||
localeStr = language.code;
|
||||
for (let regIndx = 0; regIndx < regLangsLen; regIndx++) {
|
||||
const lang = registeredLanguages[regIndx];
|
||||
if (lang.match('^' + langStr)) {
|
||||
langStr = lang;
|
||||
const locale = registeredLocales[regIndx];
|
||||
if (locale.match('^' + locale)) {
|
||||
localeStr = locale;
|
||||
foundLang = true;
|
||||
break;
|
||||
}
|
||||
|
@ -186,8 +186,8 @@ async function getTranslationLanguage(acceptLanguages) {
|
|||
}
|
||||
}
|
||||
if (foundLang) {
|
||||
return langStr;
|
||||
return localeStr;
|
||||
}
|
||||
|
||||
return DEFAULT_LANGUAGE;
|
||||
return DEFAULT_LOCALE;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue