From 41dc6ddbe2186eaed476e725d6cb1a0ed7207bf8 Mon Sep 17 00:00:00 2001 From: c-holtermann Date: Sat, 29 Feb 2020 16:16:05 +0100 Subject: [PATCH] implement gettext localization for python make gnc_path_get_localedir() and GETTEXT_PACKAGE available for python to access locales. Import gettext module to provide _ as translator method. Provide null _-method returning english text in case of missing gettext. Make a lot of messages translatable by adding _-method. Include python files to create .pot-files. --- bindings/core-utils.i | 6 ++++++ bindings/python/gnucash_core.py | 21 ++++++++++++++++++++- gnucash/python/init.py | 6 ++++++ po/CMakeLists.txt | 2 +- 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/bindings/core-utils.i b/bindings/core-utils.i index c869770673..f66a184e80 100644 --- a/bindings/core-utils.i +++ b/bindings/core-utils.i @@ -57,6 +57,12 @@ gchar * gnc_path_get_scmdir(void); %newobject gnc_path_get_reportsdir; gchar * gnc_path_get_reportsdir(void); +%newobject gnc_path_get_localedir; +gchar * gnc_path_get_localedir(void); + +/* Name of our gettext-domain (defined in config.h) */ +%constant char* GETTEXT_PACKAGE = GETTEXT_PACKAGE; + %newobject gnc_path_get_stdreportsdir; gchar * gnc_path_get_stdreportsdir(void); diff --git a/bindings/python/gnucash_core.py b/bindings/python/gnucash_core.py index 6302fba46d..a660c6afb0 100644 --- a/bindings/python/gnucash_core.py +++ b/bindings/python/gnucash_core.py @@ -28,7 +28,8 @@ # @author Jeff Green, ParIT Worker Co-operative # @ingroup python_bindings -import gnucash.gnucash_core_c as gnucash_core_c +from gnucash import gnucash_core_c +from gnucash import _sw_core_utils from gnucash.function_class import \ ClassFromFunctions, extract_attributes_with_prefix, \ @@ -46,6 +47,24 @@ from gnucash.gnucash_core_c import gncInvoiceLookup, gncInvoiceGetInvoiceFromTxn gnc_numeric_create, double_to_gnc_numeric, string_to_gnc_numeric, \ gnc_numeric_to_string +try: + import gettext + # install gettext for _-function, needs path to locales + _localedir = _sw_core_utils.gnc_path_get_localedir() + _translation = gettext.translation(_sw_core_utils.GETTEXT_PACKAGE, _localedir) + _ = _translation.gettext +except: + print("\nProblem importing gettext!") + import traceback + import sys + exc_type, exc_value, exc_traceback = sys.exc_info() + traceback.print_exception(exc_type, exc_value, exc_traceback) + print() + + def _(s): + """Null translator function, gettext not available""" + return s + class GnuCashCoreClass(ClassFromFunctions): _module = gnucash_core_c diff --git a/gnucash/python/init.py b/gnucash/python/init.py index bfc319ad5c..c364b2dd4e 100644 --- a/gnucash/python/init.py +++ b/gnucash/python/init.py @@ -1,16 +1,22 @@ import sys from gnucash import * +from gnucash import _sw_app_utils +from gnucash import _sw_core_utils from gnucash._sw_core_utils import gnc_prefs_is_extra_enabled, gnc_prefs_is_debugging_enabled +from gnucash.gnucash_core import _ from gi import require_version require_version('Gtk', '3.0') from gi.repository import Gtk import os + sys.path.append(os.path.dirname(__file__)) # output file location if gnucash has been started with # gnucash --extra if gnc_prefs_is_extra_enabled(): print("Python shell init file: %s" % (__file__)) + print("\n" + "The following string should appear translated in your preferred language:" + "\n") + print("\n" + _("Welcome to GnuCash") +"\n") # Importing the console class causes SIGTTOU to be thrown if GnuCash is # started in the background. This causes a hang if it is not handled, diff --git a/po/CMakeLists.txt b/po/CMakeLists.txt index 5c4cbaefa0..51180baa0e 100644 --- a/po/CMakeLists.txt +++ b/po/CMakeLists.txt @@ -75,7 +75,7 @@ function(make_gnucash_potfiles) ${CMAKE_SOURCE_DIR}/*.glade ${CMAKE_SOURCE_DIR}/*.desktop.in.in ${CMAKE_SOURCE_DIR}/*.gschema.xml.in ${CMAKE_SOURCE_DIR}/*.appdata.xml.in.in ${CMAKE_SOURCE_DIR}/*.keys.in ${CMAKE_SOURCE_DIR}/*.scm - ${CMAKE_SOURCE_DIR}/*/qofbookslots.h + ${CMAKE_SOURCE_DIR}/*.py ${CMAKE_SOURCE_DIR}/*/qofbookslots.h ${CMAKE_SOURCE_DIR}/*/gnc-commodity.h )