From e261180dccf3468d5d343b9385678b3cca9fa557 Mon Sep 17 00:00:00 2001 From: James Westman Date: Tue, 12 Dec 2023 19:31:07 -0600 Subject: [PATCH] language: Add translation-domain This allows you to set the translation domain of a blueprint file. --- .../language/translation_domain.py | 31 +++++++++++++++++++ blueprintcompiler/language/ui.py | 10 ++++++ blueprintcompiler/outputs/xml/__init__.py | 5 ++- docs/reference/document_root.rst | 16 +++++++++- tests/samples/translation_domain.blp | 3 ++ tests/samples/translation_domain.ui | 9 ++++++ 6 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 blueprintcompiler/language/translation_domain.py create mode 100644 tests/samples/translation_domain.blp create mode 100644 tests/samples/translation_domain.ui diff --git a/blueprintcompiler/language/translation_domain.py b/blueprintcompiler/language/translation_domain.py new file mode 100644 index 0000000..ff20ead --- /dev/null +++ b/blueprintcompiler/language/translation_domain.py @@ -0,0 +1,31 @@ +# translation_domain.py +# +# Copyright 2022 James Westman +# +# This file is free software; you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as +# published by the Free Software Foundation; either version 3 of the +# License, or (at your option) any later version. +# +# This file is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program. If not, see . +# +# SPDX-License-Identifier: LGPL-3.0-or-later + +from .common import * + + +class TranslationDomain(AstNode): + grammar = Statement( + "translation-domain", + UseQuoted("domain"), + ) + + @property + def domain(self): + return self.tokens["domain"] diff --git a/blueprintcompiler/language/ui.py b/blueprintcompiler/language/ui.py index 34ba193..8aaf359 100644 --- a/blueprintcompiler/language/ui.py +++ b/blueprintcompiler/language/ui.py @@ -26,6 +26,7 @@ from .gobject_object import Object from .gtk_menu import Menu, menu from .gtkbuilder_template import Template from .imports import GtkDirective, Import +from .translation_domain import TranslationDomain class UI(AstNode): @@ -34,6 +35,7 @@ class UI(AstNode): grammar = [ GtkDirective, ZeroOrMore(Import), + Optional(TranslationDomain), Until( AnyOf( Template, @@ -75,6 +77,14 @@ class UI(AstNode): def gtk_decl(self) -> GtkDirective: return self.children[GtkDirective][0] + @property + def translation_domain(self) -> T.Optional[TranslationDomain]: + domains = self.children[TranslationDomain] + if len(domains): + return domains[0] + else: + return None + @property def contents(self) -> T.List[T.Union[Object, Template, Menu]]: return [ diff --git a/blueprintcompiler/outputs/xml/__init__.py b/blueprintcompiler/outputs/xml/__init__.py index 3eaf7b8..b84c5cf 100644 --- a/blueprintcompiler/outputs/xml/__init__.py +++ b/blueprintcompiler/outputs/xml/__init__.py @@ -12,7 +12,10 @@ class XmlOutput(OutputFormat): return xml.result def _emit_ui(self, ui: UI, xml: XmlEmitter): - xml.start_tag("interface") + if domain := ui.translation_domain: + xml.start_tag("interface", domain=domain.domain) + else: + xml.start_tag("interface") self._emit_gtk_directive(ui.gtk_decl, xml) diff --git a/docs/reference/document_root.rst b/docs/reference/document_root.rst index 13dea58..eaf178d 100644 --- a/docs/reference/document_root.rst +++ b/docs/reference/document_root.rst @@ -10,7 +10,7 @@ Document Root .. rst-class:: grammar-block - Root = :ref:`GtkDecl` (:ref:`Using`)* ( :ref:`Template` | :ref:`Menu` | :ref:`Object` )* EOF + Root = :ref:`GtkDecl` (:ref:`Using`)* (:ref:`TranslationDomain`)? ( :ref:`Template` | :ref:`Menu` | :ref:`Object` )* EOF A blueprint document consists of a :ref:`GTK declaration`, one sor more :ref:`imports`, and a list of :ref:`objects` and/or a :ref:`template`. @@ -72,3 +72,17 @@ Example // Import libadwaita using Adw 1; + + +.. _Syntax TranslationDomain: + +Translation Domain +------------------ + +.. rst-class:: grammar-block + + TranslationDomain = 'translation-domain' `> ';' + +The translation domain is used to look up translations for translatable strings in the blueprint file. If no translation domain is specified, strings will be looked up in the program's global domain. + +See `Gtk.Builder:translation-domain `_ for more information. diff --git a/tests/samples/translation_domain.blp b/tests/samples/translation_domain.blp new file mode 100644 index 0000000..98d160d --- /dev/null +++ b/tests/samples/translation_domain.blp @@ -0,0 +1,3 @@ +using Gtk 4.0; + +translation-domain "blueprint-tests"; diff --git a/tests/samples/translation_domain.ui b/tests/samples/translation_domain.ui new file mode 100644 index 0000000..d583f23 --- /dev/null +++ b/tests/samples/translation_domain.ui @@ -0,0 +1,9 @@ + + + + +