This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
Use jed1x json format to correctly handle pluralization #753
Merged
Merged
Changes from all commits
Commits
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 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 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 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 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,69 @@ | ||
const set = require('lodash.set') | ||
|
||
/** | ||
* Convert a Jed1x-Translate object to a nested JSON object. | ||
* | ||
* Create a nested JSON object, clean up keys from jed1x context with special | ||
* character, remove keys without values, convert values from array to string, | ||
* if strings are for plural forms, join them with the pipe character. | ||
* Go from this: | ||
* { | ||
* "browse-page.load\u0004Load more results": [ | ||
* "Загрузить ещё результаты" | ||
* ], | ||
* "browse-page.all-result-count-more\u0004Over ###localeCount### results": [ | ||
* "Более ###localeCount### результата", | ||
* "Более ###localeCount### результатов", | ||
* "Более ###localeCount### результатов" | ||
* ] | ||
* "browse-page.search-form.button\u0004Search": [], | ||
* } | ||
* To: | ||
* { | ||
* "browse-page: { | ||
* "load": "Загрузить ещё результаты" | ||
* "all-result-count-more": "Более ###localeCount### результата|Более ###localeCount### результатов|Более ###localeCount### результатов", | ||
* } | ||
* | ||
*/ | ||
Comment on lines
+3
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is excellent documentation, great! |
||
|
||
// special character, context delimiter in jed format: | ||
// https://github.com/messageformat/Jed/blob/351c47d5c57c5c81e418414c53ca84075c518edb/jed.js#L117 | ||
const SPLIT_CHAR = String.fromCharCode(4) | ||
|
||
function jed1xJsonToJson(jed1xObject) { | ||
const result = {} | ||
Object.entries(jed1xObject?.locale_data?.messages).forEach(([key, value]) => { | ||
const cleanedKey = key.slice(0, key.indexOf(SPLIT_CHAR)) | ||
if (value.length > 0) { | ||
const cleanedValue = value.length === 1 ? value[0] : value.join('|') | ||
return set(result, cleanedKey, cleanedValue) | ||
sarayourfriend marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}) | ||
return result | ||
} | ||
|
||
// test | ||
// node jed1x-json-to-json.js | ||
// console.log( | ||
// JSON.stringify( | ||
// jed1xJsonToJson({ | ||
// locale_data: { | ||
// messages: { | ||
// 'browse-page.load\u0004Load more results': [ | ||
// 'Загрузить ещё результаты', | ||
// ], | ||
// 'browse-page.all-result-count-more\u0004Over ###localeCount### results': | ||
// [ | ||
// 'Более ###localeCount### результата', | ||
// 'Более ###localeCount### результатов', | ||
// 'Более ###localeCount### результатов', | ||
// ], | ||
// 'browse-page.search-form.button\u0004Search': [], | ||
// }, | ||
// }, | ||
// }) | ||
// ) | ||
// ) | ||
|
||
module.exports = jed1xJsonToJson |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if there's some kind of ESLint rule we could write to emit a warning or something when we find a translation string that should probably have plural variations... like maybe something basic, if the English string ends with
s
and also contains a variable interpolation? 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's an interesting idea, @sarayourfriend !