diff --git a/src/dev/i18n/__fixtures__/test_plugin/test_file.html b/src/dev/i18n/__fixtures__/test_plugin/test_file.html new file mode 100644 index 0000000000000..b102216c7d12e --- /dev/null +++ b/src/dev/i18n/__fixtures__/test_plugin/test_file.html @@ -0,0 +1 @@ +

{{ 'test-plugin.message-id' | i18n: { defaultMessage: 'Message text' } }}

diff --git a/src/dev/i18n/__snapshots__/extract_default_translations.test.js.snap b/src/dev/i18n/__snapshots__/extract_default_translations.test.js.snap new file mode 100644 index 0000000000000..c507ed25b37a4 --- /dev/null +++ b/src/dev/i18n/__snapshots__/extract_default_translations.test.js.snap @@ -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', +} +" +`; diff --git a/src/dev/i18n/extract_default_translations.js b/src/dev/i18n/extract_default_translations.js index d63c7b88092dd..57059ce2e400b 100644 --- a/src/dev/i18n/extract_default_translations.js +++ b/src/dev/i18n/extract_default_translations.js @@ -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; diff --git a/src/dev/i18n/extract_default_translations.test.js b/src/dev/i18n/extract_default_translations.test.js new file mode 100644 index 0000000000000..eb78987e5e75d --- /dev/null +++ b/src/dev/i18n/extract_default_translations.test.js @@ -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(); + }); +});