-
-
Notifications
You must be signed in to change notification settings - Fork 7
chore: rework i18n for js to avoid async and duplication #668
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
Open
mcdurdin
wants to merge
1
commit into
feat/search-i18n
Choose a base branch
from
chore/search-i18n-structural-tweaks
base: feat/search-i18n
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+193
−237
Open
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 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,125 @@ | ||||||||||||
| /** | ||||||||||||
| * Keyman is copyright (c) SIL Global. MIT License | ||||||||||||
| * | ||||||||||||
| * Vanilla JS for localizing keyboard search strings without a framework | ||||||||||||
| * Reference: https://medium.com/@mihura.ian/translations-in-vanilla-javascript-c942c2095170 | ||||||||||||
| * | ||||||||||||
| * Domains are loaded by Head::render() with the js_i18n_domains property, e.g. | ||||||||||||
| * | ||||||||||||
| * 'js_i18n_domains' => [ | ||||||||||||
| * 'keyboard-search' => Locale::domain_js('keyboard-search'), | ||||||||||||
| * ], | ||||||||||||
| * | ||||||||||||
| */ | ||||||||||||
|
|
||||||||||||
| export class I18n { | ||||||||||||
| // domains member has the following structure: | ||||||||||||
| // [ | ||||||||||||
| // { "locale": "fr", "strings": { "key": "value", ... } }, | ||||||||||||
| // { "locale": "en", "strings": { "key": "value", ... } }, | ||||||||||||
| // ... | ||||||||||||
| // ] | ||||||||||||
| static domains = []; | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Load the strings for the given domain | ||||||||||||
| * @param {string} domain | ||||||||||||
| */ | ||||||||||||
|
Comment on lines
+26
to
+27
Contributor
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.
Suggested change
|
||||||||||||
| static loadDomain(domain) { | ||||||||||||
|
|
||||||||||||
| // avoid reporting domain-load errors more than once | ||||||||||||
| I18n.domains[domain] = { locales: [] }; | ||||||||||||
|
|
||||||||||||
| const json = document.getElementById('i18n_'+domain)?.text; | ||||||||||||
| if(!json) { | ||||||||||||
| console.error(`i18n domain '${domain}' was not loaded`); | ||||||||||||
| return false; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| try { | ||||||||||||
| I18n.domains[domain] = { | ||||||||||||
| locales: JSON.parse(json) | ||||||||||||
| }; | ||||||||||||
| } catch(e) { | ||||||||||||
| // Handle JSON parse errors so we get a functioning page, even if it has | ||||||||||||
| // no localized strings visible | ||||||||||||
| console.error(e); | ||||||||||||
| return false; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| return true; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Navigates inside `obj` with `path` string, | ||||||||||||
| * | ||||||||||||
| * Usage: | ||||||||||||
| * objNavigate({a: {b: {c: 123}}}, "a.b.c") // returns 123 | ||||||||||||
| * | ||||||||||||
| * Fails silently. | ||||||||||||
| * @param {obj} obj | ||||||||||||
| * @param {String} path to navigate into obj | ||||||||||||
| * @returns String or undefined if variable is not found. | ||||||||||||
| */ | ||||||||||||
| static objNavigate(obj, path){ | ||||||||||||
| if(!obj) return undefined; | ||||||||||||
| var aPath = path.split('.'); | ||||||||||||
| try { | ||||||||||||
| return aPath.reduce((a, v) => a[v], obj); | ||||||||||||
| } catch { | ||||||||||||
| return undefined; | ||||||||||||
| } | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Interpolates variables wrapped with `{}` in `str` with variables in `obj` | ||||||||||||
| * It will replace what it can, and leave the rest untouched | ||||||||||||
| * | ||||||||||||
| * Usage: | ||||||||||||
| * | ||||||||||||
| * named variables: | ||||||||||||
| * strObjInterpolation("I'm {age} years old!", { age: 29 }); | ||||||||||||
| * | ||||||||||||
| * ordered variables | ||||||||||||
| * strObjInterpolation("The {0} says {1}, {1}, {1}!", ['cow', 'moo']); | ||||||||||||
| */ | ||||||||||||
| static strObjInterpolation(str, obj){ | ||||||||||||
| obj = obj || []; | ||||||||||||
| str = str ? str.toString() : ''; | ||||||||||||
| return str.replace( | ||||||||||||
| /{([^{}]*)}/g, | ||||||||||||
| (a, b) => { | ||||||||||||
| const r = obj[b]; | ||||||||||||
| return typeof r === 'string' || typeof r === 'number' ? r : a; | ||||||||||||
| }, | ||||||||||||
| ); | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Determine the display UI language for the keyboard search | ||||||||||||
| * Navigate the translation JSON | ||||||||||||
| * @param {string} domain of the localized strings | ||||||||||||
| * @param {string} key for the string | ||||||||||||
| * @param {obj} interpolations for optional formatted parameters | ||||||||||||
| * @returns localized string | ||||||||||||
| */ | ||||||||||||
| static t(domain, key, interpolations) { | ||||||||||||
| if (!I18n.domains[domain]) { | ||||||||||||
| if(!I18n.loadDomain(domain)) { | ||||||||||||
| return key; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Find best matching string in the available locales | ||||||||||||
| for(const locale of I18n.domains[domain].locales) { | ||||||||||||
| const value = I18n.objNavigate(locale.strings, key); | ||||||||||||
| if(value) { | ||||||||||||
| return I18n.strObjInterpolation(value, interpolations); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| console.warn(`Missing localization string in '${domain}' for '${key}' in all loaded locales`); | ||||||||||||
| // TODO: log to sentry? | ||||||||||||
| return key; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
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.
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.
Just wanted to confirm the folder structure for all the localized strings will be:
PHP (existing)
.../locale/strings/keyboards/(for the main keyboards/ page)JS (so far)
.../locale/strings/keyboard-search/So we don't want to have the JS strings somewhere in
/locale/strings/keyboards/?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.
The JS paths in crowdin.yml would need to be updated.
I can do here or back on my base PR
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.
There's a lot of munging on the "embed json i18n strings" for loop.
Would it be cleaner to wrap it in a Util.php function?