Skip to content

Commit

Permalink
Add Vanilla JS example to kbn-i18n README (#23556)
Browse files Browse the repository at this point in the history
Rename Node.js to Vanilla JS and give example of internationalizing a string constant.
  • Loading branch information
cjcenizal authored Sep 27, 2018
1 parent 73f955d commit ecaf26e
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions packages/kbn-i18n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,37 @@ import memoizeIntlConstructor from 'intl-format-cache';
const getMessageFormat = memoizeIntlConstructor(IntlMessageFormat);
```

## Vanilla JS

`Intl-messageformat` package assumes that the
[Intl](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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](https://github.com/yahoo/intl-messageformat/tree/master/dist/locale-data)
for all 200+ languages is loaded along with the library) and pass the translation
messages into `init` method:

```js
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:

```js
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](#i18n-engine) section above for more details).

## React

[React-intl](https://github.com/yahoo/react-intl) library is used for internalization
Expand Down Expand Up @@ -374,26 +405,6 @@ In order to translate attributes in Angular we should use `i18nFilter`:
>
```

## Node.JS

`Intl-messageformat` package assumes that the
[Intl](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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](https://github.com/yahoo/intl-messageformat/tree/master/dist/locale-data)
for all 200+ languages is loaded along with the library) and pass the translation
messages into `init` method:

```js
import { i18n } from '@kbn/i18n';

i18n.init(messages);
```

After that we are able to use all methods exposed by the i18n engine
(see [I18n engine](#i18n-engine) section above for more details).

## Build tools

In order to simplify localization process, some build tools will be added:
Expand Down

0 comments on commit ecaf26e

Please sign in to comment.