1# coding=utf-8 2# 3# Copyright (C) 2010 Nick Drobchenko, nick@cnc-club.ru 4# Copyright (C) 2005 Aaron Spike, aaron@ekips.org 5# 6# This program is free software; you can redistribute it and/or modify 7# it under the terms of the GNU General Public License as published by 8# the Free Software Foundation; either version 2 of the License, or 9# (at your option) any later version. 10# 11# This program is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14# GNU General Public License for more details. 15# 16# You should have received a copy of the GNU General Public License 17# along with this program; if not, write to the Free Software 18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19# 20""" 21Allow extensions to translate messages. 22""" 23 24import gettext 25import os 26 27# Get gettext domain and matching locale directory for translation of extensions strings 28# (both environment variables are set by Inkscape) 29GETTEXT_DOMAIN = os.environ.get('INKEX_GETTEXT_DOMAIN') 30GETTEXT_DIRECTORY = os.environ.get('INKEX_GETTEXT_DIRECTORY') 31 32# INKSCAPE_LOCALEDIR can be used to override the default locale directory Inkscape uses 33INKSCAPE_LOCALEDIR = os.environ.get('INKSCAPE_LOCALEDIR') 34 35def localize(domain=GETTEXT_DOMAIN, localedir=GETTEXT_DIRECTORY): 36 """Configure gettext and install _() function into builtins namespace for easy access""" 37 38 # Do not enable translation if GETTEXT_DOMAIN is unset. 39 # This is the case when translationdomain="none", but also when no catalog was found. 40 # Install a NullTranslation just to be sure (so we do not get errors about undefined '_') 41 if domain is None: 42 gettext.NullTranslations().install() 43 return 44 45 # Use the default system locale by default, 46 # but prefer LANGUAGE environment variable (which is set by Inkscape according to UI language) 47 languages = None 48 49 trans = gettext.translation(domain, localedir, languages, fallback=True) 50 trans.install() 51 52 53 54def inkex_localize(): 55 """ 56 Return internal Translations instance for translation of the inkex module itself 57 Those will always use the 'inkscape' domain and attempt to lookup the same catalog Inkscape uses 58 """ 59 60 domain = 'inkscape' 61 localedir = INKSCAPE_LOCALEDIR 62 languages = None 63 64 return gettext.translation(domain, localedir, languages, fallback=True) 65 66inkex_gettext = inkex_localize().gettext # pylint: disable=invalid-name 67