Skip to content

Commit

Permalink
Fix i18n default formats injection into en.json (#20929) (#21634)
Browse files Browse the repository at this point in the history
* Fix i18n default formats injection into en.json

* Use snapshots for tests
  • Loading branch information
LeanidShutau authored Aug 3, 2018
1 parent edc48a6 commit ff39ac1
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/dev/i18n/__fixtures__/test_plugin/test_file.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>{{ 'test-plugin.message-id' | i18n: { defaultMessage: 'Message text' } }}</p>
Original file line number Diff line number Diff line change
@@ -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',
}
"
`;
6 changes: 4 additions & 2 deletions src/dev/i18n/extract_default_translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down
45 changes: 45 additions & 0 deletions src/dev/i18n/extract_default_translations.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit ff39ac1

Please sign in to comment.