Merge branch 'i18n' into l18n-fr

This commit is contained in:
Valentin Lorentz 2010-11-01 11:49:04 +01:00
commit ddb3dd5a50
4 changed files with 50 additions and 16 deletions

View File

@ -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)))

View File

@ -60,13 +60,10 @@ def get_plugin_dir(plugin_name):
filename = None
try:
filename = sys.modules[plugin_name].__file__
except KeyError: # This is odd
except KeyError: # It sometimes happens with Owner
pass
if filename == None:
try:
filename = sys.modules['supybot.plugins.' + plugin_name].__file__
except KeyError: # This is odder
return
filename = sys.modules['supybot.plugins.' + plugin_name].__file__
if filename.endswith(".pyc"):
filename = filename[0:-1]
@ -171,7 +168,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
@ -182,25 +179,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':
@ -267,6 +286,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.

View File

@ -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

View File

@ -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."""