-
Notifications
You must be signed in to change notification settings - Fork 238
Added multi language support. The language can now be switched (Need translation) #216
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
HumansAreWeak
wants to merge
10
commits into
lxgr-linux:master
Choose a base branch
from
HumansAreWeak:master
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.
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
01d6a5b
Added potential multi language support. The selected language itself …
HumansAreWeak 5ca7a89
made outsourced menu work, added default settings and language setting
lxgr-linux 11242cb
integrated language with settings
lxgr-linux b9eead8
I changed the language class a bit, instead of using the settings ins…
HumansAreWeak eb5f342
Replaced some hardcoded strings with string keys, it is still incompl…
HumansAreWeak 80db693
More removed hard coded language strings. Also added some German tran…
HumansAreWeak 95de05c
Added more translation stuff and removed hardcoded strings
HumansAreWeak 6602a60
Added trainers.py translations, may have added too much whitespace
HumansAreWeak 6006fb7
Made Pokete names translateable
HumansAreWeak 487a17e
Automatic schema generator, useful for adding new languages
HumansAreWeak 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 |
|---|---|---|
|
|
@@ -3,3 +3,4 @@ templates | |
|
|
||
| .idea | ||
| scrap_engine.py | ||
| venv | ||
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 @@ | ||
| { | ||
| "$schema": "schema.json", | ||
| "error.import.scrap_engine": "Es scheint als hättest du die Erweiterung 'scrap-engine' nicht installiert", | ||
| "ui.inventory.title": "Inventar", | ||
| "ui.lang.en_us": "Englisch (Amerikanisch)", | ||
| "ui.lang.de_de": "Deutsch", | ||
| "dialog.center.welcome": "Willkommen im Pokete-Center", | ||
| "dialog.center.action": "Was möchtest du tun?", | ||
| "dialog.center.healed": "Deine Pokete sind nun geheilt!", | ||
| "dialog.center.list": "1: Schaue dein Team an\n 2: Heil alle deine Pokete\n 3: Knuddel mit deinen Pokete" | ||
| } |
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,27 @@ | ||
| { | ||
| "$schema": "schema.json", | ||
| "error.import.scrap_engine": "You seem to not have the 'scrap-engine' package installed", | ||
| "ui.inventory.title": "Inventory", | ||
| "ui.lang.en_us": "English (American)", | ||
| "ui.lang.de_de": "German", | ||
| "dialog.center.welcome": "Welcome to the Pokete-Center", | ||
| "dialog.center.action": "What do you want to do?", | ||
| "dialog.center.list": "1: See your full deck\n 2: Heal all your Poketes\n 3: Cuddle with the Poketes", | ||
| "dialog.center.healed": "Your Poketes are now healed!", | ||
| "dialog.shop.welcome": "Welcome to the Pokete-Shop", | ||
| "dialog.shop.action": "Wanna buy something?", | ||
| "dialog.shop.leave": "Have a great day!", | ||
| "dialog.trader.welcome": "I'm a trader.", | ||
| "dialog.trader.action": "Here you can trade one of your Poketes for one from another trainer.", | ||
| "npc.test_npc.hey": "Hey.", | ||
| "npc.old_man.greet": "Hello, young trainer.", | ||
| "npc.old_man.talk": "When I was your age... I also was a trainer.", | ||
| "npc.old_man.found_hyperball": "One day I found this very special Poketeball...", | ||
| "npc.old_man.hyperball": "A Hyperball!", | ||
| "npc.old_man.rarest": "It's one of the oldest and rarest of them all.", | ||
| "npc.old_man.keep": "You can keep it!", | ||
| "trainer.playmap1.franz.fight": "Wanna fight?", | ||
| "trainer.playmap1.franz.lose1": "Hahaha!", | ||
| "trainer.playmap1.franz.lose2": "You're a loser!", | ||
| "trainer.playmap1.franz.win": "You're a very good trainer!" | ||
| } |
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,30 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema", | ||
| "title": "Pokete Translation File Schema", | ||
| "properties": { | ||
| "error.import.scrap_engine": { | ||
| "type": "string" | ||
| }, | ||
| "ui.inventory.title": { | ||
| "type": "string" | ||
| }, | ||
| "ui.lang.en_us": { | ||
| "type": "string" | ||
| }, | ||
| "ui.lang.de_de": { | ||
| "type": "string" | ||
| }, | ||
| "dialog.center.welcome": { | ||
| "type": "string" | ||
| }, | ||
| "dialog.center.action": { | ||
| "type": "string" | ||
| }, | ||
| "dialog.center.list": { | ||
| "type": "string" | ||
| }, | ||
| "dialog.center.healed": { | ||
| "type": "string" | ||
| } | ||
| } | ||
| } |
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,5 @@ | ||
| from pathlib import Path | ||
|
|
||
| # Holds the path to the main pokete folder. Every path related task should be | ||
| # referring to this constant to get access to all the internal files. READ-ONLY | ||
| CWD: Path = Path(__file__).parent.parent.resolve() |
Empty file.
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,13 @@ | ||
| from pathlib import Path | ||
| from dataclasses import dataclass | ||
| from ..constants import CWD | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class LanguageEntity: | ||
| """ | ||
| Stores general information about a language. This is READ-ONLY so this | ||
| data class is marked as frozen to prevent accidental writes. | ||
| """ | ||
| name: str = "en_US" | ||
| path: Path = (CWD / "assets" / "lang" / "en_US.json").resolve() |
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,134 @@ | ||
| from __future__ import annotations | ||
| from pathlib import Path | ||
| import json | ||
| import locale | ||
| from .entities.language_entity import LanguageEntity | ||
| from .color import Color | ||
|
|
||
|
|
||
| def get_system_locale() -> str: | ||
| """ | ||
| Fetches the main language of the user's operating system. | ||
|
|
||
| Returns: ISO language code | ||
| """ | ||
| locale.setlocale(locale.LC_ALL, "") | ||
| return locale.getlocale(locale.LC_CTYPE)[0] | ||
|
|
||
|
|
||
| class Language: | ||
| """ | ||
| Class for loading and managing translation files. | ||
| """ | ||
|
|
||
| __instance = None | ||
|
|
||
| @classmethod | ||
| def instance(cls) -> Language: | ||
| """ | ||
| Returns: Singleton instance of language class | ||
| """ | ||
| if Language.__instance is None: | ||
| Language.__instance = Language() | ||
| return Language.__instance | ||
|
|
||
| def __init__(self, default_lang=None): | ||
| self._language_file: dict = dict() | ||
| self.language: LanguageEntity = LanguageEntity() | ||
| self.language_path: Path = self.language.path.parent | ||
| self.change_language(default_lang or get_system_locale()) | ||
|
|
||
| def str(self, key: str) -> str: | ||
| """ | ||
| Fetch a localized string at from the currently used language. | ||
|
|
||
| Args: | ||
| key: Identifier of the string to fetch | ||
|
|
||
| Returns: Localized string | ||
| """ | ||
| if key in self._language_file: | ||
| return str(self._language_file.get(key)) | ||
|
|
||
| # Temporarily - So previous code is not broken | ||
| return key | ||
|
|
||
| # raise RuntimeWarning(f"No entry with key '{key}' was found in the | ||
| # translation file '{self.language.path}'") | ||
|
|
||
| def change_language(self, language: str) -> LanguageEntity: | ||
| """ | ||
| Changes the current language. If parsed language identifier is invalid | ||
| then the fallback language 'en_US' will be used. | ||
|
|
||
| Args: | ||
| language: Language file to look for | ||
|
|
||
| Returns: Language entity. Will fall back to 'en_US' if no translation | ||
| file has been found. | ||
| """ | ||
| self.language = self._get_language_entity(language) | ||
| self._load_language_file(self.language.path) | ||
| return self.language | ||
|
|
||
| def get_selected_language(self) -> LanguageEntity: | ||
| """ | ||
| Returns | ||
|
|
||
| Returns: Selected language | ||
| """ | ||
| return self.language | ||
|
|
||
| def get_languages(self) -> list: | ||
| """ | ||
| Fetches and returns a list of all available language files. | ||
|
|
||
| Returns: List of available language files in the assets directory | ||
| """ | ||
| available_languages = [] | ||
|
|
||
| for item in self.language_path.glob("*.json"): | ||
| if item.is_file(): | ||
| language_name = item.name.replace(".json", "") | ||
|
|
||
| if language_name != "schema": | ||
| available_languages.append(language_name) | ||
|
|
||
| return available_languages | ||
|
|
||
| def _get_language_entity(self, language: str) -> LanguageEntity: | ||
| """ | ||
| This method will return a language entity, which contains its name | ||
| and its absolute path. | ||
|
|
||
| Args: | ||
| language: Language file to look for | ||
|
|
||
| Returns: Language entity. Will fall back to 'en_US' if no translation | ||
| file has been found. | ||
| """ | ||
| entity = LanguageEntity() | ||
|
|
||
| language_file = self.language_path / f"{language}.json" | ||
|
|
||
| if language_file.exists() and language_file.is_file(): | ||
| entity = LanguageEntity(language, language_file.resolve()) | ||
|
|
||
| return entity | ||
|
|
||
| def _load_language_file(self, language_path: Path) -> None: | ||
| """ | ||
| Loads the language file into the internal dictionary. | ||
|
|
||
| Args: | ||
| language_path: Path to the language file | ||
| """ | ||
| with open(language_path, encoding="utf-8") as language_file: | ||
| self._language_file = json.load(language_file) | ||
|
|
||
|
|
||
| # For convenient use later in the program ~ origin | ||
| _ = Language.instance().str | ||
|
|
||
| if __name__ == "__main__": | ||
| print(f"\033[31;1mDo not execute this!{Color.reset}") |
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
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.
I don't know if this is really necessary, since python itself will throw an error about this and this isn't even the first file to import scrap_engine.
Uh oh!
There was an error while loading. Please reload this page.
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.
Yeah this was kind of a test I forgot to remove. I thought it would be nice to fetch all the required packages on startup first and see if all are available, otherwise a message with all the missing packages will be printed. But I wasn't able to implement this behavior yet :o