-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Implement a build tool for locale files integration #19826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pavel06081991
merged 46 commits into
elastic:master
from
LeanidShutau:locale-files-checker
Dec 22, 2018
Merged
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 e4ae11f
Refactor locale files verification tool
LeanidShutau 7b0eafc
Fix default messages structure
LeanidShutau f89b20f
Merge branch 'master' into locale-files-checker
LeanidShutau d17eb99
Return to plain structured JSON files
LeanidShutau 94402a3
Merge branch 'master' into locale-files-checker
LeanidShutau 9a34c13
Update locale files checking tool
LeanidShutau 85e5f14
Merge branch 'master' into locale-files-checker
LeanidShutau 24314b1
Add unit tests
LeanidShutau 2f7725f
Merge branch 'master' into locale-files-checker
LeanidShutau 7ba5de5
Fix tests
LeanidShutau 203b797
Merge branch 'master' into locale-files-checker
LeanidShutau 4b72017
Resolve comments
LeanidShutau 6da7797
Normalize paths for crossplatform snapshot testing
LeanidShutau d24c954
Merge branch 'master' into locale-files-checker
LeanidShutau 06b3bad
Add locale files integration
LeanidShutau a005cff
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau 9507818
Update integration tool
LeanidShutau 38b8e27
Resolve comments
LeanidShutau 970add6
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau 9bcef28
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau 10c9022
Add mkdir for translations folders
LeanidShutau a3250bf
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau 83ef984
Fix wrong variable name
LeanidShutau 4acebd6
Rename script
maryia-lapata 8ffcf96
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau e1804c4
Resolve comments
LeanidShutau e16abcd
Fix tests
LeanidShutau 4867577
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau 0225c46
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau 025602e
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau 139b6ba
Fix locale file format bug
LeanidShutau 7e69c9d
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau 6952263
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau c7c3f6a
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau bd09898
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau c8f2643
Resolve comments
LeanidShutau f6ae212
Fix function calls in tests
LeanidShutau cd55d1b
Update snapshots
LeanidShutau dc0f083
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau 4824da4
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau bf6aa0d
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau 389699b
Fix config passing bug
LeanidShutau ab79ea5
Remove context comments from result json files
LeanidShutau 85be52e
Merge remote-tracking branch 'upstream/master' into locale-files-checker
LeanidShutau 79fcc93
add missed reporter
pavel06081991 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
163
src/dev/i18n/__snapshots__/integrate_locale_files.test.js.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| `; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
LeanidShutau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.