Fix i18n default formats injection into en.json (#20929) (#21634)

* Fix i18n default formats injection into en.json

* Use snapshots for tests
This commit is contained in:
Leanid Shutau 2018-08-03 13:51:30 +03:00 committed by GitHub
parent edc48a636d
commit ff39ac1692
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 114 additions and 2 deletions

View file

@ -0,0 +1 @@
<p>{{ 'test-plugin.message-id' | i18n: { defaultMessage: 'Message text' } }}</p>

View file

@ -0,0 +1,64 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`dev/i18n/extract_default_translations injects default formats into en.json 1`] = `
"{
formats: {
number: {
currency: {
style: 'currency',
},
percent: {
style: 'percent',
},
},
date: {
short: {
month: 'numeric',
day: 'numeric',
year: '2-digit',
},
medium: {
month: 'short',
day: 'numeric',
year: 'numeric',
},
long: {
month: 'long',
day: 'numeric',
year: 'numeric',
},
full: {
weekday: 'long',
month: 'long',
day: 'numeric',
year: 'numeric',
},
},
time: {
short: {
hour: 'numeric',
minute: 'numeric',
},
medium: {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
},
long: {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
timeZoneName: 'short',
},
full: {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
timeZoneName: 'short',
},
},
},
'test-plugin.message-id': 'Message text',
}
"
`;

View file

@ -18,7 +18,7 @@
*/
import { resolve } from 'path';
import { formats } from '@kbn/i18n';
import { i18n } from '@kbn/i18n';
import JSON5 from 'json5';
import { extractHtmlMessages } from './extract_html_messages';
@ -93,7 +93,9 @@ export async function extractDefaultTranslations(inputPath) {
);
// .slice(0, -1): remove closing curly brace from json to append messages
let jsonBuffer = Buffer.from(JSON5.stringify({ formats }, { quote: `'`, space: 2 }).slice(0, -1));
let jsonBuffer = Buffer.from(
JSON5.stringify({ formats: i18n.formats }, { quote: `'`, space: 2 }).slice(0, -1)
);
const defaultMessages = [...defaultMessagesMap].sort(([key1], [key2]) => {
return key1 < key2 ? -1 : 1;

View file

@ -0,0 +1,45 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { resolve } from 'path';
import fs from 'fs';
import { promisify } from 'util';
import { extractDefaultTranslations } from './extract_default_translations';
const readFileAsync = promisify(fs.readFile);
const removeDirAsync = promisify(fs.rmdir);
const unlinkAsync = promisify(fs.unlink);
const PLUGIN_PATH = resolve(__dirname, '__fixtures__', 'test_plugin');
describe('dev/i18n/extract_default_translations', () => {
it('injects default formats into en.json', async () => {
await extractDefaultTranslations(PLUGIN_PATH);
const extractedJSONBuffer = await readFileAsync(
resolve(PLUGIN_PATH, 'translations', 'en.json')
);
await unlinkAsync(resolve(PLUGIN_PATH, 'translations', 'en.json'));
await removeDirAsync(resolve(PLUGIN_PATH, 'translations'));
expect(extractedJSONBuffer.toString()).toMatchSnapshot();
});
});