mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
Support translation contexts
This commit is contained in:
parent
b0a8f3e2f5
commit
0e33ce190d
6 changed files with 55 additions and 31 deletions
|
@ -91,7 +91,7 @@ Translations
|
||||||
~~~~~~~~~~~~
|
~~~~~~~~~~~~
|
||||||
|
|
||||||
Use ``_("...")`` to mark strings as translatable. You can put a comment for
|
Use ``_("...")`` to mark strings as translatable. You can put a comment for
|
||||||
translators on the line above.
|
translators on the line above if needed.
|
||||||
|
|
||||||
.. code-block::
|
.. code-block::
|
||||||
|
|
||||||
|
@ -100,6 +100,18 @@ translators on the line above.
|
||||||
label: _("Hello, world!");
|
label: _("Hello, world!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Use ``C_("context", "...")`` to add a *message context* to a string to
|
||||||
|
disambiguate it, in case the same string appears in different places. Remember,
|
||||||
|
two strings might be the same in one language but different in another depending
|
||||||
|
on context.
|
||||||
|
|
||||||
|
.. code-block::
|
||||||
|
|
||||||
|
Gtk.Label label {
|
||||||
|
/* Translators: This is a section in the preferences window */
|
||||||
|
label: C_("preferences window", "Hello, world!");
|
||||||
|
}
|
||||||
|
|
||||||
Referencing objects by ID
|
Referencing objects by ID
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
|
|
@ -275,7 +275,6 @@ class Property(AstNode):
|
||||||
def emit_xml(self, xml: XmlEmitter):
|
def emit_xml(self, xml: XmlEmitter):
|
||||||
values = self.children[Value]
|
values = self.children[Value]
|
||||||
value = values[0] if len(values) == 1 else None
|
value = values[0] if len(values) == 1 else None
|
||||||
translatable = isinstance(value, TranslatedStringValue)
|
|
||||||
|
|
||||||
bind_flags = []
|
bind_flags = []
|
||||||
if self.tokens["sync_create"]:
|
if self.tokens["sync_create"]:
|
||||||
|
@ -286,12 +285,14 @@ class Property(AstNode):
|
||||||
|
|
||||||
props = {
|
props = {
|
||||||
"name": self.tokens["name"],
|
"name": self.tokens["name"],
|
||||||
"translatable": "true" if translatable else None,
|
|
||||||
"bind-source": self.tokens["bind_source"],
|
"bind-source": self.tokens["bind_source"],
|
||||||
"bind-property": self.tokens["bind_property"],
|
"bind-property": self.tokens["bind_property"],
|
||||||
"bind-flags": bind_flags_str,
|
"bind-flags": bind_flags_str,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if isinstance(value, TranslatedStringValue):
|
||||||
|
props = { **props, **value.attrs }
|
||||||
|
|
||||||
if len(self.children[Object]) == 1:
|
if len(self.children[Object]) == 1:
|
||||||
xml.start_tag("property", **props)
|
xml.start_tag("property", **props)
|
||||||
self.children[Object][0].emit_xml(xml)
|
self.children[Object][0].emit_xml(xml)
|
||||||
|
@ -300,10 +301,7 @@ class Property(AstNode):
|
||||||
xml.put_self_closing("property", **props)
|
xml.put_self_closing("property", **props)
|
||||||
else:
|
else:
|
||||||
xml.start_tag("property", **props)
|
xml.start_tag("property", **props)
|
||||||
if translatable:
|
value.emit_xml(xml)
|
||||||
xml.put_text(value.string)
|
|
||||||
else:
|
|
||||||
value.emit_xml(xml)
|
|
||||||
xml.end_tag()
|
xml.end_tag()
|
||||||
|
|
||||||
|
|
||||||
|
@ -357,11 +355,14 @@ class Value(ast.AstNode):
|
||||||
|
|
||||||
class TranslatedStringValue(Value):
|
class TranslatedStringValue(Value):
|
||||||
@property
|
@property
|
||||||
def string(self):
|
def attrs(self):
|
||||||
return self.tokens["value"]
|
attrs = { "translatable": "true" }
|
||||||
|
if "context" in self.tokens:
|
||||||
|
attrs["context"] = self.tokens["context"]
|
||||||
|
return attrs
|
||||||
|
|
||||||
def emit_xml(self, xml):
|
def emit_xml(self, xml: XmlEmitter):
|
||||||
raise CompilerBugError("TranslatedStringValues must be handled by the parent AST node")
|
xml.put_text(self.tokens["value"])
|
||||||
|
|
||||||
|
|
||||||
class LiteralValue(Value):
|
class LiteralValue(Value):
|
||||||
|
@ -470,16 +471,13 @@ class BaseAttribute(AstNode):
|
||||||
|
|
||||||
def emit_xml(self, xml: XmlEmitter):
|
def emit_xml(self, xml: XmlEmitter):
|
||||||
value = self.children[Value][0]
|
value = self.children[Value][0]
|
||||||
translatable = isinstance(value, TranslatedStringValue)
|
attrs = { self.attr_name: self.tokens["name"] }
|
||||||
attrs = {
|
|
||||||
self.attr_name: self.tokens["name"],
|
if isinstance(value, TranslatedStringValue):
|
||||||
"translatable": "true" if translatable else None
|
attrs = { **attrs, **value.attrs }
|
||||||
}
|
|
||||||
xml.start_tag(self.tag_name, **attrs)
|
xml.start_tag(self.tag_name, **attrs)
|
||||||
if translatable:
|
value.emit_xml(xml)
|
||||||
xml.put_text(value.string)
|
|
||||||
else:
|
|
||||||
value.emit_xml(xml)
|
|
||||||
xml.end_tag()
|
xml.end_tag()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -48,12 +48,9 @@ class Item(AstNode):
|
||||||
|
|
||||||
def emit_xml(self, xml: XmlEmitter):
|
def emit_xml(self, xml: XmlEmitter):
|
||||||
value = self.children[Value][0]
|
value = self.children[Value][0]
|
||||||
translatable = isinstance(value, TranslatedStringValue)
|
attrs = value.attrs if isinstance(value, TranslatedStringValue) else {}
|
||||||
xml.start_tag("item", translatable="true" if translatable else None)
|
xml.start_tag("item", **attrs)
|
||||||
if translatable:
|
value.emit_xml(xml)
|
||||||
xml.put_text(value.string)
|
|
||||||
else:
|
|
||||||
value.emit_xml(xml)
|
|
||||||
xml.end_tag()
|
xml.end_tag()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -60,11 +60,22 @@ flags_value = Group(
|
||||||
|
|
||||||
translated_string = Group(
|
translated_string = Group(
|
||||||
ast.TranslatedStringValue,
|
ast.TranslatedStringValue,
|
||||||
Sequence(
|
AnyOf(
|
||||||
Keyword("_"),
|
Sequence(
|
||||||
OpenParen(),
|
Keyword("_"),
|
||||||
UseQuoted("value").expected("a quoted string"),
|
OpenParen(),
|
||||||
CloseParen().expected("`)`"),
|
UseQuoted("value").expected("a quoted string"),
|
||||||
|
CloseParen().expected("`)`"),
|
||||||
|
),
|
||||||
|
Sequence(
|
||||||
|
Keyword("C_"),
|
||||||
|
OpenParen(),
|
||||||
|
UseQuoted("context").expected("a quoted string"),
|
||||||
|
Comma(),
|
||||||
|
UseQuoted("value").expected("a quoted string"),
|
||||||
|
Optional(Comma()),
|
||||||
|
CloseParen().expected("`)`"),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -3,3 +3,6 @@ using Gtk 4.0;
|
||||||
Label {
|
Label {
|
||||||
label: _("Hello, world!");
|
label: _("Hello, world!");
|
||||||
}
|
}
|
||||||
|
Label {
|
||||||
|
label: C_("translation context", "Hello");
|
||||||
|
}
|
||||||
|
|
|
@ -4,4 +4,7 @@
|
||||||
<object class="GtkLabel">
|
<object class="GtkLabel">
|
||||||
<property name="label" translatable="true">Hello, world!</property>
|
<property name="label" translatable="true">Hello, world!</property>
|
||||||
</object>
|
</object>
|
||||||
|
<object class="GtkLabel">
|
||||||
|
<property name="label" translatable="true" context="translation context">Hello</property>
|
||||||
|
</object>
|
||||||
</interface>
|
</interface>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue