i18n: Cleaner code and potential bug fix of multi-line translations.

This commit is contained in:
Valentin Lorentz 2014-05-31 14:56:28 +02:00
parent 483e786cde
commit bd638ce350

View File

@ -128,6 +128,19 @@ def reloadLocales():
for function in internationalizedFunctions: for function in internationalizedFunctions:
function.loadLocale() function.loadLocale()
def normalize(string, removeNewline=False):
import supybot.utils as utils
string = string.replace('\\n\\n', '\n\n')
string = string.replace('\\n', ' ')
string = string.replace('\\"', '"')
string = string.replace("\'", "'")
string = utils.str.normalizeWhitespace(string, removeNewline)
string = string.strip(' ')
string = string.strip('\n')
string = string.strip('\t')
return string
def parse(translationFile): def parse(translationFile):
step = WAITING_FOR_MSGID step = WAITING_FOR_MSGID
translations = set() translations = set()
@ -172,11 +185,11 @@ def parse(translationFile):
step = WAITING_FOR_MSGID step = WAITING_FOR_MSGID
if translated == '': if translated == '':
translated = untranslated translated = untranslated
translations |= set([(untranslated, data)]) translations |= set([(untranslated, translated)])
if step is IN_MSGSTR: if step is IN_MSGSTR:
if translated == '': if translated == '':
translated = untranslated translated = untranslated
translations |= set([(untranslated, data)]) translations |= set([(untranslated, translated)])
return translations return translations
@ -229,30 +242,21 @@ class _PluginInternationalization:
self._addToDatabase(*translation) self._addToDatabase(*translation)
def _addToDatabase(self, untranslated, translated): def _addToDatabase(self, untranslated, translated):
untranslated = self._unescape(untranslated, True) untranslated = normalize(untranslated, True)
translated = self._unescape(translated) translated = normalize(translated)
if translated: if translated:
self.translations.update({untranslated: translated}) self.translations.update({untranslated: translated})
def _unescape(self, string, removeNewline=False):
import supybot.utils as utils
string = string.replace('\\n\\n', '\n\n')
string = string.replace('\\n', ' ')
string = string.replace('\\"', '"')
string = string.replace("\'", "'")
string = utils.str.normalizeWhitespace(string, removeNewline)
return string
def __call__(self, untranslated): def __call__(self, untranslated):
"""Main function. """Main function.
This is the function which is called when a plugin runs _()""" This is the function which is called when a plugin runs _()"""
if untranslated.__class__ is InternationalizedString: if untranslated.__class__ is InternationalizedString:
return untranslated._original return untranslated
escapedUntranslated = self._unescape(untranslated, True) normalizedUntranslated = normalize(untranslated, True)
untranslated = self._unescape(untranslated, False) untranslated = normalize(untranslated, False)
try: try:
string = self._translate(escapedUntranslated) string = self._translate(normalizedUntranslated)
return self._addTracker(string, untranslated) return self._addTracker(string, untranslated)
except KeyError: except KeyError:
pass pass
@ -360,10 +364,11 @@ def internationalizeDocstring(obj):
Only useful for commands (commands' docstring is displayed on IRC)""" Only useful for commands (commands' docstring is displayed on IRC)"""
if obj.__doc__ == None: if obj.__doc__ == None:
return obj return obj
if '_' in sys.modules[obj.__module__].__dict__: plugin_module = sys.modules[obj.__module__]
if '_' in plugin_module.__dict__:
internationalizedCommands.update({hash(obj): obj}) internationalizedCommands.update({hash(obj): obj})
try: try:
obj.__doc__=sys.modules[obj.__module__]._.__call__(obj.__doc__) obj.__doc__ = plugin_module._.__call__(obj.__doc__)
# We use _.__call__() instead of _() because of a pygettext warning. # We use _.__call__() instead of _() because of a pygettext warning.
except AttributeError: except AttributeError:
# attribute '__doc__' of 'type' objects is not writable # attribute '__doc__' of 'type' objects is not writable