[i18n] Add locale for lang html attribute (#28923) (#29049)

* add locale for lang html attribute

* remove unused variable

* update readme

* fix unit tests

* add links to standards

* fix unit test

* Fix typos
This commit is contained in:
Maryia Lapata 2019-01-22 16:17:26 +03:00 committed by GitHub
parent fbeeb244e0
commit 3225df795c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 12 deletions

View file

@ -46,9 +46,13 @@ themselves, and those messages will always be in English, so we don't have to ke
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.
match exactly, e.g. `i18n.locale: zh` 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: zh` and `.../translations/zh.json` will work as expected.
__Note:__ locale should look like `zh-CN` where `zh` - lowercase two-letter or three-letter ISO-639 code
and `CN` - uppercase two-letter ISO-3166 code (optional).
[ISO-639](https://www.iso.org/iso-639-language-codes.html) and [ISO-3166](https://www.iso.org/iso-3166-country-codes.html) codes should be separated with `-` character.
## I18n engine

View file

@ -153,8 +153,7 @@ describe('I18n engine', () => {
});
test('should add messages with normalized passed locale', () => {
const locale = 'en-us';
i18n.setLocale(locale);
i18n.setLocale('en-US');
i18n.addTranslation(
{
@ -162,10 +161,10 @@ describe('I18n engine', () => {
['a.b.c']: 'bar',
},
},
'en_US'
'en-us'
);
expect(i18n.getLocale()).toBe(locale);
expect(i18n.getLocale()).toBe('en-us');
expect(i18n.getTranslation()).toEqual({
messages: {
['a.b.c']: 'bar',
@ -234,7 +233,7 @@ describe('I18n engine', () => {
});
test('should normalize passed locale', () => {
i18n.setLocale('en_US');
i18n.setLocale('en-US');
expect(i18n.getLocale()).toBe('en-us');
});
});
@ -267,7 +266,7 @@ describe('I18n engine', () => {
});
test('should normalize passed locale', () => {
i18n.setDefaultLocale('en_US');
i18n.setDefaultLocale('en-US');
expect(i18n.getDefaultLocale()).toBe('en-us');
});

View file

@ -30,7 +30,6 @@ import { isPseudoLocale, translateUsingPseudoLocale } from './pseudo_locale';
import './locales.js';
const EN_LOCALE = 'en';
const LOCALE_DELIMITER = '-';
const translationsForLocale: Record<string, Translation> = {};
const getMessageFormat = memoizeIntlConstructor(IntlMessageFormat);
@ -55,7 +54,7 @@ function getMessageById(id: string): string | undefined {
* @param locale
*/
function normalizeLocale(locale: string) {
return locale.toLowerCase().replace('_', LOCALE_DELIMITER);
return locale.toLowerCase();
}
/**

View file

@ -151,6 +151,7 @@ export function uiRenderMixin(kbnServer, server, config) {
uiPublicUrl: `${basePath}/ui`,
bootstrapScriptUrl: `${basePath}/bundles/app/${app.getId()}/bootstrap.js`,
i18n: (id, options) => i18n.translate(id, options),
locale: i18n.getLocale(),
injectedMetadata: {
version: kbnServer.version,

View file

@ -1,7 +1,7 @@
block vars
doctype html
html(lang='en')
html(lang=locale)
head
meta(charset='utf-8')
meta(http-equiv='X-UA-Compatible', content='IE=edge,chrome=1')