-
-
Notifications
You must be signed in to change notification settings - Fork 7
feat: localize JS keyboard search 🗺️ #658
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
Draft
darcywong00
wants to merge
4
commits into
master
Choose a base branch
from
feat/search-i18n
base: master
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.
+291
−17
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| { | ||
| "resultOne": { | ||
| "text": "result", | ||
| "crowdinContext": "1 keyboard result found" | ||
| }, | ||
| "resultMore": { | ||
| "text": "results", | ||
| "crowdinContext": "More than 1 keyboard results found" | ||
| }, | ||
| "pageNumberOfTotalPages": { | ||
| "text": "page {pageNumber} of {totalPages}.", | ||
| "crowdinContext": "Summary of how many pages of keyboard results" | ||
| }, | ||
| "keyboardSearchTitle": { | ||
| "text": "- Keyboard search", | ||
| "crowdinContext": "title" | ||
| }, | ||
| "obsoleteKeyboards": { | ||
| "text": "Obsolete keyboards", | ||
| "crowdinContext": "Separator for obsolete keyboards" | ||
| }, | ||
| "monthlyDownloadZero": { | ||
| "text": "No recent downloads", | ||
| "crowdinContext": "0 monthly downloads" | ||
| }, | ||
| "monthlyDownloadOne": { | ||
| "text": "monthly download", | ||
| "crowdinContext": "1 monthly download" | ||
| }, | ||
| "monthlyDownloadMore": { | ||
| "text": "monthly downloads", | ||
| "crowdinContext": "More than 1 monthly download" | ||
| }, | ||
| "notUnicode": { | ||
| "text": "Note: Not a Unicode keyboard", | ||
| "crowdinContext": "Disclaimer for legacy non-Unicode keyboard" | ||
| }, | ||
| "designedForPlatform": { | ||
| "text": "Designed for {platform}", | ||
| "crowdinContext": "Designed for {OS platform}" | ||
| }, | ||
| "noMatchesFoundForKeyboard": { | ||
| "text": "No matches found for '{keyboard}'", | ||
| "crowdinContext": "No keyboards found for search" | ||
| }, | ||
| "previousPager": { | ||
| "text": "< Previous", | ||
| "crowdinContext": "Previous pages" | ||
| }, | ||
| "nextPager": { | ||
| "text": "Next >", | ||
| "crowdinContext": "More pages" | ||
| } | ||
| } |
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,54 @@ | ||
| { | ||
| "resultOne": { | ||
| "text": "resultado", | ||
| "crowdinContext": "1 keyboard result found" | ||
| }, | ||
| "resultMore": { | ||
| "text": "resultados", | ||
| "crowdinContext": "More than 1 keyboard results found" | ||
| }, | ||
| "pageNumberOfTotalPages": { | ||
| "text": "página {pageNumber} de {totalPages}.", | ||
| "crowdinContext": "Summary of how many pages of keyboard results" | ||
| }, | ||
| "keyboardSearchTitle": { | ||
| "text": "- Búsqueda por teclado", | ||
| "crowdinContext": "title" | ||
| }, | ||
| "obsoleteKeyboards": { | ||
| "text": "Teclados obsoletos", | ||
| "crowdinContext": "Separator for obsolete keyboards" | ||
| }, | ||
| "monthlyDownloadZero": { | ||
| "text": "No hay descargas recientes", | ||
| "crowdinContext": "0 monthly downloads" | ||
| }, | ||
| "monthlyDownloadOne": { | ||
| "text": "descargas mensual", | ||
| "crowdinContext": "1 monthly download" | ||
| }, | ||
| "monthlyDownloadMore": { | ||
| "text": "descargas mensuales", | ||
| "crowdinContext": "More than 1 monthly download" | ||
| }, | ||
| "notUnicode": { | ||
| "text": "Nota: No es un teclado Unicode", | ||
| "crowdinContext": "Disclaimer for legacy non-Unicode keyboard" | ||
| }, | ||
| "designedForPlatform": { | ||
| "text": "Diseñado para {platform}", | ||
| "crowdinContext": "Designed for {OS platform}" | ||
| }, | ||
| "noMatchesFoundForKeyboard": { | ||
| "text": "No se encontraron coincidencias para '{keyboard}'", | ||
| "crowdinContext": "No keyboards found for search" | ||
| }, | ||
| "previousPager": { | ||
| "text": "< Anteriores", | ||
| "crowdinContext": "Previous pages" | ||
| }, | ||
| "nextPager": { | ||
| "text": "Más >", | ||
| "crowdinContext": "More pages" | ||
| } | ||
| } |
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,54 @@ | ||
| { | ||
| "resultOne": { | ||
| "text": "résultat", | ||
| "crowdinContext": "1 keyboard result found" | ||
| }, | ||
| "resultMore": { | ||
| "text": "résultats", | ||
| "crowdinContext": "More than 1 keyboard results found" | ||
| }, | ||
| "pageNumberOfTotalPages": { | ||
| "text": "page {pageNumber} sur {totalPages}.", | ||
| "crowdinContext": "Summary of how many pages of keyboard results" | ||
| }, | ||
| "keyboardSearchTitle": { | ||
| "text": "- Recherche au clavier", | ||
| "crowdinContext": "title" | ||
| }, | ||
| "obsoleteKeyboards": { | ||
| "text": "Claviers obsolètes", | ||
| "crowdinContext": "Separator for obsolete keyboards" | ||
| }, | ||
| "monthlyDownloadZero": { | ||
| "text": "Aucun téléchargement récent", | ||
| "crowdinContext": "0 monthly downloads" | ||
| }, | ||
| "monthlyDownloadOne": { | ||
| "text": "téléchargement mensuel", | ||
| "crowdinContext": "1 monthly download" | ||
| }, | ||
| "monthlyDownloadMore": { | ||
| "text": "téléchargements mensuels", | ||
| "crowdinContext": "More than 1 monthly download" | ||
| }, | ||
| "notUnicode": { | ||
| "text": "Remarque: Ce n'est pas un clavier Unicode.", | ||
| "crowdinContext": "Disclaimer for legacy non-Unicode keyboard" | ||
| }, | ||
| "designedForPlatform": { | ||
| "text": "Conçu pour {platform}", | ||
| "crowdinContext": "Designed for {OS platform}" | ||
| }, | ||
| "noMatchesFoundForKeyboard": { | ||
| "text": "Aucun résultat trouvé pour '{keyboard}'", | ||
| "crowdinContext": "No keyboards found for search" | ||
| }, | ||
| "previousPager": { | ||
| "text": "< Précédentes", | ||
| "crowdinContext": "Previous pages" | ||
| }, | ||
| "nextPager": { | ||
| "text": "Plus >", | ||
| "crowdinContext": "More pages" | ||
| } | ||
| } |
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,104 @@ | ||
| /** | ||
| * 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 | ||
| */ | ||
| import translationEN from './en.json' with { type: 'json' }; | ||
| import translationES from './es.json' with { type: 'json' }; | ||
| import translationFR from './fr.json' with { type: 'json' }; | ||
|
|
||
| const translations = { | ||
| "en": translationEN, | ||
| }; | ||
|
|
||
| /** | ||
| * Load translation for a language if not already added | ||
| * @param {String} lang | ||
| */ | ||
| function loadTranslation(lang) { | ||
| if (!translations.hasOwnProperty(lang)) { | ||
| switch(lang) { | ||
| case 'es' : | ||
| translations['es'] = translationES; | ||
| break; | ||
| case 'fr' : | ||
| translations['fr'] = translationFR; | ||
| break; | ||
| default: | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Navigates inside `obj` with `path` string, | ||
| * | ||
| * Usage: | ||
| * objNavigate({a: {b: 123}}, "a.b") // returns 123 | ||
| * | ||
| * Fails silently. | ||
| * @param {obj} obj | ||
| * @param {String} path to navigate into obj | ||
| * @returns String or undefined if variable is not found. | ||
| */ | ||
| function objNavigate(obj, path){ | ||
| var aPath = path.split('.'); | ||
| try { | ||
| return aPath.reduce((a, v) => a[v].text, obj); | ||
| } catch { | ||
| return; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * 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']); | ||
| */ | ||
| function 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} key | ||
| * @param {obj} interpolations for optional formatted parameters | ||
| * @returns localized string | ||
| */ | ||
| export default function t(key, interpolations) { | ||
| // embed_lang set by session.php | ||
| var language = embed_lang ?? "en"; | ||
|
|
||
| loadTranslation(language); | ||
|
|
||
| if (!translations[language]) { | ||
| // Langage is missing, so fallback to "en" | ||
| console.warn(`i18n for language: '${language}' missing, fallback to 'en'`); | ||
| language = "en"; | ||
| } | ||
|
|
||
| if (!translations[language].hasOwnProperty(key)) { | ||
| // key is missing for current language | ||
| console.warn(`key '${key}' missing in '${language}' translations`); | ||
| return ''; | ||
| } | ||
|
|
||
| const value = objNavigate(translations[language], key); | ||
| return strObjInterpolation(value, interpolations); | ||
| } |
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
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.
Was this old jquery? I was getting console error