mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
language: Add translation-domain
This allows you to set the translation domain of a blueprint file.
This commit is contained in:
parent
c5fa33363f
commit
e261180dcc
6 changed files with 72 additions and 2 deletions
31
blueprintcompiler/language/translation_domain.py
Normal file
31
blueprintcompiler/language/translation_domain.py
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
# translation_domain.py
|
||||||
|
#
|
||||||
|
# Copyright 2022 James Westman <james@jwestman.net>
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
#
|
||||||
|
# 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"]
|
|
@ -26,6 +26,7 @@ from .gobject_object import Object
|
||||||
from .gtk_menu import Menu, menu
|
from .gtk_menu import Menu, menu
|
||||||
from .gtkbuilder_template import Template
|
from .gtkbuilder_template import Template
|
||||||
from .imports import GtkDirective, Import
|
from .imports import GtkDirective, Import
|
||||||
|
from .translation_domain import TranslationDomain
|
||||||
|
|
||||||
|
|
||||||
class UI(AstNode):
|
class UI(AstNode):
|
||||||
|
@ -34,6 +35,7 @@ class UI(AstNode):
|
||||||
grammar = [
|
grammar = [
|
||||||
GtkDirective,
|
GtkDirective,
|
||||||
ZeroOrMore(Import),
|
ZeroOrMore(Import),
|
||||||
|
Optional(TranslationDomain),
|
||||||
Until(
|
Until(
|
||||||
AnyOf(
|
AnyOf(
|
||||||
Template,
|
Template,
|
||||||
|
@ -75,6 +77,14 @@ class UI(AstNode):
|
||||||
def gtk_decl(self) -> GtkDirective:
|
def gtk_decl(self) -> GtkDirective:
|
||||||
return self.children[GtkDirective][0]
|
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
|
@property
|
||||||
def contents(self) -> T.List[T.Union[Object, Template, Menu]]:
|
def contents(self) -> T.List[T.Union[Object, Template, Menu]]:
|
||||||
return [
|
return [
|
||||||
|
|
|
@ -12,6 +12,9 @@ class XmlOutput(OutputFormat):
|
||||||
return xml.result
|
return xml.result
|
||||||
|
|
||||||
def _emit_ui(self, ui: UI, xml: XmlEmitter):
|
def _emit_ui(self, ui: UI, xml: XmlEmitter):
|
||||||
|
if domain := ui.translation_domain:
|
||||||
|
xml.start_tag("interface", domain=domain.domain)
|
||||||
|
else:
|
||||||
xml.start_tag("interface")
|
xml.start_tag("interface")
|
||||||
|
|
||||||
self._emit_gtk_directive(ui.gtk_decl, xml)
|
self._emit_gtk_directive(ui.gtk_decl, xml)
|
||||||
|
|
|
@ -10,7 +10,7 @@ Document Root
|
||||||
|
|
||||||
.. rst-class:: grammar-block
|
.. rst-class:: grammar-block
|
||||||
|
|
||||||
Root = :ref:`GtkDecl<Syntax GtkDecl>` (:ref:`Using<Syntax Using>`)* ( :ref:`Template<Syntax Template>` | :ref:`Menu<Syntax Menu>` | :ref:`Object<Syntax Object>` )* EOF
|
Root = :ref:`GtkDecl<Syntax GtkDecl>` (:ref:`Using<Syntax Using>`)* (:ref:`TranslationDomain<Syntax TranslationDomain>`)? ( :ref:`Template<Syntax Template>` | :ref:`Menu<Syntax Menu>` | :ref:`Object<Syntax Object>` )* EOF
|
||||||
|
|
||||||
A blueprint document consists of a :ref:`GTK declaration<Syntax GtkDecl>`, one sor more :ref:`imports<Syntax Using>`, and a list of :ref:`objects<Syntax Object>` and/or a :ref:`template<Syntax Template>`.
|
A blueprint document consists of a :ref:`GTK declaration<Syntax GtkDecl>`, one sor more :ref:`imports<Syntax Using>`, and a list of :ref:`objects<Syntax Object>` and/or a :ref:`template<Syntax Template>`.
|
||||||
|
|
||||||
|
@ -72,3 +72,17 @@ Example
|
||||||
|
|
||||||
// Import libadwaita
|
// Import libadwaita
|
||||||
using Adw 1;
|
using Adw 1;
|
||||||
|
|
||||||
|
|
||||||
|
.. _Syntax TranslationDomain:
|
||||||
|
|
||||||
|
Translation Domain
|
||||||
|
------------------
|
||||||
|
|
||||||
|
.. rst-class:: grammar-block
|
||||||
|
|
||||||
|
TranslationDomain = 'translation-domain' <domain::ref:`QUOTED<Syntax QUOTED>`> ';'
|
||||||
|
|
||||||
|
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 <https://docs.gtk.org/gtk4/property.Builder.translation-domain.html>`_ for more information.
|
||||||
|
|
3
tests/samples/translation_domain.blp
Normal file
3
tests/samples/translation_domain.blp
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
using Gtk 4.0;
|
||||||
|
|
||||||
|
translation-domain "blueprint-tests";
|
9
tests/samples/translation_domain.ui
Normal file
9
tests/samples/translation_domain.ui
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
DO NOT EDIT!
|
||||||
|
This file was @generated by blueprint-compiler. Instead, edit the
|
||||||
|
corresponding .blp file and regenerate this file with blueprint-compiler.
|
||||||
|
-->
|
||||||
|
<interface domain="blueprint-tests">
|
||||||
|
<requires lib="gtk" version="4.0"/>
|
||||||
|
</interface>
|
Loading…
Add table
Add a link
Reference in a new issue