Support translation contexts

This commit is contained in:
James Westman 2021-11-24 21:12:26 -06:00
parent b0a8f3e2f5
commit 0e33ce190d
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
6 changed files with 55 additions and 31 deletions

View file

@ -275,7 +275,6 @@ class Property(AstNode):
def emit_xml(self, xml: XmlEmitter):
values = self.children[Value]
value = values[0] if len(values) == 1 else None
translatable = isinstance(value, TranslatedStringValue)
bind_flags = []
if self.tokens["sync_create"]:
@ -286,12 +285,14 @@ class Property(AstNode):
props = {
"name": self.tokens["name"],
"translatable": "true" if translatable else None,
"bind-source": self.tokens["bind_source"],
"bind-property": self.tokens["bind_property"],
"bind-flags": bind_flags_str,
}
if isinstance(value, TranslatedStringValue):
props = { **props, **value.attrs }
if len(self.children[Object]) == 1:
xml.start_tag("property", **props)
self.children[Object][0].emit_xml(xml)
@ -300,10 +301,7 @@ class Property(AstNode):
xml.put_self_closing("property", **props)
else:
xml.start_tag("property", **props)
if translatable:
xml.put_text(value.string)
else:
value.emit_xml(xml)
value.emit_xml(xml)
xml.end_tag()
@ -357,11 +355,14 @@ class Value(ast.AstNode):
class TranslatedStringValue(Value):
@property
def string(self):
return self.tokens["value"]
def attrs(self):
attrs = { "translatable": "true" }
if "context" in self.tokens:
attrs["context"] = self.tokens["context"]
return attrs
def emit_xml(self, xml):
raise CompilerBugError("TranslatedStringValues must be handled by the parent AST node")
def emit_xml(self, xml: XmlEmitter):
xml.put_text(self.tokens["value"])
class LiteralValue(Value):
@ -470,16 +471,13 @@ class BaseAttribute(AstNode):
def emit_xml(self, xml: XmlEmitter):
value = self.children[Value][0]
translatable = isinstance(value, TranslatedStringValue)
attrs = {
self.attr_name: self.tokens["name"],
"translatable": "true" if translatable else None
}
attrs = { self.attr_name: self.tokens["name"] }
if isinstance(value, TranslatedStringValue):
attrs = { **attrs, **value.attrs }
xml.start_tag(self.tag_name, **attrs)
if translatable:
xml.put_text(value.string)
else:
value.emit_xml(xml)
value.emit_xml(xml)
xml.end_tag()