Skip to content

Commit

Permalink
[I18n] Register translations before plugins init (elastic#26078)
Browse files Browse the repository at this point in the history
* Register translations before plugins init

* Fix i18n engine initialization

* Fix translationPath$ RxJS pipeline

* Move translations registration to mixin

* Fix arrays concatenation

* Use prettier

* Fix translations relative paths

* Use globby instead of glob

* Update docs

* Move globby to dependencies

* Get rid of translation directories config

* Update globby patterns

* Search only for current locale translation files
  • Loading branch information
LeanidShutau committed Dec 12, 2018
1 parent a87f486 commit d52b79d
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 49 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
"getos": "^3.1.0",
"glob": "^7.1.2",
"glob-all": "^3.1.0",
"globby": "^8.0.1",
"good-squeeze": "2.1.0",
"h2o2": "^8.1.2",
"handlebars": "4.0.5",
Expand Down Expand Up @@ -354,7 +355,6 @@
"fetch-mock": "^5.13.1",
"geckodriver": "1.12.2",
"getopts": "2.0.0",
"globby": "^8.0.1",
"grunt": "1.0.1",
"grunt-cli": "^1.2.0",
"grunt-contrib-watch": "^1.1.0",
Expand Down
16 changes: 1 addition & 15 deletions packages/kbn-i18n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,7 @@ For example:
src/legacy/core_plugins/kibana/translations/fr.json
```

When a new translation file is added, you have to register this file into
`uiExports.translations` array of plugin constructor parameters. For example:
```js
export default function (kibana) {
return new kibana.Plugin({
uiExports: {
translations: [
resolve(__dirname, './translations/fr.json'),
],
...
},
...
});
}
```
The engine scans `x-pack/plugins/*/translations`, `src/core_plugins/*/translations`, `plugins/*/translations` and `src/ui/translations` folders on initialization, so there is no need to register translation files.

The engine uses a `config/kibana.yml` file for locale resolution process. If locale is
defined via `i18n.locale` option in `config/kibana.yml` then it will be used as a base
Expand Down
2 changes: 0 additions & 2 deletions src/legacy/core_plugins/kibana/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ export default function (kibana) {
};
},

translations: [],

mappings,
uiSettingDefaults: getUiSettingDefaults(),
},
Expand Down
1 change: 0 additions & 1 deletion src/legacy/core_plugins/timelion/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export default function (kibana) {
category: ['timelion'],
}
},
translations: [],
},
init: require('./init.js'),
});
Expand Down
35 changes: 34 additions & 1 deletion src/server/i18n/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,45 @@
* under the License.
*/

import { resolve } from 'path';
import globby from 'globby';
import { i18n, i18nLoader } from '@kbn/i18n';

import { fromRoot } from '../../utils';

export async function i18nMixin(kbnServer, server, config) {
const { translationPaths = [] } = kbnServer.uiExports;
const locale = config.get('i18n.locale');

const translationsDirs = [fromRoot('src/ui/translations')];

const groupedEntries = await Promise.all([
...config.get('plugins.scanDirs').map(async path => {
const entries = await globby(`*/translations/${locale}.json`, {
cwd: path,
});
return entries.map(entry => resolve(path, entry));
}),

...config.get('plugins.paths').map(async path => {
const entries = await globby(
[`translations/${locale}.json`, `plugins/*/translations/${locale}.json`],
{
cwd: path,
}
);
return entries.map(entry => resolve(path, entry));
}),

...translationsDirs.map(async path => {
const entries = await globby(`${locale}.json`, {
cwd: path,
});
return entries.map(entry => resolve(path, entry));
}),
]);

const translationPaths = [].concat(...groupedEntries);

i18nLoader.registerTranslationFiles(translationPaths);

const pureTranslations = await i18nLoader.getTranslationsByLocale(locale);
Expand Down
4 changes: 3 additions & 1 deletion src/server/kbn_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ export default class KbnServer {
// writes pid file
pidMixin,

// scan translations dirs, register locale files, initialize i18n engine and define `server.getUiTranslations`
i18nMixin,

// find plugins and set this.plugins and this.pluginSpecs
Plugins.scanMixin,

Expand All @@ -83,7 +86,6 @@ export default class KbnServer {

// setup this.uiExports and this.uiBundles
uiMixin,
i18nMixin,
indexPatternsMixin,

// setup saved object routes
Expand Down
4 changes: 0 additions & 4 deletions src/ui/ui_exports/ui_export_types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ export {
shareContextMenuExtensions,
} from './ui_app_extensions';

export {
translations,
} from './ui_i18n';

export {
link,
links,
Expand Down
24 changes: 0 additions & 24 deletions src/ui/ui_exports/ui_export_types/ui_i18n.js

This file was deleted.

0 comments on commit d52b79d

Please sign in to comment.