From 3e57f2f4821752020a72cf4f0d5fddaa7acb75ac Mon Sep 17 00:00:00 2001 From: Jehan Date: Thu, 26 May 2022 01:03:07 +0200 Subject: [PATCH] extensions, po-plug-ins: demo extensions use the new i18n logic. Since these are demos, for the sake of showing how the localization works, let's localize the goat-exercises with a locally installed catalog. Note that actually use the gimp30-std-plug-ins catalog, simply I copy it in the plug-in folder and rename it as org.gimp.extension.goat-exercises domain. As a consequence: - The C plug-in does not need the INIT_I18N anymore, which was specifically for the centrally installed catalog and cannot be used by third-party plug-in developers (so it's not a good demo code). - I now use GLib.dgettext() for Python instead of the gettext Python module, because the later won't "catch" the catalog declared in libgimp. - The other Goat exercises are now localized correctly, unlike before. - Just setting GETTEXT_PACKAGE is apparently enough for the Vala plug-in. - Lua is untested. Hopefully the code will work. --- extensions/goat-exercises/goat-exercise-c.c | 2 -- extensions/goat-exercises/goat-exercise-gjs.js | 16 +++++++++------- extensions/goat-exercises/goat-exercise-lua.lua | 16 ++++++++++++---- extensions/goat-exercises/goat-exercise-py3.py | 15 +-------------- .../goat-exercises/goat-exercise-vala.vala | 6 +++--- extensions/goat-exercises/meson.build | 2 +- po-plug-ins/meson.build | 7 +++++++ 7 files changed, 33 insertions(+), 31 deletions(-) diff --git a/extensions/goat-exercises/goat-exercise-c.c b/extensions/goat-exercises/goat-exercise-c.c index 43355495e5..3ce9f95e47 100644 --- a/extensions/goat-exercises/goat-exercise-c.c +++ b/extensions/goat-exercises/goat-exercise-c.c @@ -142,8 +142,6 @@ goat_run (GimpProcedure *procedure, GimpPDBStatusType status = GIMP_PDB_SUCCESS; gint x, y, width, height; - INIT_I18N(); - if (n_drawables != 1) { GError *error = NULL; diff --git a/extensions/goat-exercises/goat-exercise-gjs.js b/extensions/goat-exercises/goat-exercise-gjs.js index e1942de35e..8b3d9d45c9 100755 --- a/extensions/goat-exercises/goat-exercise-gjs.js +++ b/extensions/goat-exercises/goat-exercise-gjs.js @@ -44,6 +44,8 @@ ARGV.unshift(System.programInvocationName); let url = "https://gitlab.gnome.org/GNOME/gimp/blob/master/extensions/goat-exercises/goat-exercise-gjs.js"; +function _(message) { return GLib.dgettext(null, message); } + var Goat = GObject.registerClass({ GTypeName: 'Goat', }, class Goat extends Gimp.PlugIn { @@ -58,12 +60,12 @@ var Goat = GObject.registerClass({ procedure.set_image_types("*"); procedure.set_sensitivity_mask(Gimp.ProcedureSensitivityMask.DRAWABLE); - procedure.set_menu_label("Exercise a JavaScript goat"); + procedure.set_menu_label(_("Exercise a JavaScript goat")); procedure.set_icon_name(GimpUi.ICON_GEGL); procedure.add_menu_path ('/Filters/Development/Goat exercises/'); - procedure.set_documentation("Exercise a goat in the JavaScript language (GJS)", - "Takes a goat for a walk in Javascript with the GJS interpreter", + procedure.set_documentation(_("Exercise a goat in the JavaScript language (GJS)"), + _("Takes a goat for a walk in Javascript with the GJS interpreter"), name); procedure.set_attribution("Jehan", "Jehan", "2019"); @@ -85,13 +87,13 @@ var Goat = GObject.registerClass({ GimpUi.init("goat-exercise-gjs"); /* TODO: help function and ID. */ let dialog = new GimpUi.Dialog({ - title: "Exercise a goat (JavaScript)", + title: _("Exercise a goat (JavaScript)"), role: "goat-exercise-JavaScript", use_header_bar: true, }); - dialog.add_button("_Cancel", Gtk.ResponseType.CANCEL); - dialog.add_button("_Source", Gtk.ResponseType.APPLY); - dialog.add_button("_OK", Gtk.ResponseType.OK); + dialog.add_button(_("_Cancel"), Gtk.ResponseType.CANCEL); + dialog.add_button(_("_Source"), Gtk.ResponseType.APPLY); + dialog.add_button(_("_OK"), Gtk.ResponseType.OK); let geometry = new Gdk.Geometry(); geometry.min_aspect = 0.5; diff --git a/extensions/goat-exercises/goat-exercise-lua.lua b/extensions/goat-exercises/goat-exercise-lua.lua index 1af3f44451..a9acfc663d 100755 --- a/extensions/goat-exercises/goat-exercise-lua.lua +++ b/extensions/goat-exercises/goat-exercise-lua.lua @@ -33,6 +33,14 @@ local Gdk = lgi.Gdk local Goat = lgi.package 'Goat' local Goat = lgi.Goat +function N_(message) + return message; +end + +function _(message) + return GLib.dgettext(nil, message); +end + function run(procedure, run_mode, image, drawables, args, run_data) -- procedure:new_return_values() crashes LGI so we construct the -- GimpValueArray manually. @@ -56,13 +64,13 @@ function run(procedure, run_mode, image, drawables, args, run_data) if run_mode == "INTERACTIVE" then GimpUi.init("goat-exercise-lua"); local dialog = GimpUi.Dialog { - title = "Exercise a goat (Lua)", + title = N_("Exercise a goat (Lua)"), role = "goat-exercise-Lua", use_header_bar = 1 } - dialog:add_button("_Cancel", Gtk.ResponseType.CANCEL); - dialog:add_button("_Source", Gtk.ResponseType.APPLY); - dialog:add_button("_OK", Gtk.ResponseType.OK); + dialog:add_button(_("_Cancel"), Gtk.ResponseType.CANCEL); + dialog:add_button(_("_Source"), Gtk.ResponseType.APPLY); + dialog:add_button(_("_OK"), Gtk.ResponseType.OK); local geometry = Gdk.Geometry() geometry.min_aspect = 0.5; diff --git a/extensions/goat-exercises/goat-exercise-py3.py b/extensions/goat-exercises/goat-exercise-py3.py index da7c7e58b1..30b026a797 100755 --- a/extensions/goat-exercises/goat-exercise-py3.py +++ b/extensions/goat-exercises/goat-exercise-py3.py @@ -24,28 +24,15 @@ from gi.repository import GObject from gi.repository import GLib from gi.repository import Gio -import gettext import os import sys -# Set-up localization for your plug-in with your own text domain. -# This is complementary to the gimp_plug_in_set_translation_domain() -# which is only useful for the menu entries inside GIMP interface, -# whereas the below calls are used for localization within the plug-in. -textdomain = 'gimp30-std-plug-ins' -gettext.bindtextdomain(textdomain, Gimp.locale_directory()) -gettext.textdomain(textdomain) -_ = gettext.gettext def N_(message): return message +def _(message): return GLib.dgettext(None, message) class Goat (Gimp.PlugIn): ## GimpPlugIn virtual methods ## def do_query_procedures(self): - # Localization for the menu entries. It has to be called in the - # query function only. - self.set_translation_domain(textdomain, - Gio.file_new_for_path(Gimp.locale_directory())) - return [ "plug-in-goat-exercise-python" ] def do_create_procedure(self, name): diff --git a/extensions/goat-exercises/goat-exercise-vala.vala b/extensions/goat-exercises/goat-exercise-vala.vala index 004d9e18d1..5b81866f1f 100755 --- a/extensions/goat-exercises/goat-exercise-vala.vala +++ b/extensions/goat-exercises/goat-exercise-vala.vala @@ -42,9 +42,9 @@ public class Goat : Gimp.PlugIn { var procedure = new Gimp.ImageProcedure(this, name, Gimp.PDBProcType.PLUGIN, this.run); procedure.set_image_types("RGB*, INDEXED*, GRAY*"); procedure.set_sensitivity_mask(Gimp.ProcedureSensitivityMask.DRAWABLE); - procedure.set_menu_label("Exercise a Vala goat"); - procedure.set_documentation("Exercise a goat in the Vala language", - "Takes a goat for a walk in Vala", + procedure.set_menu_label(N_("Exercise a Vala goat")); + procedure.set_documentation(N_("Exercise a goat in the Vala language"), + N_("Takes a goat for a walk in Vala"), PLUG_IN_PROC); procedure.add_menu_path("/Filters/Development/Goat exercises/"); procedure.set_attribution("Niels De Graef", "Niels De Graef", "2020"); diff --git a/extensions/goat-exercises/meson.build b/extensions/goat-exercises/meson.build index d53b54847a..7a379c81f6 100644 --- a/extensions/goat-exercises/meson.build +++ b/extensions/goat-exercises/meson.build @@ -50,7 +50,7 @@ if have_vala and have_gobject_introspection libgimp_vapi, libgimpui_vapi, gtk3, gegl, math, ], c_args: [ - '-DGETTEXT_PACKAGE="@0@"'.format(gettext_package), + '-DGETTEXT_PACKAGE="@0@"'.format('org.gimp.extension.goat-exercises'), ], install: true, install_dir: gimpplugindir / 'extensions' / extension_name, diff --git a/po-plug-ins/meson.build b/po-plug-ins/meson.build index 2f8bc85d28..1f12c0f781 100644 --- a/po-plug-ins/meson.build +++ b/po-plug-ins/meson.build @@ -1,2 +1,9 @@ po_plug_ins_dir = meson.current_source_dir() i18n.gettext(gettext_package + '-std-plug-ins', preset: 'glib') + +# Special-casing, we just reuse the same locale dictionnary for our demo +# extension. We could have separated the strings but just for the sake +# of demo on code-side, it's simpler to do it this way. +extension_i18n = 'org.gimp.extension.goat-exercises' +i18n.gettext(extension_i18n, preset: 'glib', + install_dir: gimpplugindir / 'extensions' / extension_i18n / 'locale')