Start of Translations

- This is the start of the translations system, does not yet work as I can't seem to get it to swap languages, I too tired today to fix it but it's a start.
- It also adds properties registration as well.
This commit is contained in:
Yusarina
2024-06-19 02:27:07 +01:00
parent 96d03075c0
commit 01f8363e07
8 changed files with 140 additions and 19 deletions
+11 -14
View File
@@ -1,5 +1,3 @@
if "bpy" not in locals(): if "bpy" not in locals():
import bpy import bpy
from . import ui from . import ui
@@ -7,28 +5,27 @@ if "bpy" not in locals():
from . import functions from . import functions
from .core import register from .core import register
from .core.register import __bl_ordered_classes from .core.register import __bl_ordered_classes
from .core import properties
else: else:
import importlib import importlib
importlib.reload(ui) importlib.reload(ui)
importlib.reload(core) importlib.reload(core)
importlib.reload(functions) importlib.reload(functions)
importlib.reload(properties)
def register(): def register():
print("Registering Avatar Toolkit") print("Registering Avatar Toolkit")
# Order the classes before registration properties.register()
core.register.order_classes() core.register.order_classes()
# Register the UI classes core.register.register_properties()
for cls in core.register.__bl_ordered_classes:
# Iterate over the classes to register and register them print("registering " + str(cls))
for cls in __bl_ordered_classes:
print("registering" + str(cls))
bpy.utils.register_class(cls) bpy.utils.register_class(cls)
def unregister(): def unregister():
print("Unregistering Avatar Toolkit") print("Unregistering Avatar Toolkit")
# Unregister the UI classes for cls in reversed(core.register.__bl_ordered_classes):
# Iterate over the classes to unregister in reverse order and unregister them
for cls in reversed(list(__bl_ordered_classes)):
bpy.utils.unregister_class(cls) bpy.utils.unregister_class(cls)
print("unregistering" + str(cls)) print("unregistering " + str(cls))
core.register.unregister_properties()
properties.unregister()
+15
View File
@@ -0,0 +1,15 @@
import bpy
from ..functions.translations import t, get_languages_list, update_ui
from ..core.register import register_property
from typing import Tuple
def register() -> None:
register_property((bpy.types.Scene, "language", bpy.props.EnumProperty(
name=t("Settings.language.label"),
description=t("Settings.language.desc"),
items=get_languages_list,
update=update_ui
)))
def unregister() -> None:
pass
+13
View File
@@ -5,6 +5,8 @@ import typing
__bl_classes = [] __bl_classes = []
# List to store the ordered classes for registration # List to store the ordered classes for registration
__bl_ordered_classes = [] __bl_ordered_classes = []
# List to store props to register
__bl_props = []
def register_wrap(cls): def register_wrap(cls):
# Check if the class has a 'bl_rna' attribute (indicating it's a Blender class) # Check if the class has a 'bl_rna' attribute (indicating it's a Blender class)
@@ -13,6 +15,17 @@ def register_wrap(cls):
__bl_classes.append(cls) __bl_classes.append(cls)
return cls return cls
# Register all properties
def register_property(prop):
__bl_props.append(prop)
def register_properties():
for prop in __bl_props:
setattr(prop[0], prop[1], prop[2])
def unregister_properties():
for prop in reversed(__bl_props):
delattr(prop[0], prop[1])
#- @989onan had to add this from Cats. This is extremely important else you will be screamed at by register order issues! #- @989onan had to add this from Cats. This is extremely important else you will be screamed at by register order issues!
# Find order to register to solve dependencies # Find order to register to solve dependencies
+64
View File
@@ -0,0 +1,64 @@
import os
import json
import bpy
from bpy.app.translations import locale
from ..core.register import register_wrap
from typing import Dict, List, Tuple
main_dir: str = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
resources_dir: str = os.path.join(main_dir, "resources")
translations_dir: str = os.path.join(resources_dir, "translations")
dictionary: Dict[str, str] = dict()
languages: List[str] = []
verbose: bool = True
def load_translations() -> None:
global dictionary, languages
dictionary = dict()
languages = ["auto"]
language: str = bpy.context.preferences.view.language
for i in os.listdir(translations_dir):
languages.append(i.split(".")[0])
translation_file: str = os.path.join(translations_dir, language + ".json")
if os.path.exists(translation_file):
with open(translation_file, 'r', encoding='utf-8') as file:
dictionary = json.load(file)["messages"]
else:
custom_language: str = language.split("_")[0]
custom_translation_file: str = os.path.join(translations_dir, custom_language + ".json")
if os.path.exists(custom_translation_file):
with open(custom_translation_file, 'r', encoding='utf-8') as file:
dictionary = json.load(file)["messages"]
else:
print(f"Translation file not found for language: {language}")
default_file: str = os.path.join(translations_dir, "en_US.json")
if os.path.exists(default_file):
with open(default_file, 'r', encoding='utf-8') as file:
dictionary = json.load(file)["messages"]
else:
print("Default translation file 'en_US.json' not found.")
def t(phrase: str, *args, **kwargs) -> str:
output: str = dictionary.get(phrase)
if output is None:
if verbose:
print('Warning: Unknown phrase: ' + phrase)
return phrase
return output.format(*args, **kwargs)
def get_languages_list(self, context) -> List[Tuple[str, str, str]]:
choices: List[Tuple[str, str, str]] = []
for language in languages:
choices.append((language, language, language))
return choices
def update_ui(self, context) -> None:
load_translations()
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
load_translations()
+9
View File
@@ -0,0 +1,9 @@
{
"messages": {
"Settings.label": "Settings",
"Settings.language.label": "Language",
"Settings.language.desc": "Select the language for the addon's UI"
}
}
+8
View File
@@ -0,0 +1,8 @@
{
"messages": {
"Settings.label": "Settings Ja Test",
"Settings.language.label": "Language Ja Test",
"Settings.language.desc": "Select the language for the addon's UI Ja Test"
}
}
-4
View File
@@ -13,7 +13,3 @@ else:
for module_name in [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]: for module_name in [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]:
print("reloading " +module_name) print("reloading " +module_name)
exec("importlib.reload("+module_name+")") exec("importlib.reload("+module_name+")")
+19
View File
@@ -0,0 +1,19 @@
import bpy
from ..core.register import register_wrap
from .panel import AvatarToolkitPanel
from ..functions.translations import t
@register_wrap
class AvatarToolkitSettingsPanel(bpy.types.Panel):
bl_label = t("Settings.label")
bl_idname = "OBJECT_PT_avatar_toolkit_settings"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "Avatar Toolkit"
bl_parent_id = "OBJECT_PT_avatar_toolkit"
def draw(self, context):
layout = self.layout
props = context.scene
layout.prop(props, "language")