diff options
Diffstat (limited to 'tutorius/translator.py')
-rw-r--r-- | tutorius/translator.py | 96 |
1 files changed, 80 insertions, 16 deletions
diff --git a/tutorius/translator.py b/tutorius/translator.py index f1c088b..ee1067b 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,18 +105,15 @@ 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) logger.debug("ResourceTranslator :: Matching resource %s to file %s" % (res_value, filepath)) - # Create the new file representation - file_prop = TFileProperty(filepath) - - return file_prop + return filepath def translate(self, prop_container): """ @@ -100,6 +140,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": @@ -150,12 +196,6 @@ class ResourceTranslator(object): def detach(self, activity_id): self._probe_manager.detach(activity_id) - def subscribe(self, event, notification_cb, event_subscribed_cb, error_cb): - return self._probe_manager.subscribe(event, notification_cb, event_subscribed_cb, error_cb) - - def unsubscribe(self, address): - return self._probe_manager.unsubscribe(address) - def register_probe(self, process_name, unique_id): self._probe_manager.register_probe(process_name, unique_id) @@ -165,6 +205,8 @@ class ResourceTranslator(object): def get_registered_probes_list(self, process_name=None): return self._probe_manager.get_registered_probes_list(process_name) + def create_event(self, *args, **kwargs): + return self._probe_manager.create_event(*args, **kwargs) ########################################################################### def action_installed(self, action_installed_cb, address): @@ -186,17 +228,39 @@ class ResourceTranslator(object): self.translate(new_action) # Send the new action to the probe manager - self._probe_manager.install(new_action, save_args(self.action_installed, action_installed_cb), + self._probe_manager.install(new_action, + save_args(self.action_installed, action_installed_cb), save_args(self.action_install_error, error_cb, new_action), is_editing=is_editing, editing_cb=editing_cb) - def update(self, action_address, newaction): + def update(self, action_address, newaction, is_editing=False): translated_new_action = copy_module.deepcopy(newaction) self.translate(translated_new_action) - self._probe_manager.update(action_address, translated_new_action, block) + self._probe_manager.update(action_address, translated_new_action, is_editing) - def uninstall(self, action_address): + def uninstall(self, action_address, is_editing=False): self._probe_manager.uninstall(action_address) + def subscribe_complete_cb(self, event_subscribed_cb, event, address): + event_subscribed_cb(address) + + def event_subscribe_error(self, error_cb, event, exception): + error_cb(event, exception) + + def translator_notification_cb(self, event, notification_cb, new_event): + notification_cb(event) + + def subscribe(self, event, notification_cb, event_subscribed_cb, error_cb): + new_event = copy_module.deepcopy(event) + self.translate(new_event) + + self._probe_manager.subscribe(new_event, + save_args(self.translator_notification_cb, event, notification_cb), + save_args(self.subscribe_complete_cb, event_subscribed_cb, event), + save_args(self.event_subscribe_error, error_cb)) + + def unsubscribe(self, address): + return self._probe_manager.unsubscribe(address) + |