Skip to content
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

load translation based on OS language #92

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,13 @@ def change_lang_callback(self, enabled:bool):
if(not enabled): # Language not selected
return

app.removeTranslator(translator)

user_lang_id = self.language_action_group.checkedAction().data()
self.profile_ui.set_global_setting("language",user_lang_id) # store language setting

if user_lang_id == self.profile_ui.get_global_setting("language",DEFAULTLANG): # If user selected language same as current language
return

app.removeTranslator(translator)
self.profile_ui.set_global_setting("language",user_lang_id) # store language setting
self.languagechanged.emit() # loading in next start

def restart_app(self):
Expand Down
31 changes: 23 additions & 8 deletions profile_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,37 @@ def load_profiles_from_file(self):
if self.ui_initialized:
self.refresh_combox_list()

def create_or_update_profile_file(self, create: bool = False):
"""Create a profile file if not exist, else update the existing one."""
def create_or_update_profile_file(self, create: bool = False) -> bool:
"""Create a new profile file or update existing one

Args:
create: Whether to create a new file

Returns:
bool: Whether operation succeeded
"""
if create:
self.profiles = self.__PROFILES_TEMPLATE
os_lang = PyQt6.QtCore.QLocale.system().name() # get system language, such as zh_CN
lang_file = os.path.join("translations", f"{os_lang}.qm")

# Use system language if translation exists, otherwise use English
if os.path.exists(lang_file):
default_lang = os_lang
else:
default_lang = "en_US"

self.profiles['global']['language'] = default_lang
self.log(f"Profile: use {default_lang} as default language")
self.log("Profile: profile file created")

try:
file = open(self.__PROFILES_FILENAME, "w", encoding="utf_8")
with open(self.__PROFILES_FILENAME, "w", encoding="utf_8") as f:
json.dump(self.profiles, f)
return True
except OSError:
return False

with file as profile_file:
json.dump(self.profiles, profile_file)

return True

def get_global_setting(self,key : str, default = None):
"""Returns an entry of the global section or saves a default if set and not found"""
if "global" in self.profiles:
Expand Down
2 changes: 1 addition & 1 deletion translations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ In PyQt6, internationalization (i18n) typically involves translating the text of
- Use Linguist to create a translation file (.ts file). This file will contain all the text in your application that needs to be translated.

2. Use Translation Files in the Application:
- In your PyQt6 application, use `QtCore.QLocale` to determine the user's preferred language.
- In your PyQt6 application, use `PyQt6.QtCore.QLocale.system().name()` to determine the user's preferred language.
- Use `QtCore.QTranslator` to load the appropriate translation file based on the user's preferred language setting.

3. Mark Text for Translation in the User Interface:
Expand Down