Simplify Translated

Remove the TranslatedWithContext and TranslatedWithoutContext rules and
just use Translated.
This commit is contained in:
James Westman 2023-04-29 21:48:02 -05:00
parent 779e27d7ac
commit 3c1941a17e
3 changed files with 22 additions and 43 deletions

View file

@ -39,8 +39,6 @@ from .values import (
QuotedLiteral, QuotedLiteral,
StringValue, StringValue,
Translated, Translated,
TranslatedWithContext,
TranslatedWithoutContext,
TypeLiteral, TypeLiteral,
Value, Value,
) )

View file

@ -27,40 +27,27 @@ from .gobject_object import Object
from .contexts import ScopeCtx, ValueTypeCtx from .contexts import ScopeCtx, ValueTypeCtx
class TranslatedWithoutContext(AstNode):
grammar = ["_", "(", UseQuoted("string"), Optional(","), ")"]
@property
def string(self) -> str:
return self.tokens["string"]
class TranslatedWithContext(AstNode):
grammar = [
"C_",
"(",
UseQuoted("context"),
",",
UseQuoted("string"),
Optional(","),
")",
]
@property
def string(self) -> str:
return self.tokens["string"]
@property
def translate_context(self) -> str:
return self.tokens["context"]
class Translated(AstNode): class Translated(AstNode):
grammar = AnyOf(TranslatedWithoutContext, TranslatedWithContext) grammar = AnyOf(
["_", "(", UseQuoted("string"), Optional(","), ")"],
[
"C_",
"(",
UseQuoted("context"),
",",
UseQuoted("string"),
Optional(","),
")",
],
)
@property @property
def child(self) -> T.Union[TranslatedWithContext, TranslatedWithoutContext]: def string(self) -> str:
return self.children[0] return self.tokens["string"]
@property
def translate_context(self) -> T.Optional[str]:
return self.tokens["context"]
@validate() @validate()
def validate_for_type(self) -> None: def validate_for_type(self) -> None:
@ -382,7 +369,7 @@ class StringValue(AstNode):
@property @property
def string(self) -> str: def string(self) -> str:
if isinstance(self.child, Translated): if isinstance(self.child, Translated):
return self.child.child.string return self.child.string
else: else:
return self.child.value return self.child.value

View file

@ -103,7 +103,7 @@ class XmlOutput(OutputFormat):
xml.start_tag( xml.start_tag(
"property", **props, **self._translated_string_attrs(child) "property", **props, **self._translated_string_attrs(child)
) )
xml.put_text(child.child.string) xml.put_text(child.string)
xml.end_tag() xml.end_tag()
else: else:
xml.start_tag("property", **props) xml.start_tag("property", **props)
@ -149,12 +149,7 @@ class XmlOutput(OutputFormat):
if isinstance(translated, QuotedLiteral): if isinstance(translated, QuotedLiteral):
return {} return {}
else: else:
return { return {"translatable": "true", "context": translated.translate_context}
"translatable": "true",
"context": translated.child.translate_context
if isinstance(translated.child, TranslatedWithContext)
else None,
}
def _emit_signal(self, signal: Signal, xml: XmlEmitter): def _emit_signal(self, signal: Signal, xml: XmlEmitter):
name = signal.name name = signal.name
@ -267,7 +262,7 @@ class XmlOutput(OutputFormat):
if isinstance(value.child, Translated): if isinstance(value.child, Translated):
xml.start_tag(tag, **attrs, **self._translated_string_attrs(value.child)) xml.start_tag(tag, **attrs, **self._translated_string_attrs(value.child))
xml.put_text(value.child.child.string) xml.put_text(value.child.string)
xml.end_tag() xml.end_tag()
elif isinstance(value.child, QuotedLiteral): elif isinstance(value.child, QuotedLiteral):
xml.start_tag(tag, **attrs) xml.start_tag(tag, **attrs)
@ -308,7 +303,6 @@ class XmlOutput(OutputFormat):
elif isinstance(extension, Responses): elif isinstance(extension, Responses):
xml.start_tag("responses") xml.start_tag("responses")
for response in extension.responses: for response in extension.responses:
# todo: translated
xml.start_tag( xml.start_tag(
"response", "response",
id=response.id, id=response.id,