Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
c1943c7
Implement a build tool for locale files verification
LeanidShutau Jun 1, 2018
e4ae11f
Refactor locale files verification tool
LeanidShutau Jun 14, 2018
7b0eafc
Fix default messages structure
LeanidShutau Jun 14, 2018
f89b20f
Merge branch 'master' into locale-files-checker
LeanidShutau Jun 26, 2018
d17eb99
Return to plain structured JSON files
LeanidShutau Jun 26, 2018
94402a3
Merge branch 'master' into locale-files-checker
LeanidShutau Jul 9, 2018
9a34c13
Update locale files checking tool
LeanidShutau Jul 9, 2018
85e5f14
Merge branch 'master' into locale-files-checker
LeanidShutau Jul 17, 2018
24314b1
Add unit tests
LeanidShutau Jul 20, 2018
2f7725f
Merge branch 'master' into locale-files-checker
LeanidShutau Jul 20, 2018
7ba5de5
Fix tests
LeanidShutau Jul 20, 2018
203b797
Merge branch 'master' into locale-files-checker
LeanidShutau Jul 23, 2018
4b72017
Resolve comments
LeanidShutau Jul 26, 2018
6da7797
Normalize paths for crossplatform snapshot testing
LeanidShutau Jul 26, 2018
d24c954
Merge branch 'master' into locale-files-checker
LeanidShutau Aug 13, 2018
06b3bad
Add locale files integration
LeanidShutau Aug 13, 2018
a005cff
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau Aug 22, 2018
9507818
Update integration tool
LeanidShutau Aug 22, 2018
38b8e27
Resolve comments
LeanidShutau Aug 27, 2018
970add6
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau Aug 27, 2018
9bcef28
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau Aug 29, 2018
10c9022
Add mkdir for translations folders
LeanidShutau Aug 29, 2018
a3250bf
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau Sep 6, 2018
83ef984
Fix wrong variable name
LeanidShutau Sep 7, 2018
4acebd6
Rename script
maryia-lapata Sep 12, 2018
8ffcf96
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau Sep 26, 2018
e1804c4
Resolve comments
LeanidShutau Sep 27, 2018
e16abcd
Fix tests
LeanidShutau Oct 19, 2018
4867577
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau Oct 19, 2018
0225c46
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau Oct 26, 2018
025602e
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau Oct 29, 2018
139b6ba
Fix locale file format bug
LeanidShutau Nov 2, 2018
7e69c9d
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau Nov 5, 2018
6952263
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau Nov 8, 2018
c7c3f6a
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau Nov 12, 2018
bd09898
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau Dec 18, 2018
c8f2643
Resolve comments
LeanidShutau Dec 19, 2018
f6ae212
Fix function calls in tests
LeanidShutau Dec 20, 2018
cd55d1b
Update snapshots
LeanidShutau Dec 20, 2018
dc0f083
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau Dec 20, 2018
4824da4
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau Dec 21, 2018
bf6aa0d
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau Dec 21, 2018
389699b
Fix config passing bug
LeanidShutau Dec 21, 2018
ab79ea5
Remove context comments from result json files
LeanidShutau Dec 21, 2018
85be52e
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau Dec 21, 2018
79fcc93
add missed reporter
pavel06081991 Dec 22, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions scripts/i18n_integrate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.
*/

require('../src/setup_node_env');
require('../src/dev/run_i18n_integrate');
63 changes: 63 additions & 0 deletions src/dev/i18n/__fixtures__/integrate_locale_files/fr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"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"
}
}
},
"messages": {
"plugin-1.message-id-1": "Translated text 1",
"plugin-1.message-id-2": "Translated text 2",
"plugin-2.message-id": "Translated text"
}
}
163 changes: 163 additions & 0 deletions src/dev/i18n/__snapshots__/integrate_locale_files.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`dev/i18n/integrate_locale_files integrateLocaleFiles splits locale file by plugins and writes them into the right folders 1`] = `
Array [
"src/dev/i18n/__fixtures__/integrate_locale_files/test_plugin_1/translations/fr.json",
"{
\\"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\\"
}
}
},
\\"messages\\": {
\\"plugin-1.message-id-1\\": \\"Translated text 1\\",
\\"plugin-1.message-id-2\\": \\"Translated text 2\\"
}
}",
]
`;

exports[`dev/i18n/integrate_locale_files integrateLocaleFiles splits locale file by plugins and writes them into the right folders 2`] = `
Array [
"src/dev/i18n/__fixtures__/integrate_locale_files/test_plugin_2/translations/fr.json",
"{
\\"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\\"
}
}
},
\\"messages\\": {
\\"plugin-2.message-id\\": \\"Translated text\\"
}
}",
]
`;

exports[`dev/i18n/integrate_locale_files integrateLocaleFiles splits locale file by plugins and writes them into the right folders 3`] = `
Array [
"src/dev/i18n/__fixtures__/integrate_locale_files/test_plugin_1/translations",
"src/dev/i18n/__fixtures__/integrate_locale_files/test_plugin_2/translations",
]
`;

