From 4dcd7f7fb46a11149a56d38885e1dc57c836f3c5 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Mon, 1 Nov 2010 11:42:33 +0100 Subject: [PATCH 1/2] Bug fixes (mainly the &config help internationalization) --- plugins/Misc/plugin.py | 2 +- src/i18n.py | 53 +++++++++++++++++++++++++++++++++++------- src/registry.py | 3 ++- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/plugins/Misc/plugin.py b/plugins/Misc/plugin.py index 147f29701..4f9510148 100644 --- a/plugins/Misc/plugin.py +++ b/plugins/Misc/plugin.py @@ -199,7 +199,7 @@ class Misc(callbacks.Plugin): 'you want help with.'), names)) else: assert cbs, 'Odd, maxL == command, but no cbs.' - irc.reply(cbs[0].getCommandHelp(command, False)) + irc.reply(_.__call__(cbs[0].getCommandHelp(command, False))) else: irc.error(format(_('There is no command %q.'), callbacks.formatCommand(command))) diff --git a/src/i18n.py b/src/i18n.py index 2d0c6d45e..4005e2288 100644 --- a/src/i18n.py +++ b/src/i18n.py @@ -57,7 +57,13 @@ def import_conf(): i18nClasses[key].loadLocale() def get_plugin_dir(plugin_name): - filename = sys.modules[plugin_name].__file__ + filename = None + try: + filename = sys.modules[plugin_name].__file__ + except KeyError: # It sometimes happens with Owner + pass + if filename == None: + filename = sys.modules['supybot.plugins.' + plugin_name].__file__ if filename.endswith(".pyc"): filename = filename[0:-1] @@ -164,7 +170,7 @@ class _PluginInternationalization: if len(data) == 0: # Multiline mode step = IN_MSGSTR else: - self._translate(untranslated, data) + self._addToDatabase(untranslated, data) step = WAITING_FOR_MSGID @@ -175,25 +181,47 @@ class _PluginInternationalization: step = WAITING_FOR_MSGID if translated == '': translated = untranslated - self._translate(untranslated, translated) + self._addToDatabase(untranslated, translated) - def _translate(self, untranslated, translated): - self.translations.update({self._unescape(untranslated): - self._unescape(translated)}) + def _addToDatabase(self, untranslated, translated): + untranslated = self._unescape(untranslated) + translated = self._unescape(translated) + self.translations.update({untranslated: translated}) def _unescape(self, string): - return str.replace(string, '\\n', '\n') + import supybot.utils as utils + return utils.str.normalizeWhitespace(str.replace(string, '\\n', '\n')) def __call__(self, untranslated): + if untranslated.__class__ == internationalizedString: + return untranslated._original + untranslated = __import__('supybot').utils.str.normalizeWhitespace(untranslated) if not 'conf' in globals(): return untranslated if self.currentLocaleName != conf.supybot.language(): # If the locale has been changed reloadLocals() try: - return self.translations[untranslated] + string = self._translate(untranslated) + return self._addTracker(string, untranslated) except KeyError: - return untranslated + pass + return untranslated + + def _translate(self, string): + if string.__class__ == internationalizedString: + return string._internationalizer(string.untranslated) + else: + return self.translations[string] + + def _addTracker(self, string, untranslated): + if string.__class__ == internationalizedString: + return string + else: + string = internationalizedString(string) + string._original = untranslated + string._internationalizer = self + return string def _loadL10nCode(self): if self.name != 'supybot': @@ -260,6 +288,13 @@ class internationalizedFunction: def restore(self): self.__call__ = self._origin +class internationalizedString(str): + pass +# def __init__(self, *args, **kwargs): +# self.__parent.__init__(*args, **kwargs) +# self._original = str(self) +# self._internationalizer = None + def internationalizeDocstring(obj): """Decorates functions and internationalize their docstring. diff --git a/src/registry.py b/src/registry.py index 5fb9b7f25..2497b4390 100644 --- a/src/registry.py +++ b/src/registry.py @@ -35,6 +35,7 @@ import string import textwrap import supybot.utils as utils +import supybot.i18n as i18n def error(s): """Replace me with something better from another module!""" @@ -211,7 +212,7 @@ class Group(object): self.__nonExistentEntry(attr) def help(self): - return self._help + return i18n.PluginInternationalization().__call__(self._help) def get(self, attr): # Not getattr(self, attr) because some nodes might have groups that From a2c8c613db7058d63a68ef5b9f8c247859fc78c4 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Mon, 1 Nov 2010 11:47:19 +0100 Subject: [PATCH 2/2] Bug fix (didn't keep any beginning or ending space) --- src/utils/str.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/utils/str.py b/src/utils/str.py index 32d24e418..5eb8cb569 100644 --- a/src/utils/str.py +++ b/src/utils/str.py @@ -61,7 +61,14 @@ def rsplit(s, sep=None, maxsplit=-1): def normalizeWhitespace(s): """Normalizes the whitespace in a string; \s+ becomes one space.""" - return ' '.join(s.split()) + beginning = s.startswith(' ') + ending = s.endswith(' ') + s = ' '.join(s.split()) + if beginning: + s = ' ' + s + if ending: + s = s + ' ' + return s def distance(s, t): """Returns the levenshtein edit distance between two strings."""