diff --git a/BabbleApp/Locale/English/locale.json b/BabbleApp/Locale/English/locale.json new file mode 100644 index 0000000..2f4845b --- /dev/null +++ b/BabbleApp/Locale/English/locale.json @@ -0,0 +1,26 @@ +{ + "general.checkForUpdates" : "Check For Updates" + "general.toolip" : "Toggle update check on launch", + "general.oscSettings" : "OSC Settings", + "general.locationPrefix" : "Location Prefix", + "general.locationTooltip" : "Prefix for OSC address", + "general.address" : "Address", + "general.addressTooltip" : "IP address we send OSC data to", + "general.port" : "Port", + "general.portTooltip" : "OSC port we send data to", + "general.receiver" : "Receive functions", + "general.receiverTooltip" : "Toggle OSC receive functions", + "general.receiverPort" : "Receiver Port", + "general.receiverPortTooltip" : "Port we receive OSC data from (used to recalibrate from within VRChat.", + "general.recalibrate" : "Recalibrate Address", + "general.recalibrateTooltip" : "OSC address we use for recalibrating", + "general.uvcCameraSettings" : "UVC Camera Settings", + "general.useRedChannel" : "Use Red Channel", + "general.useRedChannelTooltip" : "Uses only the red channel for Omnicept capture.", + "general.xResolution" : "X Resolution", + "general.xResolutionTooltip" : "X capture resolution. Default = 0", + "general.yResolution" : "Y Resolution", + "general.yResolutionTooltip" : "Y capture resolution. Default = 0", + "general.framerate" : "Framerate", + "general.framerateTooltip" : "Capture framerate. Default = 0", +} \ No newline at end of file diff --git a/BabbleApp/babbleapp.py b/BabbleApp/babbleapp.py index e979dcd..f86d994 100644 --- a/BabbleApp/babbleapp.py +++ b/BabbleApp/babbleapp.py @@ -29,7 +29,8 @@ from osc import VRChatOSCReceiver, VRChatOSC from general_settings_widget import SettingsWidget from algo_settings_widget import AlgoSettingsWidget from calib_settings_widget import CalibSettingsWidget -from utils.misc_utils import is_nt +from utils.misc_utils import EnsurePath, is_nt +from lang_manager import LocaleStringManager if is_nt: from winotify import Notification os.system('color') # init ANSI color @@ -57,6 +58,10 @@ def main(): config: BabbleConfig = BabbleConfig.load() config.save() + EnsurePath() + + # Init locale manager + LocaleStringManager("Locale", config.settings.gui_language) cancellation_event = threading.Event() ROSC = False # Check to see if we can connect to our video source first. If not, bring up camera finding diff --git a/BabbleApp/config.py b/BabbleApp/config.py index 2b740b0..9c160f1 100644 --- a/BabbleApp/config.py +++ b/BabbleApp/config.py @@ -2,6 +2,7 @@ import json import os.path import shutil +from BabbleApp.utils.misc_utils import EnsurePath from tab import Tab from pydantic import BaseModel from typing import Union @@ -44,6 +45,7 @@ class BabbleSettingsConfig(BaseModel): gui_cam_framerate: int = 0 use_calibration: bool = False calibration_mode: str = 'Neutral' + gui_language: str = 'English' class BabbleConfig(BaseModel): version: int = 1 @@ -53,6 +55,8 @@ class BabbleConfig(BaseModel): @staticmethod def load(): + EnsurePath() + if not os.path.exists(CONFIG_FILE_NAME): print("No settings file, using base settings") return BabbleConfig() @@ -75,6 +79,8 @@ class BabbleConfig(BaseModel): return load_config def save(self): + EnsurePath() + # make sure this is only called if there is a change if os.path.exists(CONFIG_FILE_NAME): try: diff --git a/BabbleApp/general_settings_widget.py b/BabbleApp/general_settings_widget.py index c2e8e6f..45e866d 100644 --- a/BabbleApp/general_settings_widget.py +++ b/BabbleApp/general_settings_widget.py @@ -1,4 +1,5 @@ import PySimpleGUI as sg +from lang_manager import LocaleStringManager as lang from config import BabbleSettingsConfig from osc import Tab from queue import Queue @@ -20,136 +21,151 @@ class SettingsWidget: self.gui_cam_resolution_y = f"-CAMRESY{widget_id}-" self.gui_cam_framerate = f"-CAMFRAMERATE{widget_id}-" self.gui_use_red_channel = f"-REDCHANNEL{widget_id}-" + self.gui_language = f"-LANGUAGE{widget_id}-" # Add this line self.main_config = main_config self.config = main_config.settings self.osc_queue = osc_queue + self.available_languages = lang._instance.get_languages() # Add more languages as needed + + bg_color_highlight = '#424042' + bg_color_clear = '#242224' # Define the window's contents self.general_settings_layout = [ - - - [sg.Checkbox( - "Check For Updates", + lang._instance.get_string("general.checkForUpdates"), default=self.config.gui_update_check, key=self.gui_update_check, - background_color='#424042', - tooltip = "Toggle update check on launch.", + background_color=bg_color_highlight, + tooltip = lang._instance.get_string("general.toolTip"), ), ], - - [ - sg.Text("OSC Settings:", background_color='#242224'), + [ + sg.Text(f'{lang._instance.get_string("general.oscSettings")}:', background_color=bg_color_clear), ], [ - sg.Text("Location Prefix:", background_color='#424042'), + sg.Text(f'{lang._instance.get_string('general.locationPrefix')}:', background_color=bg_color_highlight), sg.InputText( self.config.gui_osc_location, key=self.gui_osc_location, size=(30), - tooltip="Prefix for OSC address.", + tooltip=f'{lang._instance.get_string('general.locationTooltip')}.', ), ], [ - sg.Text("Address:", background_color='#424042'), + sg.Text(f'{lang._instance.get_string('general.address')}:', background_color=bg_color_highlight), sg.InputText( self.config.gui_osc_address, key=self.gui_osc_address, size=(0,20), - tooltip = "IP address we send OSC data to.", + tooltip = f'{lang._instance.get_string('general.addressTooltip')}.', ), # ], # [ - sg.Text("Port:", background_color='#424042'), + sg.Text(f'{lang._instance.get_string('general.port')}:', background_color=bg_color_highlight), sg.InputText( self.config.gui_osc_port, key=self.gui_osc_port, size=(0,10), - tooltip = "OSC port we send data to.", + tooltip = f'{lang._instance.get_string('general.portTooltip')}.', ), ], [ - sg.Text("Receive functions", background_color='#424042'), + sg.Text(f'{lang._instance.get_string('general.receiver')}', background_color=bg_color_highlight), sg.Checkbox( "", default=self.config.gui_ROSC, key=self.gui_ROSC, - background_color='#424042', + background_color=bg_color_highlight, size=(0,10), - tooltip = "Toggle OSC receive functions.", + tooltip = f'{lang._instance.get_string('general.receiverTooltip')}.', ), ], [ - sg.Text("Receiver Port:", background_color='#424042'), + sg.Text(f'{lang._instance.get_string('general.receiver')}:', background_color=bg_color_highlight), sg.InputText( self.config.gui_osc_receiver_port, key=self.gui_osc_receiver_port, size=(0,10), - tooltip = "Port we receive OSC data from (used to recalibrate from within VRChat.", + tooltip = f'{lang._instance.get_string('general.receiverPortTooltip')}:', ), ], [ - sg.Text("Recalibrate Address:", background_color='#424042'), + sg.Text(f'{lang._instance.get_string('general.recalibrate')}:', background_color=bg_color_highlight), sg.InputText( self.config.gui_osc_recalibrate_address, key=self.gui_osc_recalibrate_address, size=(0,10), - tooltip = "OSC address we use for recalibrating.", + tooltip = f'{lang._instance.get_string('general.recalibrateTooltip')}.', ), ], [ - sg.Text("UVC Camera Settings:", background_color='#242224'), + sg.Text(f'{lang._instance.get_string('general.uvcCameraSettings')}:', background_color=bg_color_clear), ], [ - sg.Text("Use Red Channel", background_color='#424042'), + sg.Text(f'{lang._instance.get_string('general.useRedChannel')}:', background_color=bg_color_highlight), sg.Checkbox( "", default=self.config.gui_use_red_channel, key=self.gui_use_red_channel, - background_color='#424042', + background_color=bg_color_highlight, size=(0,10), - tooltip = "Uses only the red channel for Omnicept capture.", + tooltip = f'{lang._instance.get_string('general.useRedChannelTooltip')}.', ), - sg.Text("X Resolution:", background_color='#424042'), + sg.Text(f'{lang._instance.get_string('general.xResolution')}.', background_color=bg_color_highlight), sg.InputText( self.config.gui_cam_resolution_x, key=self.gui_cam_resolution_x, size=(0,20), - tooltip = "X capture resolution. Default = 0", + tooltip = f'{lang._instance.get_string('general.xResolutionTooltip')}', ), # ], # [ - sg.Text("Y Resolution:", background_color='#424042'), + sg.Text(f'{lang._instance.get_string('general.yResolution')}.', background_color=bg_color_highlight), sg.InputText( self.config.gui_cam_resolution_y, key=self.gui_cam_resolution_y, size=(0,10), - tooltip = "Y capture resolution. Default = 0", + tooltip = f'{lang._instance.get_string('general.yResolutionTooltip')}', ), # ], # [ - sg.Text("Framerate:", background_color='#424042'), + sg.Text(f'{lang._instance.get_string('general.framerate')}:', background_color=bg_color_highlight), sg.InputText( self.config.gui_cam_framerate, key=self.gui_cam_framerate, size=(0,10), - tooltip = "Capture framerate. Default = 0", + tooltip = f'{lang._instance.get_string('general.framerateTooltip')}', ), ], + [ + sg.Text(f'{lang._instance.get_string('general.language')}"', background_color=bg_color_clear), + ], + [ + sg.Text(f'{lang._instance.get_string('general.languageInstructions')}.', background_color=bg_color_highlight), + ], + [ + sg.OptionMenu( + self.available_languages, + default_value=self.config.gui_language, + key=self.gui_language, + tooltip=f'{lang._instance.get_string('general.languageTooltip')}.', + ), + ] ] self.widget_layout = [ [ - sg.Text("General Settings:", background_color='#242224'), + sg.Text(f'{lang._instance.get_string('general.header')}:', background_color=bg_color_clear), ], [ - sg.Column(self.general_settings_layout, key=self.gui_general_settings_layout, background_color='#424042' ), + sg.Column(self.general_settings_layout, key=self.gui_general_settings_layout, background_color=bg_color_highlight ), ], ] @@ -185,11 +201,11 @@ class SettingsWidget: self.config.gui_osc_port = int(values[self.gui_osc_port]) changed = True else: - print("\033[91m[ERROR] OSC port value must be an integer 0-65535\033[0m") + print(f"\033[91m[ERROR] {lang._instance.get_string('general.oscPort')}\033[0m") except: - print("\033[91m[ERROR] OSC port value must be an integer 0-65535\033[0m") + print(f"\033[91m[ERROR] {lang._instance.get_string('general.oscPort')}\033[0m") except: - print("\033[91m[ERROR] OSC port value cannot be empty and must be an integer 0-65535\033[0m") + print(f"\033[91m[ERROR] {lang._instance.get_string('general.oscValue')}\033[0m") try: if self.config.gui_osc_receiver_port != int(values[self.gui_osc_receiver_port]): @@ -199,11 +215,11 @@ class SettingsWidget: self.config.gui_osc_receiver_port = int(values[self.gui_osc_receiver_port]) changed = True else: - print("\033[91m[ERROR] OSC receive port value must be an integer 0-65535\033[0m") + print(f"\033[91m[ERROR] {lang._instance.get_string('general.oscPort')}\033[0m") except: - print("\033[91m[ERROR] OSC receive port value must be an integer 0-65535\033[0m") + print(f"\033[91m[ERROR] {lang._instance.get_string('general.oscPort')}\033[0m") except: - print("\033[91m[ERROR] OSC receive port value cannot be empty and must be an integer 0-65535\033[0m") + print(f"\033[91m[ERROR] {lang._instance.get_string('general.oscValue')}\033[0m") if self.config.gui_osc_address != values[self.gui_osc_address]: self.config.gui_osc_address = values[self.gui_osc_address] @@ -224,28 +240,33 @@ class SettingsWidget: if self.config.gui_osc_location != values[self.gui_osc_location]: self.config.gui_osc_location = values[self.gui_osc_location] changed = True + if values[self.gui_cam_resolution_x] != '': if str(self.config.gui_cam_resolution_x) != values[self.gui_cam_resolution_x]: try: self.config.gui_cam_resolution_x = int(values[self.gui_cam_resolution_x]) changed = True - except: print("Not an Int") + except: print(lang._instance.get_string('general.notAnInt')) if values[self.gui_cam_resolution_y] != '': if str(self.config.gui_cam_resolution_y) != values[self.gui_cam_resolution_y]: try: self.config.gui_cam_resolution_y = int(values[self.gui_cam_resolution_y]) changed = True - except: print("Not an Int") + except: print(lang._instance.get_string('general.notAnInt')) if values[self.gui_cam_framerate] != '': if str(self.config.gui_cam_framerate) != values[self.gui_cam_framerate]: try: self.config.gui_cam_framerate = int(values[self.gui_cam_framerate]) changed = True - except: print("Not an Int") + except: print(lang._instance.get_string('general.notAnInt')) if self.config.gui_use_red_channel != bool(values[self.gui_use_red_channel]): self.config.gui_use_red_channel = bool(values[self.gui_use_red_channel]) changed = True + + if self.config.gui_language != values[self.gui_language]: + self.config.gui_language = values[self.gui_language] + changed = True if changed: self.main_config.save() diff --git a/BabbleApp/lang_manager.py b/BabbleApp/lang_manager.py new file mode 100644 index 0000000..4d35555 --- /dev/null +++ b/BabbleApp/lang_manager.py @@ -0,0 +1,54 @@ +import os +import json + +class LocaleStringManager: + _instance = None + _languages = [] + _strings = {} + _current_language = None + + def __new__(self, locale_directory, language): + if self._instance is None: + self._instance = super(LocaleStringManager, self).__new__(self) + self._instance._initialize(locale_directory, language) + return self._instance + + def _initialize(self, locale_directory, language): + self._instance._languages = [lang for lang in os.listdir(locale_directory) if os.path.isdir(os.path.join(locale_directory, lang))] + self._instance._current_language = language + + for lang in self._languages: + self._instance._strings[lang] = {} + lang_dir = os.path.join(locale_directory, lang) + for file in os.listdir(lang_dir): + if file.endswith('.json'): + with open(os.path.join(lang_dir, file), 'r', encoding='utf-8') as f: + file_strings = json.load(f) + file_name = os.path.splitext(file)[0] + for key, value in file_strings.items(): + self._instance._strings[lang][f"{file_name}.{key}"] = value + + self._load_language(language) + + def _load_language(self, language): + self._instance._current_language = language + if self._instance._current_language not in self._instance._languages: + raise ValueError(f"Language '{self._instance._current_language}' from settings is not supported") + + @classmethod + def get_languages(self): + return self._instance._languages + + @classmethod + def get_string(self, pattern): + pattern = f'locale.{pattern}' + if pattern not in self._instance._strings[self._instance._current_language]: + raise KeyError(f"String pattern '{pattern}' not found for current language '{self._instance._current_language}'") + return self._instance._strings[self._instance._current_language][pattern] + + @classmethod + def update_language(self, config): + self._instance._load_language(config) +# +# # If settings are updated: +# LocaleStringManager.update_language(main_config.settings) diff --git a/BabbleApp/utils/misc_utils.py b/BabbleApp/utils/misc_utils.py index a69823b..7c384bb 100644 --- a/BabbleApp/utils/misc_utils.py +++ b/BabbleApp/utils/misc_utils.py @@ -166,6 +166,11 @@ def get_camera_index_by_name(name): def PlaySound(*args, **kwargs): pass +# Handle debugging virtual envs. +def EnsurePath(): + if os.path.exists(os.path.join(os.getcwd(), "BabbleApp")): + os.chdir(os.path.join(os.getcwd(), "BabbleApp")) + SND_FILENAME = SND_ASYNC = 1 diff --git a/extract_strings.py b/extract_strings.py new file mode 100644 index 0000000..6625c80 --- /dev/null +++ b/extract_strings.py @@ -0,0 +1,54 @@ +import os +import ast +import json +import argparse + +def extract_strings(node): + strings = [] + for child in ast.iter_child_nodes(node): + if isinstance(child, ast.Str): + strings.append(child.s) + elif isinstance(child, ast.Constant) and isinstance(child.value, str): + strings.append(child.value) + strings.extend(extract_strings(child)) + return strings + +def process_file(file_path): + with open(file_path, 'r', encoding='utf-8') as file: + try: + tree = ast.parse(file.read()) + return extract_strings(tree) + except SyntaxError: + print(f"Syntax error in file: {file_path}") + return [] + +def process_directory(directory): + all_strings = {} + for root, _, files in os.walk(directory): + for file in files: + if file.endswith('.py'): + file_path = os.path.join(root, file) + relative_path = os.path.relpath(file_path, directory) + if relative_path.startswith('build') or relative_path.startswith('dist'): + continue + strings = process_file(file_path) + if strings: + all_strings[relative_path] = strings + return all_strings + +def save_to_json(strings, output_file): + with open(output_file, 'w', encoding='utf-8') as f: + json.dump(strings, f, ensure_ascii=False, indent=2) + +def main(): + parser = argparse.ArgumentParser(description="Extract strings from Python files in a directory") + parser.add_argument("directory", help="Directory to scan for Python files") + parser.add_argument("-o", "--output", default="extracted_strings.json", help="Output JSON file name") + args = parser.parse_args() + + all_strings = process_directory(args.directory) + save_to_json(all_strings, args.output) + print(f"Extracted strings have been saved to {args.output}") + +if __name__ == "__main__": + main() \ No newline at end of file