mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
* Fix i18n default formats injection into en.json * Use snapshots for tests
This commit is contained in:
parent
edc48a636d
commit
ff39ac1692
4 changed files with 114 additions and 2 deletions
1
src/dev/i18n/__fixtures__/test_plugin/test_file.html
Normal file
1
src/dev/i18n/__fixtures__/test_plugin/test_file.html
Normal file
|
@ -0,0 +1 @@
|
|||
<p>{{ 'test-plugin.message-id' | i18n: { defaultMessage: 'Message text' } }}</p>
|
|
@ -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',
|
||||
}
|
||||
"
|
||||
`;
|
|
@ -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;
|
||||
|
|
45
src/dev/i18n/extract_default_translations.test.js
Normal file
45
src/dev/i18n/extract_default_translations.test.js
Normal 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();
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue