From d9eb3274aaa6e1395c86c4dfaa488e493969857b Mon Sep 17 00:00:00 2001 From: mike Date: Mon, 30 Nov 2009 16:51:15 +0000 Subject: LP 448319 : Translation layer tests and automatic generation of pot file (ongoing) --- (limited to 'tutorius/translator.py') diff --git a/tutorius/translator.py b/tutorius/translator.py index bd24f8f..f8a3fd7 100644 --- a/tutorius/translator.py +++ b/tutorius/translator.py @@ -17,6 +17,9 @@ import os import logging import copy as copy_module +import gettext +import os +import locale logger = logging.getLogger("ResourceTranslator") @@ -52,7 +55,47 @@ class ResourceTranslator(object): """ self._probe_manager = probe_manager self._tutorial_id = tutorial_id + + # Pick up the language for the user + langs = [] + language = os.environ.get("LANGUAGE", None) + if language: + langs = language.split(':') + + # Get the default machine language + lc, encoding = locale.getdefaultlocale() + if lc: + langs += [lc] + + l10n_dir = Vault.get_localization_dir(tutorial_id) + logger.debug("ResourceTranslator :: Looking for localization resources for languages %s in folder %s"%(str(langs), l10n_dir)) + if l10n_dir: + try: + trans = gettext.translation('tutorial_text', + l10n_dir, + languages=langs) + self._ = trans.ugettext + except IOError: + self._ = None + else: + self._ = None + def translate_string(self, str_value): + """ + Replaces the TString property by its localized equivalent. + + @param str_value The straing to translate + """ + # If we have a localization folder + if self._: + # Apply the translation + u_string = unicode(self._(str_value)) + + # Encode the string in UTF-8 for it to pass thru DBus + return u_string.encode("utf-8") + # There was no translation + return unicode(str_value).encode("utf-8") + def translate_resource(self, res_value): """ Replace the TResourceProperty in the container by their @@ -62,9 +105,9 @@ class ResourceTranslator(object): to transform the resource identifier into the absolute path for the process to be able to use it properly. - @param res_prop The resource property's value to be translated + @param res_value The resource property's value to be translated @return The TFileProperty corresponding to this resource, containing - an absolute path to the resource + an absolute path to it """ # We need to replace the resource by a file representation filepath = Vault.get_resource_path(self._tutorial_id, res_value) @@ -100,6 +143,12 @@ class ResourceTranslator(object): prop_value = getattr(prop_container, propname) prop_type = getattr(type(prop_container), propname).type + # If the propert is a string, we need to use the localization + # to find it's equivalent + if prop_type == "string": + str_value = self.translate_string(prop_value) + prop_container.replace_property(propname, str_value) + # If the property is a resource, then we need to query the # vault to create its correspondent if prop_type == "resource": -- cgit v0.9.1