Toolbox for analyzing and editing pkg application files for psp,ps3, ps4 and ps5, includes the most useful functions you might need.
| 1 | from PyQt5.QtCore import QTranslator, QLocale |
| 2 | import json |
| 3 | import os |
| 4 | import sys |
| 5 | |
| 6 | class Translator: |
| 7 | def __init__(self): |
| 8 | self.translator = QTranslator() |
| 9 | self.current_language = "en" |
| 10 | self.translations = {} |
| 11 | self.load_translations() |
| 12 | |
| 13 | def load_translations(self): |
| 14 | """Load all translation files""" |
| 15 | locales_dir = None |
| 16 | |
| 17 | # Try PyInstaller frozen paths first |
| 18 | try: |
| 19 | if getattr(sys, 'frozen', False): |
| 20 | # Prefer the unpacked temp directory |
| 21 | base_path = getattr(sys, '_MEIPASS', None) |
| 22 | if base_path: |
| 23 | candidate = os.path.join(base_path, 'GraphicUserInterface', 'locales') |
| 24 | if os.path.isdir(candidate): |
| 25 | locales_dir = candidate |
| 26 | |
| 27 | # PyInstaller onedir structure often places assets under _internal |
| 28 | if not locales_dir: |
| 29 | exec_dir = os.path.dirname(sys.executable) |
| 30 | candidate = os.path.join(exec_dir, '_internal', 'GraphicUserInterface', 'locales') |
| 31 | if os.path.isdir(candidate): |
| 32 | locales_dir = candidate |
| 33 | except Exception: |
| 34 | # Ignore path detection errors and fallback below |
| 35 | pass |
| 36 | |
| 37 | # Fallback to the package directory when running from source or if the above fails |
| 38 | if not locales_dir: |
| 39 | locales_dir = os.path.dirname(os.path.abspath(__file__)) |
| 40 | |
| 41 | # Load all json translations if the folder exists |
| 42 | if os.path.isdir(locales_dir): |
| 43 | for file in os.listdir(locales_dir): |
| 44 | if file.endswith('.json'): |
| 45 | lang_code = file[:-5] # Remove .json |
| 46 | with open(os.path.join(locales_dir, file), 'r', encoding='utf-8') as f: |
| 47 | self.translations[lang_code] = json.load(f) |
| 48 | else: |
| 49 | # If the folder is missing, keep an empty translations map (UI will show default text) |
| 50 | self.translations = {} |
| 51 | |
| 52 | def translate(self, text): |
| 53 | """Translate text to current language""" |
| 54 | if self.current_language in self.translations: |
| 55 | return self.translations[self.current_language].get(text, text) |
| 56 | return text |
| 57 | |
| 58 | def change_language(self, lang_code): |
| 59 | """Change current language""" |
| 60 | if lang_code in self.translations: |
| 61 | self.current_language = lang_code |
| 62 | return True |
| 63 | return False |