diff options
Diffstat (limited to 'build_tools/larch8/liblarch/translation.py')
-rw-r--r-- | build_tools/larch8/liblarch/translation.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/build_tools/larch8/liblarch/translation.py b/build_tools/larch8/liblarch/translation.py new file mode 100644 index 0000000..114a8a2 --- /dev/null +++ b/build_tools/larch8/liblarch/translation.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python2 +# +# translation.py -- Translation services +# +# (c) Copyright 2010 Michael Towers (larch42 at googlemail dot com) +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# +#------------------------------------------------------------------- +# 2010.08.14 + +import os, gettext +from liblarch_conf import liblarchdir + +def i18n_module(basedir, appname): + """This doesn't use gettext.install in order not to affect the whole + application, it allows per module translation services. + """ + return gettext.translation(appname, basedir + '/i18n', fallback=True).ugettext + + +def i18n_liblarch(): + """Provides translation for liblarch itself. + """ + return i18n_module(liblarchdir, 'liblarch') +_ = i18n_liblarch() + + +lang = (os.environ.get('LANGUAGE') or os.environ.get('LC_ALL') + or os.environ.get('LC_MESSAGES') or os.environ.get('LANG')) + + +def i18nurl(doc): + docbase, docfile = doc.rsplit('/', 1) + docbase += '/' + i18npath = docbase + lang.split('.')[0] + '/' + docfile + if not os.path.isfile(i18npath): + i18npath = docbase + lang.split('_')[0] + '/' + docfile + if not os.path.isfile(i18npath): + i18npath = doc + if not os.path.isfile(i18npath): + return None + return i18npath + +def i18ndoc(doc): + p = i18nurl(doc) + if not p: + return _("Document '%s' not found") % i18npath + fh = open(p) + data = fh.read().decode('utf-8') + fh.close() + return data + |