-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gettext translation within component inline templates is not working #63
Comments
The only solution ATM is to use Overriding / patching Django's behaviour here seems to be a bit overwhelming. Evtl. tetra could add a But that makes me shiver a bit if I think about it... |
@samwillis So you have any idea how to get into this? Just need a hint here. |
Hey @nerdoc it looks like Django explicitly doesn't use the "django" domain for .py files, which is understandable as it expecting normal gettext behaviour in a .py: I would look at overriding the makemessages command like tetra does for startserver with an option to extract messages from templates in .py files. https://docs.djangoproject.com/en/5.0/topics/i18n/translation/#customizing-the-makemessages-command |
That's a good catch. I'll look into that, thanks! |
Tetra could overwrite the def is_templatized(self):
if self.domain == "djangojs":
return self.command.gettext_version < (0, 18, 3)
elif self.domain == "django":
file_ext = os.path.splitext(self.translatable.file)[1]
if file_ext == ".py":
with open(self.translatable.file, "r") as file:
content = file.read()
try:
tree = ast.parse(content)
for node in ast.walk(tree):
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id == "Component":
return True
except SyntaxError:
return False
return file_ext != ".py"
return False This would be horribly inefficient and slow when firing makemessages, but could be a starting point. |
this is a good plan. You can probably infer if there might be a component by checking if the files text includes the correct import statements, and only parse it if so. That would make it significantly more efficient. |
There may be multiple ways of importing Tetra components. from tetra.components import Component # -> class Foo(Component)
from tetra import components # -> class Foo(components.Component)
from tetra.components import component as Baz # -> class Foo(Baz)
from my_app import FooComponentBase # -> class Foo(FooComponentBase) Which are all correct imports, but makes parsing more complicated... |
class BuildFile(MakeMessagesBuildFile):
def is_templatized(self):
if self.domain == "django":
file_ext = os.path.splitext(self.translatable.file)[1]
if file_ext == ".py":
with open(self.translatable.file, "r") as file:
content = file.read()
if "from tetra.components import Component" in content:
try:
tree = ast.parse(content)
for node in ast.walk(tree):
if isinstance(node, ast.ClassDef):
has_component_base = False
has_template_attr = False
for base in node.bases:
if (
isinstance(base, ast.Attribute)
and base.attr == "Component"
and isinstance(base.value, ast.Name)
and base.value.id == "tetra.components"
):
has_component_base = True
for stmt in node.body:
if isinstance(stmt, ast.Assign):
for target in stmt.targets:
if (
isinstance(target, ast.Name)
and target.id == "template"
):
has_template_attr = True
if has_component_base and has_template_attr:
return True
except SyntaxError:
return False
return file_ext != ".py"
return super().is_templatized() smashed together by ChatGPT. Leave it here as lift-off point, have no time this evening for real coding :-( |
This is a major issue and not easy to solve. This seems such a complicated task that it occurs to me that there must be something wrong. My first mental approach would be: why this complicated? Why don't we support (or even recommend) building a component in a directory instead of a single file?
Then all those problems would be solved, including caching, IDE/highlighting support etc. This is what other component frameworks do as well. But @samwillis - I think you mad this one-file approach with a clear goal in mind. The problem here is that, without huge effort, translating is not possible in a component - which is VeryBad™. If anyone has an idea, please elaborate! |
Ah, it's always the same. Countless hours of coding, reading, thinking. Then, I decide to ask in a forum, open an issue, go for help. And a few minutes later a new idea comes up, and that path solves the issue. I seem to have done it. It's (as always) easier than initially thought: The The only issue remaining is that the code line referenced in the .po file is the line the inline template starts. This could be done in another step. But at least, this is solved. |
It seems that Django's makemessages does not recognize/find a simple translated string within a component's inline template:
When using template_name and a file, it is handled correctly, as expected.
I don't know exactly where this must be handled.
InlineTemplate
?@samwillis - any ideas?
The text was updated successfully, but these errors were encountered: