Skip to content

Commit 3b1736c

Browse files
tinyboxxxUltrawipf
authored andcommitted
load translation based on OS language
- when init profile file (fresh install), load translation based on OS language. - skip app restart if user select same language as current one.
1 parent ed61e5a commit 3b1736c

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,13 @@ def change_lang_callback(self, enabled:bool):
214214
if(not enabled): # Language not selected
215215
return
216216

217-
app.removeTranslator(translator)
218-
219217
user_lang_id = self.language_action_group.checkedAction().data()
220-
self.profile_ui.set_global_setting("language",user_lang_id) # store language setting
221218

219+
if user_lang_id == self.profile_ui.get_global_setting("language",DEFAULTLANG): # If user selected language same as current language
220+
return
221+
222+
app.removeTranslator(translator)
223+
self.profile_ui.set_global_setting("language",user_lang_id) # store language setting
222224
self.languagechanged.emit() # loading in next start
223225

224226
def restart_app(self):

profile_ui.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,37 @@ def load_profiles_from_file(self):
154154
if self.ui_initialized:
155155
self.refresh_combox_list()
156156

157-
def create_or_update_profile_file(self, create: bool = False):
158-
"""Create a profile file if not exist, else update the existing one."""
157+
def create_or_update_profile_file(self, create: bool = False) -> bool:
158+
"""Create a new profile file or update existing one
159+
160+
Args:
161+
create: Whether to create a new file
162+
163+
Returns:
164+
bool: Whether operation succeeded
165+
"""
159166
if create:
160167
self.profiles = self.__PROFILES_TEMPLATE
168+
os_lang = PyQt6.QtCore.QLocale.system().name() # get system language, such as zh_CN
169+
lang_file = os.path.join("translations", f"{os_lang}.qm")
170+
171+
# Use system language if translation exists, otherwise use English
172+
if os.path.exists(lang_file):
173+
default_lang = os_lang
174+
else:
175+
default_lang = "en_US"
176+
177+
self.profiles['global']['language'] = default_lang
178+
self.log(f"Profile: use {default_lang} as default language")
161179
self.log("Profile: profile file created")
162180

163181
try:
164-
file = open(self.__PROFILES_FILENAME, "w", encoding="utf_8")
182+
with open(self.__PROFILES_FILENAME, "w", encoding="utf_8") as f:
183+
json.dump(self.profiles, f)
184+
return True
165185
except OSError:
166186
return False
167187

168-
with file as profile_file:
169-
json.dump(self.profiles, profile_file)
170-
171-
return True
172-
173188
def get_global_setting(self,key : str, default = None):
174189
"""Returns an entry of the global section or saves a default if set and not found"""
175190
if "global" in self.profiles:

translations/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ In PyQt6, internationalization (i18n) typically involves translating the text of
2323
- Use Linguist to create a translation file (.ts file). This file will contain all the text in your application that needs to be translated.
2424

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

2929
3. Mark Text for Translation in the User Interface:

0 commit comments

Comments
 (0)