Expose the i18n APIs in the server object for plugin access

Plugins should call the i18n plugin APIs through the server object
and not directly from the module.

This closes he following comments:
- https://github.com/elastic/kibana/pull/7545#discussion_r74662598
- https://github.com/elastic/kibana/pull/7545#discussion_r74669327
- https://github.com/elastic/kibana/pull/7545#discussion_r74669765
This commit is contained in:
Martin Hickey 2016-08-19 18:31:10 +01:00
parent 8c39a8d5e9
commit a9e30d3931
3 changed files with 15 additions and 12 deletions

View file

@ -1,7 +1,11 @@
import i18n from './server/i18n/i18n';
export default function (kibana) {
return new kibana.Plugin({
init(server, options) {
// Add server routes and initalize the plugin here
server.expose('getRegisteredLocaleTranslations', i18n.getRegisteredLocaleTranslations);
server.expose('getRegisteredTranslationLocales', i18n.getRegisteredTranslationLocales);
server.expose('registerTranslations', i18n.registerTranslations);
}
});
};

View file

@ -1,7 +1,6 @@
import Promise from 'bluebird';
import { mkdirp as mkdirpNode } from 'mkdirp';
import manageUuid from './server/lib/manage_uuid';
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';
@ -16,6 +15,8 @@ module.exports = function (kibana) {
const kbnBaseUrl = '/app/kibana';
return new kibana.Plugin({
id: 'kibana',
require: [ 'i18n' ],
config: function (Joi) {
return Joi.object({
enabled: Joi.boolean().default(true),
@ -117,14 +118,14 @@ module.exports = function (kibana) {
settings(server);
scripts(server);
server.expose('systemApi', systemApi);
registerCoreTranslations();
registerCoreTranslations(server);
}
});
};
function registerCoreTranslations()
function registerCoreTranslations(server)
{
const corePluginTranslationFile = fromRoot('/src/core_plugins/kibana/i18n/en.json');
i18nPlugin.registerTranslations(corePluginTranslationFile);
server.plugins.i18n.registerTranslations(corePluginTranslationFile);
}

View file

@ -10,7 +10,6 @@ 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/i18n';
import langParser from 'accept-language-parser';
let kibanaTranslations = [];
@ -142,25 +141,24 @@ function translate(key) {
return kibanaTranslations[key];
}
function getTranslationLocale(acceptLanguages) {
function getTranslationLocale(acceptLanguages, server) {
let localeStr = '';
if (acceptLanguages === null || acceptLanguages.length <= 0) {
return DEFAULT_LOCALE;
}
localeStr = getTranslationLocaleExactMatch(acceptLanguages);
const registeredLocales = server.plugins.i18n.getRegisteredTranslationLocales();
localeStr = getTranslationLocaleExactMatch(acceptLanguages, registeredLocales);
if (localeStr != null) {
return localeStr;
}
localeStr = getTranslationLocaleBestCaseMatch(acceptLanguages);
localeStr = getTranslationLocaleBestCaseMatch(acceptLanguages, registeredLocales);
}
function getTranslationLocaleExactMatch(acceptLanguages) {
function getTranslationLocaleExactMatch(acceptLanguages, registeredLocales) {
let localeStr = '';
const acceptLangsLen = acceptLanguages.length;
const registeredLocales = i18nPlugin.getRegisteredTranslationLocales();
for (let indx = 0; indx < acceptLangsLen; indx++) {
const language = acceptLanguages[indx];
if (language.region) {