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,
StringValue,
Translated,
TranslatedWithContext,
TranslatedWithoutContext,
TypeLiteral,
Value,
)

View file

@ -27,16 +27,10 @@ from .gobject_object import Object
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 = [
class Translated(AstNode):
grammar = AnyOf(
["_", "(", UseQuoted("string"), Optional(","), ")"],
[
"C_",
"(",
UseQuoted("context"),
@ -44,24 +38,17 @@ class TranslatedWithContext(AstNode):
UseQuoted("string"),
Optional(","),
")",
]
],
)
@property
def string(self) -> str:
return self.tokens["string"]
@property
def translate_context(self) -> str:
def translate_context(self) -> T.Optional[str]:
return self.tokens["context"]
class Translated(AstNode):
grammar = AnyOf(TranslatedWithoutContext, TranslatedWithContext)
@property
def child(self) -> T.Union[TranslatedWithContext, TranslatedWithoutContext]:
return self.children[0]
@validate()
def validate_for_type(self) -> None:
expected_type = self.context[ValueTypeCtx].value_type
@ -382,7 +369,7 @@ class StringValue(AstNode):
@property
def string(self) -> str:
if isinstance(self.child, Translated):
return self.child.child.string
return self.child.string
else:
return self.child.value

View file

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