exports[`dev/i18n/integrate_locale_files verifyMessages throws an error for unused id and missing id 1`] = `
"
Missing translations:
plugin-1.message-id-2"
`;

exports[`dev/i18n/integrate_locale_files verifyMessages throws an error for unused id and missing id 2`] = `
"
Unused translations:
plugin-1.message-id-3"
`;

exports[`dev/i18n/integrate_locale_files verifyMessages throws an error for unused id and missing id 3`] = `
"
Unused translations:
plugin-2.message
Missing translations:
plugin-2.message-id"
`;
2 changes: 2 additions & 0 deletions src/dev/i18n/__snapshots__/utils.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ exports[`i18n utils should create verbose parser error message 1`] = `
"
`;

exports[`i18n utils should normalizePath 1`] = `"src/dev/i18n"`;

exports[`i18n utils should not escape linebreaks 1`] = `
"Text
with
Expand Down
11 changes: 11 additions & 0 deletions src/dev/i18n/extract_default_translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
extractHandlebarsMessages,
} from './extractors';
import { globAsync, readFileAsync, normalizePath } from './utils';

import { createFailError, isFailError } from '../run';

function addMessageToMap(targetMap, key, value, reporter) {
Expand Down Expand Up @@ -147,3 +148,13 @@ export async function extractMessagesFromPathToMap(inputPath, targetMap, config,
})
);
}

export async function getDefaultMessagesMap(inputPaths, config, reporter) {
const defaultMessagesMap = new Map();

for (const inputPath of filterPaths(inputPaths, config.paths)) {
await extractMessagesFromPathToMap(inputPath, defaultMessagesMap, config, reporter);
}

return defaultMessagesMap;
}
112 changes: 112 additions & 0 deletions src/dev/i18n/integrate_locale_files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* 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 path from 'path';

import {
difference,
readFileAsync,
writeFileAsync,
accessAsync,
makeDirAsync,
normalizePath,
ErrorReporter,
} from './utils';
import { paths, exclude } from '../../../.i18nrc.json';
import { getDefaultMessagesMap } from './extract_default_translations';
import { createFailError } from '../run';
import { serializeToJson } from './serializers/json';

export function verifyMessages(localizedMessagesMap, defaultMessagesMap) {
let errorMessage = '';

const defaultMessagesIds = [...defaultMessagesMap.keys()];
const localizedMessagesIds = [...localizedMessagesMap.keys()];

const unusedTranslations = difference(localizedMessagesIds, defaultMessagesIds);
if (unusedTranslations.length > 0) {
errorMessage += `\nUnused translations:\n${unusedTranslations.join(', ')}`;
}

const missingTranslations = difference(defaultMessagesIds, localizedMessagesIds);
if (missingTranslations.length > 0) {
errorMessage += `\nMissing translations:\n${missingTranslations.join(', ')}`;
}

if (errorMessage) {
throw createFailError(errorMessage);
}
}

function groupMessagesByNamespace(localizedMessagesMap) {
const localizedMessagesByNamespace = new Map();
const knownNamespaces = Object.keys(paths);

for (const [messageId, messageValue] of localizedMessagesMap) {
const namespace = knownNamespaces.find(key => messageId.startsWith(`${key}.`));

if (!namespace) {
throw createFailError(`Unknown namespace in id ${messageId}.`);
}

if (!localizedMessagesByNamespace.has(namespace)) {
localizedMessagesByNamespace.set(namespace, []);
}

localizedMessagesByNamespace
.get(namespace)
.push([messageId, { message: messageValue.text || messageValue }]);
}

return localizedMessagesByNamespace;
}

async function writeMessages(localizedMessagesByNamespace, fileName, formats, log) {
for (const [namespace, messages] of localizedMessagesByNamespace) {
const destPath = path.resolve(paths[namespace], 'translations');

try {
await accessAsync(destPath);
} catch (_) {
await makeDirAsync(destPath);
}

const writePath = path.resolve(destPath, fileName);
await writeFileAsync(writePath, serializeToJson(messages, formats));
log.success(`Translations have been integrated to ${normalizePath(writePath)}`);
}
}

export async function integrateLocaleFiles(filePath, log) {
const reporter = new ErrorReporter();
const defaultMessagesMap = await getDefaultMessagesMap(['.'], { paths, exclude }, reporter);
const localizedMessages = JSON.parse((await readFileAsync(filePath)).toString());

if (!localizedMessages.formats) {
throw createFailError(`Locale file should contain formats object.`);
}

const localizedMessagesMap = new Map(Object.entries(localizedMessages.messages));
verifyMessages(localizedMessagesMap, defaultMessagesMap);

// use basename of filePath to write the same locale name as the source file has
const fileName = path.basename(filePath);
const localizedMessagesByNamespace = groupMessagesByNamespace(localizedMessagesMap);
await writeMessages(localizedMessagesByNamespace, fileName, localizedMessages.formats, log);
}
Loading