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.
This commit is contained in:
c-holtermann 2020-02-29 16:16:05 +01:00
parent 890637b2dc
commit 41dc6ddbe2
4 changed files with 33 additions and 2 deletions

View File

@ -57,6 +57,12 @@ gchar * gnc_path_get_scmdir(void);
%newobject gnc_path_get_reportsdir; %newobject gnc_path_get_reportsdir;
gchar * gnc_path_get_reportsdir(void); 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; %newobject gnc_path_get_stdreportsdir;
gchar * gnc_path_get_stdreportsdir(void); gchar * gnc_path_get_stdreportsdir(void);

View File

@ -28,7 +28,8 @@
# @author Jeff Green, ParIT Worker Co-operative <jeff@parit.ca> # @author Jeff Green, ParIT Worker Co-operative <jeff@parit.ca>
# @ingroup python_bindings # @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 \ from gnucash.function_class import \
ClassFromFunctions, extract_attributes_with_prefix, \ 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_create, double_to_gnc_numeric, string_to_gnc_numeric, \
gnc_numeric_to_string 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): class GnuCashCoreClass(ClassFromFunctions):
_module = gnucash_core_c _module = gnucash_core_c

View File

@ -1,16 +1,22 @@
import sys import sys
from gnucash import * 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._sw_core_utils import gnc_prefs_is_extra_enabled, gnc_prefs_is_debugging_enabled
from gnucash.gnucash_core import _
from gi import require_version from gi import require_version
require_version('Gtk', '3.0') require_version('Gtk', '3.0')
from gi.repository import Gtk from gi.repository import Gtk
import os import os
sys.path.append(os.path.dirname(__file__)) sys.path.append(os.path.dirname(__file__))
# output file location if gnucash has been started with # output file location if gnucash has been started with
# gnucash --extra # gnucash --extra
if gnc_prefs_is_extra_enabled(): if gnc_prefs_is_extra_enabled():
print("Python shell init file: %s" % (__file__)) 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 # 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, # started in the background. This causes a hang if it is not handled,

View File

@ -75,7 +75,7 @@ function(make_gnucash_potfiles)
${CMAKE_SOURCE_DIR}/*.glade ${CMAKE_SOURCE_DIR}/*.desktop.in.in ${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}/*.gschema.xml.in ${CMAKE_SOURCE_DIR}/*.appdata.xml.in.in
${CMAKE_SOURCE_DIR}/*.keys.in ${CMAKE_SOURCE_DIR}/*.scm ${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 ${CMAKE_SOURCE_DIR}/*/gnc-commodity.h
) )