-
Notifications
You must be signed in to change notification settings - Fork 2.6k
add multi-language support (localization) #2133
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
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2259999
feat: add multi-language support (localization)
Parvinmh 882d93b
refactor(translator): apply PR review fixes for language handling
Parvinmh cd0675b
run prettier
Parvinmh fa79845
refactor(translator): apply PR review fixes for language handling
Parvinmh 6dc8a3d
Trigger CI
Parvinmh 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,11 @@ | ||
| export default { | ||
| buttons: { | ||
| next: "Weiter", | ||
| prev: "Zurück", | ||
| skip: "Überspringen", | ||
| done: "Fertig", | ||
| }, | ||
| alerts: { | ||
| dontShowAgainLabel: "Nicht mehr anzeigen", | ||
| }, | ||
| }; |
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,11 @@ | ||
| export default { | ||
| buttons: { | ||
| next: "Next", | ||
| prev: "Back", | ||
| skip: "Skip", | ||
| done: "Done", | ||
| }, | ||
| alerts: { | ||
| dontShowAgainLabel: "Don't show this again", | ||
| }, | ||
| }; | ||
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,11 @@ | ||
| export default { | ||
| buttons: { | ||
| next: "Siguiente", | ||
| prev: "Atrás", | ||
| skip: "Saltar", | ||
| done: "Hecho", | ||
| }, | ||
| alerts: { | ||
| dontShowAgainLabel: "No mostrar de nuevo", | ||
| }, | ||
| }; |
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,11 @@ | ||
| export default { | ||
| buttons: { | ||
| next: "بعدی", | ||
| prev: "قبلی", | ||
| skip: "رد کردن", | ||
| done: "پایان", | ||
| }, | ||
| alerts: { | ||
| dontShowAgainLabel: "دیگه نشون نده", | ||
| }, | ||
| }; |
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,11 @@ | ||
| export default { | ||
| buttons: { | ||
| next: "Suivant", | ||
| prev: "Retour", | ||
| skip: "Passer", | ||
| done: "Terminé", | ||
| }, | ||
| alerts: { | ||
| dontShowAgainLabel: "Ne plus afficher", | ||
| }, | ||
| }; |
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,52 @@ | ||
| import enUS from "./en_US"; | ||
|
|
||
| type MessageFormat = (...args: any[]) => string; | ||
| type Message = string | MessageFormat; | ||
| export type Language = { [key: string]: Message | Language }; | ||
|
|
||
| export class Translator { | ||
| private _language: Language; | ||
|
|
||
| constructor(language?: Language) { | ||
| if (language) { | ||
| this._language = language; | ||
| } else { | ||
| const browserLang = navigator.language.replace("-", "_"); | ||
| try { | ||
| this._language = require(`./${browserLang}`).default; | ||
Parvinmh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } catch { | ||
| this._language = enUS; | ||
Parvinmh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| setLanguage(language: Language) { | ||
| this._language = language; | ||
| } | ||
|
|
||
| private getString(message: string, lang: Language): MessageFormat | null { | ||
Parvinmh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!lang || !message) return null; | ||
|
|
||
| const splitted = message.split("."); | ||
| const key = splitted[0]; | ||
|
|
||
| if (lang[key]) { | ||
| const val = lang[key]; | ||
|
|
||
| if (typeof val === "string") { | ||
| return (): string => val; | ||
| } else if (typeof val === "function") { | ||
| return val; | ||
| } else { | ||
| return this.getString(splitted.slice(1).join("."), val); | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| translate(message: string, ...args: any[]): string { | ||
| const translated = this.getString(message, this._language); | ||
| return translated ? translated(...args) : message; | ||
| } | ||
| } | ||
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
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.