This updated the `kbn:boostrap` scripts in the packages to use `--quiet` when bootstrapping. This prevents babel from logging every file it builds, but still logs errors if any occur. To make things a little simpler I also used the [`npm-run-all`](https://github.com/mysticatea/npm-run-all) package in `@kbn/i18n` to execute all the builds there in parallel and pass `--quiet`/`--watch` to all builds without duplicating the tasks or making them incompatible with windows. Before: ``` running `kbn:bootstrap` scripts @kbn/config-schema: $ yarn build @kbn/datemath: $ yarn build @kbn/i18n: $ yarn build @kbn/config-schema: $ tsc @kbn/datemath: $ babel src --out-dir target --copy-files @kbn/i18n: $ yarn build:web && yarn build:node && yarn build:types @kbn/i18n: $ cross-env BABEL_ENV=web babel src --config-file ./babel.config.js --out-dir target/web --extensions ".ts,.js,.tsx" @kbn/datemath: src/index.js -> target/index.js @kbn/i18n: Successfully compiled 18 files with Babel. @kbn/i18n: $ cross-env BABEL_ENV=node babel src --config-file ./babel.config.js --out-dir target/node --extensions ".ts,.js,.tsx" @kbn/i18n: Successfully compiled 18 files with Babel. @kbn/i18n: $ tsc --emitDeclarationOnly @kbn/dev-utils: $ yarn build @kbn/es-query: $ yarn build @kbn/dev-utils: $ babel src --out-dir target @kbn/es-query: $ babel src --out-dir target @kbn/dev-utils: src/index.js -> target/index.js @kbn/dev-utils: src/proc_runner/errors.js -> target/proc_runner/errors.js @kbn/dev-utils: src/proc_runner/index.js -> target/proc_runner/index.js @kbn/dev-utils: src/proc_runner/observe_lines.js -> target/proc_runner/observe_lines.js @kbn/dev-utils: src/proc_runner/observe_readable.js -> target/proc_runner/observe_readable.js @kbn/es-query: src/es_query/__tests__/_migrate_filter.js -> target/es_query/__tests__/_migrate_filter.js @kbn/dev-utils: src/proc_runner/observe_signals.js -> target/proc_runner/observe_signals.js @kbn/es-query: src/es_query/__tests__/build_es_query.js -> target/es_query/__tests__/build_es_query.js @kbn/es-query: src/es_query/__tests__/decorate_query.js -> target/es_query/__tests__/decorate_query.js @kbn/dev-utils: src/proc_runner/proc.js -> target/proc_runner/proc.js @kbn/es-query: src/es_query/__tests__/from_filters.js -> target/es_query/__tests__/from_filters.js @kbn/es-query: src/es_query/__tests__/from_kuery.js -> target/es_query/__tests__/from_kuery.js @kbn/es-query: src/es_query/__tests__/from_lucene.js -> target/es_query/__tests__/from_lucene.js @kbn/es-query: src/es_query/__tests__/lucene_string_to_dsl.js -> target/es_query/__tests__/lucene_string_to_dsl.js @kbn/dev-utils: src/proc_runner/proc_runner.js -> target/proc_runner/proc_runner.js @kbn/dev-utils: src/proc_runner/with_proc_runner.js -> target/proc_runner/with_proc_runner.js @kbn/dev-utils: src/proc_runner/with_proc_runner.test.js -> target/proc_runner/with_proc_runner.test.js @kbn/es-query: src/es_query/build_es_query.js -> target/es_query/build_es_query.js @kbn/dev-utils: src/serializers/absolute_path_serializer.js -> target/serializers/absolute_path_serializer.js @kbn/es-query: src/es_query/decorate_query.js -> target/es_query/decorate_query.js @kbn/dev-utils: src/serializers/index.js -> target/serializers/index.js @kbn/es-query: src/es_query/from_filters.js -> target/es_query/from_filters.js @kbn/dev-utils: src/streams/concat_stream.js -> target/streams/concat_stream.js @kbn/dev-utils: src/streams/index.js -> target/streams/index.js @kbn/dev-utils: src/streams/promise_from_streams.js -> target/streams/promise_from_streams.js @kbn/es-query: src/es_query/from_kuery.js -> target/es_query/from_kuery.js @kbn/dev-utils: src/streams/reduce_stream.js -> target/streams/reduce_stream.js @kbn/es-query: src/es_query/from_lucene.js -> target/es_query/from_lucene.js @kbn/dev-utils: src/tooling_log/index.js -> target/tooling_log/index.js @kbn/es-query: src/es_query/index.js -> target/es_query/index.js @kbn/dev-utils: src/tooling_log/log_levels.js -> target/tooling_log/log_levels.js @kbn/es-query: src/es_query/lucene_string_to_dsl.js -> target/es_query/lucene_string_to_dsl.js @kbn/dev-utils: src/tooling_log/log_levels.test.js -> target/tooling_log/log_levels.test.js @kbn/es-query: src/es_query/migrate_filter.js -> target/es_query/migrate_filter.js @kbn/dev-utils: src/tooling_log/tooling_log.js -> target/tooling_log/tooling_log.js @kbn/es-query: src/filters/__tests__/phrase.js -> target/filters/__tests__/phrase.js @kbn/es-query: src/filters/__tests__/query.js -> target/filters/__tests__/query.js @kbn/dev-utils: src/tooling_log/tooling_log.test.js -> target/tooling_log/tooling_log.test.js @kbn/dev-utils: src/tooling_log/tooling_log_text_writer.js -> target/tooling_log/tooling_log_text_writer.js @kbn/dev-utils: src/tooling_log/tooling_log_text_writer.test.js -> target/tooling_log/tooling_log_text_writer.test.js @kbn/es-query: src/filters/__tests__/range.js -> target/filters/__tests__/range.js @kbn/es-query: src/filters/exists.js -> target/filters/exists.js @kbn/es-query: src/filters/index.js -> target/filters/index.js @kbn/es-query: src/filters/phrase.js -> target/filters/phrase.js @kbn/es-query: src/filters/phrases.js -> target/filters/phrases.js @kbn/es-query: src/filters/query.js -> target/filters/query.js @kbn/es-query: src/filters/range.js -> target/filters/range.js @kbn/es-query: src/index.js -> target/index.js @kbn/es-query: src/kuery/ast/__tests__/ast.js -> target/kuery/ast/__tests__/ast.js @kbn/es-query: src/kuery/ast/ast.js -> target/kuery/ast/ast.js @kbn/es-query: src/kuery/ast/index.js -> target/kuery/ast/index.js @kbn/es-query: src/kuery/ast/kuery.js -> target/kuery/ast/kuery.js @kbn/es-query: src/kuery/ast/legacy_kuery.js -> target/kuery/ast/legacy_kuery.js @kbn/es-query: src/kuery/filter_migration/__tests__/exists.js -> target/kuery/filter_migration/__tests__/exists.js @kbn/es-query: src/kuery/filter_migration/__tests__/filter_to_kuery.js -> target/kuery/filter_migration/__tests__/filter_to_kuery.js @kbn/es-query: src/kuery/filter_migration/__tests__/geo_bounding_box.js -> target/kuery/filter_migration/__tests__/geo_bounding_box.js @kbn/es-query: src/kuery/filter_migration/__tests__/geo_polygon.js -> target/kuery/filter_migration/__tests__/geo_polygon.js @kbn/es-query: src/kuery/filter_migration/__tests__/phrase.js -> target/kuery/filter_migration/__tests__/phrase.js @kbn/es-query: src/kuery/filter_migration/__tests__/range.js -> target/kuery/filter_migration/__tests__/range.js @kbn/es-query: src/kuery/filter_migration/exists.js -> target/kuery/filter_migration/exists.js @kbn/es-query: src/kuery/filter_migration/filter_to_kuery.js -> target/kuery/filter_migration/filter_to_kuery.js @kbn/es-query: src/kuery/filter_migration/geo_bounding_box.js -> target/kuery/filter_migration/geo_bounding_box.js @kbn/es-query: src/kuery/filter_migration/geo_polygon.js -> target/kuery/filter_migration/geo_polygon.js @kbn/es-query: src/kuery/filter_migration/index.js -> target/kuery/filter_migration/index.js @kbn/es-query: src/kuery/filter_migration/phrase.js -> target/kuery/filter_migration/phrase.js @kbn/es-query: src/kuery/filter_migration/range.js -> target/kuery/filter_migration/range.js @kbn/es-query: src/kuery/functions/__tests__/and.js -> target/kuery/functions/__tests__/and.js @kbn/es-query: src/kuery/functions/__tests__/exists.js -> target/kuery/functions/__tests__/exists.js @kbn/es-query: src/kuery/functions/__tests__/geo_bounding_box.js -> target/kuery/functions/__tests__/geo_bounding_box.js @kbn/es-query: src/kuery/functions/__tests__/geo_polygon.js -> target/kuery/functions/__tests__/geo_polygon.js @kbn/es-query: src/kuery/functions/__tests__/is.js -> target/kuery/functions/__tests__/is.js @kbn/es-query: src/kuery/functions/__tests__/not.js -> target/kuery/functions/__tests__/not.js @kbn/es-query: src/kuery/functions/__tests__/or.js -> target/kuery/functions/__tests__/or.js @kbn/es-query: src/kuery/functions/__tests__/range.js -> target/kuery/functions/__tests__/range.js @kbn/es-query: src/kuery/functions/__tests__/utils/get_fields.js -> target/kuery/functions/__tests__/utils/get_fields.js @kbn/es-query: src/kuery/functions/and.js -> target/kuery/functions/and.js @kbn/es-query: src/kuery/functions/exists.js -> target/kuery/functions/exists.js @kbn/es-query: src/kuery/functions/geo_bounding_box.js -> target/kuery/functions/geo_bounding_box.js @kbn/es-query: src/kuery/functions/geo_polygon.js -> target/kuery/functions/geo_polygon.js @kbn/es-query: src/kuery/functions/index.js -> target/kuery/functions/index.js @kbn/es-query: src/kuery/functions/is.js -> target/kuery/functions/is.js @kbn/es-query: src/kuery/functions/not.js -> target/kuery/functions/not.js @kbn/es-query: src/kuery/functions/or.js -> target/kuery/functions/or.js @kbn/es-query: src/kuery/functions/range.js -> target/kuery/functions/range.js @kbn/es-query: src/kuery/functions/utils/get_fields.js -> target/kuery/functions/utils/get_fields.js @kbn/es-query: src/kuery/index.js -> target/kuery/index.js @kbn/es-query: src/kuery/node_types/__tests__/function.js -> target/kuery/node_types/__tests__/function.js @kbn/es-query: src/kuery/node_types/__tests__/literal.js -> target/kuery/node_types/__tests__/literal.js @kbn/es-query: src/kuery/node_types/__tests__/named_arg.js -> target/kuery/node_types/__tests__/named_arg.js @kbn/es-query: src/kuery/node_types/__tests__/wildcard.js -> target/kuery/node_types/__tests__/wildcard.js @kbn/es-query: src/kuery/node_types/function.js -> target/kuery/node_types/function.js @kbn/es-query: src/kuery/node_types/index.js -> target/kuery/node_types/index.js @kbn/es-query: src/kuery/node_types/literal.js -> target/kuery/node_types/literal.js @kbn/es-query: src/kuery/node_types/named_arg.js -> target/kuery/node_types/named_arg.js @kbn/es-query: src/kuery/node_types/wildcard.js -> target/kuery/node_types/wildcard.js @kbn/test: $ yarn build @kbn/test: $ babel src --out-dir target @kbn/test: src/es/es_test_cluster.js -> target/es/es_test_cluster.js @kbn/test: src/es/es_test_config.js -> target/es/es_test_config.js @kbn/test: src/es/index.js -> target/es/index.js @kbn/test: src/functional_tests/cli/index.js -> target/functional_tests/cli/index.js @kbn/test: src/functional_tests/cli/run_tests/args.js -> target/functional_tests/cli/run_tests/args.js @kbn/test: src/functional_tests/cli/run_tests/args.test.js -> target/functional_tests/cli/run_tests/args.test.js @kbn/test: src/functional_tests/cli/run_tests/cli.js -> target/functional_tests/cli/run_tests/cli.js @kbn/test: src/functional_tests/cli/run_tests/cli.test.js -> target/functional_tests/cli/run_tests/cli.test.js @kbn/test: src/functional_tests/cli/start_servers/args.js -> target/functional_tests/cli/start_servers/args.js @kbn/test: src/functional_tests/cli/start_servers/args.test.js -> target/functional_tests/cli/start_servers/args.test.js @kbn/test: src/functional_tests/cli/start_servers/cli.js -> target/functional_tests/cli/start_servers/cli.js @kbn/test: src/functional_tests/cli/start_servers/cli.test.js -> target/functional_tests/cli/start_servers/cli.test.js @kbn/test: src/functional_tests/lib/auth.js -> target/functional_tests/lib/auth.js @kbn/test: src/functional_tests/lib/index.js -> target/functional_tests/lib/index.js @kbn/test: src/functional_tests/lib/paths.js -> target/functional_tests/lib/paths.js @kbn/test: src/functional_tests/lib/run_cli.js -> target/functional_tests/lib/run_cli.js @kbn/test: src/functional_tests/lib/run_cli.test.js -> target/functional_tests/lib/run_cli.test.js @kbn/test: src/functional_tests/lib/run_elasticsearch.js -> target/functional_tests/lib/run_elasticsearch.js @kbn/test: src/functional_tests/lib/run_ftr.js -> target/functional_tests/lib/run_ftr.js @kbn/test: src/functional_tests/lib/run_kibana_server.js -> target/functional_tests/lib/run_kibana_server.js @kbn/test: src/functional_tests/tasks.js -> target/functional_tests/tasks.js @kbn/test: src/functional_tests/test_helpers.js -> target/functional_tests/test_helpers.js @kbn/test: src/index.js -> target/index.js @kbn/test: src/kbn/index.js -> target/kbn/index.js @kbn/test: src/kbn/kbn_test_config.js -> target/kbn/kbn_test_config.js @kbn/test: src/kbn/users.js -> target/kbn/users.js x-pack: $ gulp canvas:plugins:build x-pack: [13:22:34] /Users/spalger/kbn-dev/master/kibana/x-pack/plugins/canvas/canvas_plugin x-pack: [13:22:34] Using gulpfile ~/kbn-dev/master/kibana/x-pack/gulpfile.js x-pack: [13:22:34] Starting 'canvas:plugins:build'... x-pack: [13:22:50] canvas:plugins Plugins built in 15.75 seconds x-pack: [13:22:50] Finished 'canvas:plugins:build' after 16 s kibana: $ yarn build:types && node scripts/register_git_hook kibana: $ tsc --p tsconfig.types.json kibana: Registering Kibana pre-commit git hook... kibana: success Kibana pre-commit git hook was installed successfully. ``` (this will be even worse when we merge #26095) After: ``` running `kbn:bootstrap` scripts @kbn/config-schema: $ yarn build @kbn/datemath: $ yarn build --quiet @kbn/i18n: $ run-p 'build:babel:** --quiet' build:tsc @kbn/datemath: $ babel src --out-dir target --copy-files --quiet @kbn/config-schema: $ tsc @kbn/i18n: $ cross-env BABEL_ENV=node babel src --config-file ./babel.config.js --out-dir target/node --extensions ".ts,.js,.tsx" --quiet @kbn/i18n: $ cross-env BABEL_ENV=web babel src --config-file ./babel.config.js --out-dir target/web --extensions ".ts,.js,.tsx" --quiet @kbn/i18n: $ tsc --emitDeclarationOnly @kbn/i18n: Successfully compiled 18 files with Babel. @kbn/i18n: Successfully compiled 18 files with Babel. @kbn/dev-utils: $ yarn build --quiet @kbn/es-query: $ yarn build --quiet @kbn/dev-utils: $ babel src --out-dir target --quiet @kbn/es-query: $ babel src --out-dir target --quiet @kbn/test: $ yarn build --quiet @kbn/test: $ babel src --out-dir target --quiet x-pack: $ gulp canvas:plugins:build x-pack: [13:16:56] /Users/spalger/kbn-dev/master/kibana/x-pack/plugins/canvas/canvas_plugin x-pack: [13:16:56] Using gulpfile ~/kbn-dev/master/kibana/x-pack/gulpfile.js x-pack: [13:16:56] Starting 'canvas:plugins:build'... x-pack: [13:17:11] canvas:plugins Plugins built in 13.98 seconds x-pack: [13:17:11] Finished 'canvas:plugins:build' after 14 s kibana: $ yarn build:types && node scripts/register_git_hook kibana: $ tsc --p tsconfig.types.json kibana: Registering Kibana pre-commit git hook... kibana: success Kibana pre-commit git hook was installed successfully. ``` |
||
---|---|---|
.. | ||
angular | ||
react | ||
src | ||
types | ||
babel.config.js | ||
GUIDELINE.md | ||
package.json | ||
README.md | ||
tsconfig.json | ||
tslint.yml |
I18n
Kibana relies on several UI frameworks (React and Angular) and
requires localization in different environments (browser and NodeJS).
Internationalization engine is framework agnostic and consumable in
all parts of Kibana (React, Angular and NodeJS). In order to simplify
internationalization in UI frameworks, the additional abstractions are
built around the I18n engine: react-intl
for React and custom
components for Angular. React-intl
is built around intl-messageformat,
so both React and Angular frameworks use the same engine and the same
message syntax.
Localization files
Localization files have JSON5 format.
The main benefits of using JSON5
:
- Objects may have a single trailing comma.
- Single and multi-line comments are allowed.
- Strings may span multiple lines by escaping new line characters.
Short example:
{
// comments
unquoted: 'and you can quote me on that',
singleQuotes: 'I can use "double quotes" here',
lineBreaks: "Wow! \
No \\n's!",
hexadecimal: 0xdecaf,
leadingDecimalPoint: .8675309, andTrailing: 8675309.,
positiveSign: +1,
trailingComma: 'in objects', andIn: ['arrays',],
"backwardsCompatible": "with JSON",
}
Using comments can help to understand which section of the application
the localization key is used for. Also namespaces
are used in order to simplify message location search. For example, if
we are going to translate the title of /management/sections/objects/_objects.html
file, we should use message path like this: 'MANAGEMENT.OBJECTS.TITLE'
.
Each Kibana plugin has a separate folder with translation files located at
{path/to/plugin}/translations/{locale}.json
where locale
is ISO 639 language code.
For example:
src/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:
export default function (kibana) {
return new kibana.Plugin({
uiExports: {
translations: [
resolve(__dirname, './translations/fr.json'),
],
...
},
...
});
}
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
locale, otherwise i18n engine will fall back to en
. The en
locale will also be used
if translation can't be found for the base non-English locale.
One of our technical requirements is to have default messages in the templates
themselves, and those messages will always be in English, so we don't have to keep
en.json
file in repository. We can generate that file from defaultMessage
s
defined inline.
Note: locale defined in i18n.locale
and the one used for translation files should
match exactly, e.g. i18n.locale: zn
and .../translations/zh_CN.json
won't match and
default English translations will be used, but i18n.locale: zh_CN
and.../translations/zh_CN.json
or i18n.locale: zn
and .../translations/zn.json
will work as expected.
I18n engine
I18n engine is the platform agnostic abstraction that helps to supply locale data to UI frameworks and provides methods for the direct translation.
Here is the public API exposed by this engine:
addMessages(messages: Map<string, string>, [locale: string])
- provides a way to register translations with the enginegetMessages()
- returns messages for the current languagesetLocale(locale: string)
- tells the engine which language to use by given language keygetLocale()
- returns the current localesetDefaultLocale(locale: string)
- tells the library which language to fallback when missing translationsgetDefaultLocale()
- returns the default localesetFormats(formats: object)
- supplies a set of options to the underlying formatter. For the detailed explanation, see the section belowgetFormats()
- returns current formatsgetRegisteredLocales()
- returns array of locales having translationstranslate(id: string, [{values: object, defaultMessage: string, description: string}])
– translate message by id.description
is optional context comment that will be extracted by i18n tools and added as a comment next to translation message atdefaultMessages.json
.init(messages: Map<string, string>)
- initializes the engine
I18n engine internals
The engine uses the ICU Message syntax and works for all CLDR languages which
have pluralization rules defined. It's built around intl-messageformat
package
which exposes IntlMessageFormat
class. Messages are provided into the constructor
as a string message, or a pre-parsed AST object.
import IntlMessageFormat from 'intl-messageformat';
const msg = new IntlMessageFormat(message, locales, [formats]);
The string message
is parsed, then stored internally in a
compiled form that is optimized for the format()
method to
produce the formatted string for displaying to the user.
const output = msg.format(values);
formats
parameter in IntlMessageFormat
constructor allows formatting numbers
and dates/times in messages using Intl.NumberFormat
and Intl.DateTimeFormat
,
respectively.
const msg = new IntlMessageFormat('The price is: {price, number, USD}', 'en-US', {
number: {
USD: {
style : 'currency',
currency: 'USD',
},
},
});
const output = msg.format({ price: 100 });
console.log(output); // => "The price is: $100.00"
In this example, we're defining a USD number format style which is passed to
the underlying Intl.NumberFormat
instance as its options.
Here
you can find default format options used as the prototype of the formats
provided to the constructor.
Creating instances of IntlMessageFormat
is expensive.
Intl-format-cache
library is simply to make it easier to create a cache of format
instances of a particular type to aid in their reuse. Under the
hood, this package creates a cache key based on the arguments passed
to the memoized constructor.
import memoizeIntlConstructor from 'intl-format-cache';
const getMessageFormat = memoizeIntlConstructor(IntlMessageFormat);
Vanilla JS
Intl-messageformat
package assumes that the
Intl
global object exists in the runtime. Intl
is present in all modern
browsers and Node.js 0.10+. In order to load i18n engine
in Node.js we should simply import
this module (in Node.js, the
data
for all 200+ languages is loaded along with the library) and pass the translation
messages into init
method:
import { i18n } from '@kbn/i18n';
i18n.init(messages);
One common use-case is that of internationalizing a string constant. Here's an example of how we'd do that:
import { i18n } from '@kbn/i18n';
export const HELLO_WORLD = i18n.translate('hello.wonderful.world', {
defaultMessage: 'Greetings, planet Earth!',
}),
We're also able to use all methods exposed by the i18n engine (see I18n engine section above for more details).
React
React-intl library is used for internalization React part of the application. It provides React components and an API to format dates, numbers, and strings, including pluralization and handling translations.
React Intl uses the provider pattern to scope an i18n context to a tree of components.
IntlProvider
component is used to setup the i18n context for a tree. After that we
are able to use FormattedMessage
component in order to translate messages.
IntlProvider
should wrap react app's root component (inside each react render method).
In order to translate messages we need to use I18nProvider
component that
uses I18n engine under the hood:
import React from 'react';
import ReactDOM from 'react-dom';
import { I18nProvider } from '@kbn/i18n/react';
ReactDOM.render(
<I18nProvider>
<RootComponent>
...
</RootComponent>
</I18nProvider>,
document.getElementById('container')
);
After that we can use FormattedMessage
components inside RootComponent
:
import React, { Component } from 'react';
import { FormattedMessage } from '@kbn/i18n/react';
class RootComponent extends Component {
constructor(props) {
super(props);
this.state = {
name: 'Eric',
unreadCount: 1000,
};
}
render() {
const {
name,
unreadCount,
} = this.state;
return (
<p>
<FormattedMessage
id="welcome"
defaultMessage={`Hello {name}, you have {unreadCount, number} {unreadCount, plural,
one {message}
other {messages}
}`}
values={{name: <b>{name}</b>, unreadCount}}
/>
...
</p>
);
}
}
Optionally we can pass description
prop into FormattedMessage
component.
This prop is optional context comment that will be extracted by i18n tools
and added as a comment next to translation message at defaultMessages.json
Attributes translation in React
React wrapper provides an ability to inject the imperative formatting API into a React component via its props using injectI18n
Higher-Order Component. This should be used when your React component needs to format data to a string value where a React element is not suitable; e.g., a title
or aria
attribute. In order to use it you should wrap your component with injectI18n
Higher-Order Component. The formatting API will be provided to the wrapped component via props.intl
.
React component as a pure function:
import React from 'react';
import { injectI18n, intlShape } from '@kbn/i18n/react';
const MyComponentContent = ({ intl }) => (
<input
type="text"
placeholder={intl.formatMessage(
{
id: 'welcome',
defaultMessage: 'Hello {name}, you have {unreadCount, number}\
{unreadCount, plural, one {message} other {messages}}',
description: 'Message description',
},
{ name, unreadCount }
)}
/>
);
MyComponentContent.propTypes = {
intl: intlShape.isRequired,
};
export const MyComponent = injectI18n(MyComponentContent);
React component as a class:
import React from 'react';
import { injectI18n, intlShape } from '@kbn/i18n/react';
class MyComponentContent extends React.Component {
static propTypes = {
intl: intlShape.isRequired,
};
render() {
const { intl } = this.props;
return (
<input
type="text"
placeholder={intl.formatMessage({
id: 'KIBANA-MANAGEMENT-OBJECTS-SEARCH_PLACEHOLDER',
defaultMessage: 'Search',
})}
/>
);
}
}
export const MyComponent = injectI18n(MyComponentContent);
Angular
Angular wrapper has 4 entities: translation provider
, service
, directive
and filter
. Both the directive and the filter use the translation service
with i18n engine under the hood.
The translation provider
is used for service
configuration and
has the following methods:
addMessages(messages: Map<string, string>, [locale: string])
- provides a way to register translations with the librarysetLocale(locale: string)
- tells the library which language to use by given language keygetLocale()
- returns the current localesetDefaultLocale(locale: string)
- tells the library which language to fallback when missing translationsgetDefaultLocale()
- returns the default localesetFormats(formats: object)
- supplies a set of options to the underlying formattergetFormats()
- returns current formatsgetRegisteredLocales()
- returns array of locales having translationsinit(messages: Map<string, string>)
- initializes the engine
The translation service
provides only one method:
i18n(id: string, [{values: object, defaultMessage: string, description: string }])
– translate message by id
The translation filter
is used for attributes translation and has
the following syntax:
{{'translationId' | i18n[:{ values: object, defaultMessage: string, description: string }]}}
Where:
translationId
- translation id to be translatedvalues
- values to pass into translationdefaultMessage
- will be used unless translation was successful (the final fallback in english, will be used for generatingen.json
)description
- optional context comment that will be extracted by i18n tools and added as a comment next to translation message atdefaultMessages.json
The translation directive
has the following syntax:
<ANY
i18n-id="{string}"
[i18n-values="{object}"]
[i18n-default-message="{string}"]
[i18n-description="{string}"]
></ANY>
Where:
i18n-id
- translation id to be translatedi18n-values
- values to pass into translationi18n-default-message
- will be used unless translation was successfuli18n-description
- optional context comment that will be extracted by i18n tools and added as a comment next to translation message atdefaultMessages.json
Angular I18n
module is placed into autoload
module, so it will be
loaded automatically. After that we can use i18n directive in Angular templates:
<span
i18n-id="welcome"
i18n-default-message="Hello!"
></span>
In order to translate attributes in Angular we should use i18nFilter
:
<input
type="text"
placeholder="{{ ::'KIBANA-MANAGEMENT-OBJECTS-SEARCH_PLACEHOLDER' | i18n: {
defaultMessage: 'Search'
} }}"
>
Build tools
In order to simplify localization process, some build tools will be added:
- tool for verifying all translations have translatable strings
- tool for checking unused translation strings
- tool for extracting default messages from templates
While react-intl
has
babel-plugin-react-intl
library which extracts string messages for translation, angular wrapper requires
own implementation of such tool. In order to extracrt translation keys from the
template, we have to parse it and create AST object. There is a
babel-plugin-syntax-jsx
plugin which helps to parse JSX syntax and then create AST object. Unfortunately,
there are no babel plugins to parse angular templates. One of the solution can be internal
$parse.$$getAst
angular method.