Skip to content

Commit

Permalink
Translation on code generated. refs #120
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandroautalan committed May 14, 2022
1 parent 8d6c032 commit d9c7656
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pygubudesigner/codegen/codebuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def __init__(self):
self.uidefinition = None
self._builder = Builder()
self._options = {}
self._with_i18n_support = False

def add_import_line(self, module_name, as_name=None, priority=0):
if module_name not in self._extra_imports:
Expand Down Expand Up @@ -405,3 +406,19 @@ def _make_identifier(self, name):
output = name.replace('.', '_')
output = ''.join(x for x in output if x.isalnum() or x == '_')
return output

def code_translate_str(self, value: str) -> str:
escaped = BuilderObject.code_escape_str(value)
if self.with_i18n_support:
trval = f'_({escaped})'
return trval
else:
return escaped

@property
def with_i18n_support(self) -> bool:
return self._with_i18n_support

@with_i18n_support.setter
def with_i18n_support(self, value: bool):
self._with_i18n_support = value
3 changes: 3 additions & 0 deletions pygubudesigner/codegen/scriptgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def on_code_generate_clicked(self):
target = self.tree.get_widget_id(tree_item)
target_class = self.tree.get_widget_class(tree_item)
class_name = self.classnamevar.get()
with_i18n_support = True

main_widget_is_toplevel = False
if target_class == 'tk.Toplevel':
Expand All @@ -105,8 +106,10 @@ def on_code_generate_clicked(self):
'tkvariables': [],
'has_ttk_styles': False,
'set_project_path': False,
'with_i18n_support': with_i18n_support,
}

generator.with_i18n_support = with_i18n_support
black_fm = black.FileMode()
if template == 'application':
generator.add_import_line('pathlib')
Expand Down
5 changes: 5 additions & 0 deletions pygubudesigner/codegen/template/app.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ PROJECT_UI = PROJECT_PATH / "${project_name}"

<%block name="class_definition" filter="trim">
class ${class_name}:
%if with_i18n_support:
def __init__(self, master=None, translator=None):
self.builder = builder = pygubu.Builder(translator)
%else:
def __init__(self, master=None):
self.builder = builder = pygubu.Builder()
%endif
builder.add_resource_path(PROJECT_PATH)
builder.add_from_file(PROJECT_UI)
self.mainwindow = builder.get_object('${main_widget}', master)
Expand Down
7 changes: 7 additions & 0 deletions pygubudesigner/codegen/template/script.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

<%block name="class_definition" filter="trim">
class ${class_name}:
%if with_i18n_support:
def __init__(self, master=None, translator=None):
_ = translator
if translator is None:
_ = lambda x: x
%else:
def __init__(self, master=None):
%endif
# build ui
${widget_code}
# Main widget
Expand Down

0 comments on commit d9c7656

Please sign in to comment.