2010-10-09 11:36:22 +02:00
|
|
|
###
|
|
|
|
# Copyright (c) 2010, Valentin Lorentz
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
|
|
|
Supybot internationalisation and localisation managment.
|
|
|
|
"""
|
|
|
|
|
2010-10-30 21:41:25 +02:00
|
|
|
__all__ = ['PluginInternationalization', 'internationalizeDocstring']
|
2010-10-09 11:36:22 +02:00
|
|
|
|
2013-01-11 18:29:04 +01:00
|
|
|
import os
|
2010-10-10 14:45:25 +02:00
|
|
|
import re
|
2010-10-09 11:36:22 +02:00
|
|
|
import sys
|
2010-10-20 18:26:52 +02:00
|
|
|
import time
|
2014-01-22 10:51:01 +01:00
|
|
|
import weakref
|
2010-10-20 18:26:52 +02:00
|
|
|
import threading
|
2010-11-11 12:01:56 +01:00
|
|
|
conf = None
|
2010-10-20 18:26:52 +02:00
|
|
|
# Don't import conf here ; because conf needs this module
|
2010-10-09 11:36:22 +02:00
|
|
|
|
|
|
|
WAITING_FOR_MSGID = 1
|
|
|
|
IN_MSGID = 2
|
|
|
|
WAITING_FOR_MSGSTR = 3
|
|
|
|
IN_MSGSTR = 4
|
|
|
|
|
|
|
|
MSGID = 'msgid "'
|
|
|
|
MSGSTR = 'msgstr "'
|
|
|
|
|
2010-11-11 12:01:56 +01:00
|
|
|
currentLocale = 'en'
|
|
|
|
|
2011-06-28 19:52:18 +02:00
|
|
|
class PluginNotFound(Exception):
|
|
|
|
pass
|
|
|
|
|
2010-11-11 12:01:56 +01:00
|
|
|
def getLocaleFromRegistryFilename(filename):
|
|
|
|
"""Called by the 'supybot' script. Gets the locale name before conf is
|
|
|
|
loaded."""
|
|
|
|
global currentLocale
|
2014-01-03 17:44:01 +01:00
|
|
|
with open(filename, 'r') as fd:
|
|
|
|
for line in fd:
|
|
|
|
if line.startswith('supybot.language: '):
|
|
|
|
currentLocale = line[len('supybot.language: '):]
|
2010-11-11 12:01:56 +01:00
|
|
|
|
2010-10-20 18:26:52 +02:00
|
|
|
def import_conf():
|
2010-11-11 12:01:56 +01:00
|
|
|
"""Imports the conf into this module"""
|
|
|
|
global conf
|
|
|
|
conf = __import__('supybot.conf').conf
|
2010-10-20 18:26:52 +02:00
|
|
|
conf.registerGlobalValue(conf.supybot, 'language',
|
2010-11-19 17:00:55 +01:00
|
|
|
conf.registry.String(currentLocale, """Determines the bot's default
|
2010-11-11 12:01:56 +01:00
|
|
|
language. Valid values are things like en, fr, de, etc."""))
|
2012-12-21 20:08:34 +01:00
|
|
|
conf.supybot.language.addCallback(reloadLocalesIfRequired)
|
2010-10-09 11:36:22 +02:00
|
|
|
|
2010-11-01 14:33:43 +01:00
|
|
|
def getPluginDir(plugin_name):
|
2010-11-11 12:01:56 +01:00
|
|
|
"""Gets the directory of the given plugin"""
|
2010-10-31 18:36:33 +01:00
|
|
|
filename = None
|
|
|
|
try:
|
2010-11-19 17:00:55 +01:00
|
|
|
filename = sys.modules[plugin_name].__file__
|
2010-11-01 11:42:33 +01:00
|
|
|
except KeyError: # It sometimes happens with Owner
|
2010-11-19 17:00:55 +01:00
|
|
|
pass
|
2010-10-31 18:36:33 +01:00
|
|
|
if filename == None:
|
2011-02-28 16:02:17 +01:00
|
|
|
try:
|
|
|
|
filename = sys.modules['supybot.plugins.' + plugin_name].__file__
|
|
|
|
except: # In the case where the plugin is not loaded by Supybot
|
|
|
|
try:
|
|
|
|
filename = sys.modules['plugin'].__file__
|
|
|
|
except:
|
|
|
|
filename = sys.modules['__main__'].__file__
|
2010-10-10 14:45:25 +02:00
|
|
|
if filename.endswith(".pyc"):
|
2010-11-19 17:00:55 +01:00
|
|
|
filename = filename[0:-1]
|
|
|
|
|
2010-10-10 14:45:25 +02:00
|
|
|
allowed_files = ['__init__.py', 'config.py', 'plugin.py', 'test.py']
|
|
|
|
for allowed_file in allowed_files:
|
2010-11-19 17:00:55 +01:00
|
|
|
if filename.endswith(allowed_file):
|
|
|
|
return filename[0:-len(allowed_file)]
|
2011-06-28 19:52:18 +02:00
|
|
|
raise PluginNotFound()
|
2010-10-09 11:36:22 +02:00
|
|
|
|
2010-10-28 17:28:27 +02:00
|
|
|
def getLocalePath(name, localeName, extension):
|
2010-11-11 12:01:56 +01:00
|
|
|
"""Gets the path of the locale file of the given plugin ('supybot' stands
|
|
|
|
for the core)."""
|
2010-10-28 17:28:27 +02:00
|
|
|
if name != 'supybot':
|
2013-01-11 18:29:04 +01:00
|
|
|
base = getPluginDir(name)
|
2010-10-28 17:28:27 +02:00
|
|
|
else:
|
2013-01-11 18:29:04 +01:00
|
|
|
from . import ansi # Any Supybot plugin could fit
|
|
|
|
base = ansi.__file__[0:-len('ansi.pyc')]
|
|
|
|
directory = os.path.join(base, 'locales')
|
2010-10-28 17:28:27 +02:00
|
|
|
return '%s/%s.%s' % (directory, localeName, extension)
|
|
|
|
|
2014-01-22 10:51:01 +01:00
|
|
|
i18nClasses = weakref.WeakValueDictionary()
|
|
|
|
internationalizedCommands = weakref.WeakValueDictionary()
|
|
|
|
internationalizedFunctions = [] # No need to know their name
|
2010-10-10 14:45:25 +02:00
|
|
|
|
2010-11-11 12:01:56 +01:00
|
|
|
def reloadLocalesIfRequired():
|
|
|
|
global currentLocale
|
|
|
|
if conf is None:
|
2010-11-19 17:00:55 +01:00
|
|
|
return
|
2010-11-11 12:01:56 +01:00
|
|
|
if currentLocale != conf.supybot.language():
|
2010-11-19 17:00:55 +01:00
|
|
|
currentLocale = conf.supybot.language()
|
|
|
|
reloadLocales()
|
2010-11-11 12:01:56 +01:00
|
|
|
|
|
|
|
def reloadLocales():
|
2014-01-22 10:51:01 +01:00
|
|
|
for pluginClass in i18nClasses.values():
|
|
|
|
pluginClass.loadLocale()
|
2012-12-21 20:08:34 +01:00
|
|
|
for command in internationalizedCommands.values():
|
|
|
|
internationalizeDocstring(command)
|
2014-01-22 11:04:08 +01:00
|
|
|
for function in internationalizedFunctions:
|
|
|
|
function.loadLocale()
|
2010-11-19 17:00:55 +01:00
|
|
|
|
2014-05-31 14:56:28 +02:00
|
|
|
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('\n')
|
|
|
|
string = string.strip('\t')
|
|
|
|
return string
|
|
|
|
|
|
|
|
|
2014-01-21 22:39:48 +01:00
|
|
|
def parse(translationFile):
|
|
|
|
step = WAITING_FOR_MSGID
|
|
|
|
translations = set()
|
|
|
|
for line in translationFile:
|
|
|
|
line = line[0:-1] # Remove the ending \n
|
|
|
|
line = line
|
|
|
|
|
|
|
|
if line.startswith(MSGID):
|
|
|
|
# Don't check if step is WAITING_FOR_MSGID
|
|
|
|
untranslated = ''
|
|
|
|
translated = ''
|
|
|
|
data = line[len(MSGID):-1]
|
|
|
|
if len(data) == 0: # Multiline mode
|
|
|
|
step = IN_MSGID
|
|
|
|
else:
|
|
|
|
untranslated += data
|
|
|
|
step = WAITING_FOR_MSGSTR
|
|
|
|
|
|
|
|
|
|
|
|
elif step is IN_MSGID and line.startswith('"') and \
|
|
|
|
line.endswith('"'):
|
|
|
|
untranslated += line[1:-1]
|
|
|
|
elif step is IN_MSGID and untranslated == '': # Empty MSGID
|
|
|
|
step = WAITING_FOR_MSGID
|
|
|
|
elif step is IN_MSGID: # the MSGID is finished
|
|
|
|
step = WAITING_FOR_MSGSTR
|
|
|
|
|
|
|
|
|
|
|
|
if step is WAITING_FOR_MSGSTR and line.startswith(MSGSTR):
|
|
|
|
data = line[len(MSGSTR):-1]
|
|
|
|
if len(data) == 0: # Multiline mode
|
|
|
|
step = IN_MSGSTR
|
|
|
|
else:
|
2014-01-26 21:34:24 +01:00
|
|
|
translations |= set([(untranslated, data)])
|
2014-01-21 22:39:48 +01:00
|
|
|
step = WAITING_FOR_MSGID
|
|
|
|
|
|
|
|
|
|
|
|
elif step is IN_MSGSTR and line.startswith('"') and \
|
|
|
|
line.endswith('"'):
|
|
|
|
translated += line[1:-1]
|
|
|
|
elif step is IN_MSGSTR: # the MSGSTR is finished
|
|
|
|
step = WAITING_FOR_MSGID
|
|
|
|
if translated == '':
|
|
|
|
translated = untranslated
|
2014-05-31 14:56:28 +02:00
|
|
|
translations |= set([(untranslated, translated)])
|
2014-01-21 22:39:48 +01:00
|
|
|
if step is IN_MSGSTR:
|
|
|
|
if translated == '':
|
|
|
|
translated = untranslated
|
2014-05-31 14:56:28 +02:00
|
|
|
translations |= set([(untranslated, translated)])
|
2014-01-21 22:39:48 +01:00
|
|
|
return translations
|
|
|
|
|
2010-10-09 11:36:22 +02:00
|
|
|
|
2010-10-30 21:41:25 +02:00
|
|
|
i18nSupybot = None
|
2010-10-30 21:10:49 +02:00
|
|
|
def PluginInternationalization(name='supybot'):
|
2010-10-31 18:36:33 +01:00
|
|
|
# This is a proxy that prevents having several objects for the same plugin
|
Start accelerating the 2to3 step (remove fix_apply, fix_buffer, fix_callable, fix_exec, fix_execfile, fix_exitfunc, fix_filter, fix_funcattrs, fix_future, fix_getcwdu, and fix_has_key).
2014-01-20 14:49:47 +01:00
|
|
|
if name in i18nClasses:
|
2010-11-19 17:00:55 +01:00
|
|
|
return i18nClasses[name]
|
2010-10-31 18:36:33 +01:00
|
|
|
else:
|
2010-11-19 17:00:55 +01:00
|
|
|
return _PluginInternationalization(name)
|
2010-10-30 21:10:49 +02:00
|
|
|
|
|
|
|
class _PluginInternationalization:
|
2010-10-09 11:36:22 +02:00
|
|
|
"""Internationalization managment for a plugin."""
|
|
|
|
def __init__(self, name='supybot'):
|
2010-11-19 17:00:55 +01:00
|
|
|
self.name = name
|
2012-12-19 18:04:39 +01:00
|
|
|
self.translations = {}
|
2010-11-19 17:00:55 +01:00
|
|
|
self.currentLocaleName = None
|
|
|
|
i18nClasses.update({name: self})
|
|
|
|
self.loadLocale()
|
|
|
|
|
2010-10-10 14:45:25 +02:00
|
|
|
def loadLocale(self, localeName=None):
|
2010-11-19 17:00:55 +01:00
|
|
|
"""(Re)loads the locale used by this class."""
|
2012-12-21 19:19:03 +01:00
|
|
|
self.translations = {}
|
2010-11-19 17:00:55 +01:00
|
|
|
if localeName is None:
|
|
|
|
localeName = currentLocale
|
|
|
|
self.currentLocaleName = localeName
|
|
|
|
|
|
|
|
self._loadL10nCode()
|
|
|
|
|
|
|
|
try:
|
2011-05-07 09:12:03 +02:00
|
|
|
try:
|
|
|
|
translationFile = open(getLocalePath(self.name,
|
|
|
|
localeName, 'po'), 'ru')
|
|
|
|
except ValueError: # We are using Windows
|
|
|
|
translationFile = open(getLocalePath(self.name,
|
|
|
|
localeName, 'po'), 'r')
|
2010-11-19 17:00:55 +01:00
|
|
|
self._parse(translationFile)
|
2012-12-22 01:06:26 +01:00
|
|
|
except (IOError, PluginNotFound): # The translation is unavailable
|
2012-12-19 18:04:39 +01:00
|
|
|
pass
|
2014-01-03 17:44:01 +01:00
|
|
|
finally:
|
2014-01-03 18:15:32 +01:00
|
|
|
if 'translationFile' in locals():
|
|
|
|
translationFile.close()
|
2012-12-19 18:04:39 +01:00
|
|
|
|
2010-10-30 21:41:25 +02:00
|
|
|
def _parse(self, translationFile):
|
2010-11-19 17:00:55 +01:00
|
|
|
"""A .po files parser.
|
|
|
|
|
|
|
|
Give it a file object."""
|
|
|
|
self.translations = {}
|
2014-01-21 22:39:48 +01:00
|
|
|
for translation in parse(translationFile):
|
|
|
|
self._addToDatabase(*translation)
|
2010-11-19 17:00:55 +01:00
|
|
|
|
2010-11-01 11:42:33 +01:00
|
|
|
def _addToDatabase(self, untranslated, translated):
|
2014-05-31 14:56:28 +02:00
|
|
|
untranslated = normalize(untranslated, True)
|
|
|
|
translated = normalize(translated)
|
2014-03-10 16:25:16 +01:00
|
|
|
if translated:
|
|
|
|
self.translations.update({untranslated: translated})
|
2010-11-19 17:00:55 +01:00
|
|
|
|
2010-10-30 21:41:25 +02:00
|
|
|
def __call__(self, untranslated):
|
2010-11-19 17:00:55 +01:00
|
|
|
"""Main function.
|
|
|
|
|
2013-03-08 20:26:50 +01:00
|
|
|
This is the function which is called when a plugin runs _()"""
|
2014-05-31 14:56:28 +02:00
|
|
|
normalizedUntranslated = normalize(untranslated, True)
|
2010-11-19 17:00:55 +01:00
|
|
|
try:
|
2014-05-31 14:56:28 +02:00
|
|
|
string = self._translate(normalizedUntranslated)
|
2010-11-19 17:00:55 +01:00
|
|
|
return self._addTracker(string, untranslated)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2014-05-31 15:18:23 +02:00
|
|
|
if untranslated.__class__ is InternationalizedString:
|
|
|
|
return untranslated._original
|
|
|
|
else:
|
|
|
|
return untranslated
|
2010-11-19 17:00:55 +01:00
|
|
|
|
2010-11-01 11:42:33 +01:00
|
|
|
def _translate(self, string):
|
2010-11-19 17:00:55 +01:00
|
|
|
"""Translate the string.
|
|
|
|
|
|
|
|
C the string internationalizer if any; else, use the local database"""
|
2014-01-22 10:54:40 +01:00
|
|
|
if string.__class__ == InternationalizedString:
|
2010-11-19 17:00:55 +01:00
|
|
|
return string._internationalizer(string.untranslated)
|
|
|
|
else:
|
|
|
|
return self.translations[string]
|
|
|
|
|
2010-11-01 11:42:33 +01:00
|
|
|
def _addTracker(self, string, untranslated):
|
2010-11-19 17:00:55 +01:00
|
|
|
"""Add a kind of 'tracker' on the string, in order to keep the
|
|
|
|
untranslated string (used when changing the locale)"""
|
2014-01-22 10:54:40 +01:00
|
|
|
if string.__class__ == InternationalizedString:
|
2010-11-19 17:00:55 +01:00
|
|
|
return string
|
|
|
|
else:
|
2014-01-22 10:54:40 +01:00
|
|
|
string = InternationalizedString(string)
|
2010-11-19 17:00:55 +01:00
|
|
|
string._original = untranslated
|
|
|
|
string._internationalizer = self
|
|
|
|
return string
|
2010-10-28 16:42:52 +02:00
|
|
|
|
2010-10-30 21:10:49 +02:00
|
|
|
def _loadL10nCode(self):
|
2010-11-19 17:00:55 +01:00
|
|
|
"""Open the file containing the code specific to this locale, and
|
|
|
|
load its functions."""
|
|
|
|
if self.name != 'supybot':
|
|
|
|
return
|
Start accelerating the 2to3 step (remove fix_apply, fix_buffer, fix_callable, fix_exec, fix_execfile, fix_exitfunc, fix_filter, fix_funcattrs, fix_future, fix_getcwdu, and fix_has_key).
2014-01-20 14:49:47 +01:00
|
|
|
path = self._getL10nCodePath()
|
2010-11-19 17:00:55 +01:00
|
|
|
try:
|
Start accelerating the 2to3 step (remove fix_apply, fix_buffer, fix_callable, fix_exec, fix_execfile, fix_exitfunc, fix_filter, fix_funcattrs, fix_future, fix_getcwdu, and fix_has_key).
2014-01-20 14:49:47 +01:00
|
|
|
with open(path) as fd:
|
|
|
|
exec(compile(fd.read(), path, 'exec'))
|
2010-11-19 17:00:55 +01:00
|
|
|
except IOError: # File doesn't exist
|
|
|
|
pass
|
|
|
|
|
|
|
|
functions = locals()
|
|
|
|
functions.pop('self')
|
|
|
|
self._l10nFunctions = functions
|
|
|
|
# Remove old functions and come back to the native language
|
|
|
|
|
2010-10-30 21:10:49 +02:00
|
|
|
def _getL10nCodePath(self):
|
2010-11-19 17:00:55 +01:00
|
|
|
"""Returns the path to the code localization file.
|
|
|
|
|
|
|
|
It contains functions that needs to by fully (code + strings)
|
|
|
|
localized"""
|
|
|
|
if self.name != 'supybot':
|
|
|
|
return
|
|
|
|
return getLocalePath('supybot', self.currentLocaleName, 'py')
|
|
|
|
|
2010-10-30 21:10:49 +02:00
|
|
|
def localizeFunction(self, name):
|
2010-11-19 17:00:55 +01:00
|
|
|
"""Returns the localized version of the function.
|
|
|
|
|
2014-01-22 11:04:08 +01:00
|
|
|
Should be used only by the InternationalizedFunction class"""
|
2010-11-19 17:00:55 +01:00
|
|
|
if self.name != 'supybot':
|
|
|
|
return
|
|
|
|
if hasattr(self, '_l10nFunctions') and \
|
Start accelerating the 2to3 step (remove fix_apply, fix_buffer, fix_callable, fix_exec, fix_execfile, fix_exitfunc, fix_filter, fix_funcattrs, fix_future, fix_getcwdu, and fix_has_key).
2014-01-20 14:49:47 +01:00
|
|
|
name in self._l10nFunctions:
|
2010-11-19 17:00:55 +01:00
|
|
|
return self._l10nFunctions[name]
|
|
|
|
|
2010-10-30 21:10:49 +02:00
|
|
|
def internationalizeFunction(self, name):
|
2010-11-19 17:00:55 +01:00
|
|
|
"""Decorates functions and internationalize their code.
|
|
|
|
|
|
|
|
Only useful for Supybot core functions"""
|
|
|
|
if self.name != 'supybot':
|
|
|
|
return
|
|
|
|
class FunctionInternationalizer:
|
|
|
|
def __init__(self, parent, name):
|
|
|
|
self._parent = parent
|
|
|
|
self._name = name
|
|
|
|
def __call__(self, obj):
|
2014-01-22 11:04:08 +01:00
|
|
|
obj = InternationalizedFunction(self._parent, self._name, obj)
|
2010-11-19 17:00:55 +01:00
|
|
|
obj.loadLocale()
|
|
|
|
return obj
|
|
|
|
return FunctionInternationalizer(self, name)
|
2010-10-30 21:10:49 +02:00
|
|
|
|
2014-01-22 11:04:08 +01:00
|
|
|
class InternationalizedFunction:
|
2010-10-30 21:41:25 +02:00
|
|
|
"""Proxy for functions that need to be fully localized.
|
|
|
|
|
2012-12-26 15:43:35 +01:00
|
|
|
The localization code is in locales/LOCALE.py"""
|
2010-10-30 21:10:49 +02:00
|
|
|
def __init__(self, internationalizer, name, function):
|
2010-11-19 17:00:55 +01:00
|
|
|
self._internationalizer = internationalizer
|
|
|
|
self._name = name
|
|
|
|
self._origin = function
|
2014-01-22 11:04:08 +01:00
|
|
|
internationalizedFunctions.append(self)
|
2010-10-30 21:10:49 +02:00
|
|
|
def loadLocale(self):
|
2010-11-19 17:00:55 +01:00
|
|
|
self.__call__ = self._internationalizer.localizeFunction(self._name)
|
|
|
|
if self.__call__ == None:
|
|
|
|
self.restore()
|
2010-10-30 21:10:49 +02:00
|
|
|
def restore(self):
|
2010-11-19 17:00:55 +01:00
|
|
|
self.__call__ = self._origin
|
2010-10-10 14:45:25 +02:00
|
|
|
|
2012-08-04 12:09:11 +02:00
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
return self._origin(*args, **kwargs)
|
|
|
|
|
2014-01-22 10:54:40 +01:00
|
|
|
class InternationalizedString(str):
|
2010-11-01 14:33:43 +01:00
|
|
|
"""Simple subclass to str, that allow to add attributes. Also used to
|
|
|
|
know if a string is already localized"""
|
2010-11-01 11:42:33 +01:00
|
|
|
pass
|
|
|
|
|
2010-10-10 14:45:25 +02:00
|
|
|
def internationalizeDocstring(obj):
|
2010-10-30 21:41:25 +02:00
|
|
|
"""Decorates functions and internationalize their docstring.
|
|
|
|
|
|
|
|
Only useful for commands (commands' docstring is displayed on IRC)"""
|
2010-11-01 19:57:18 +01:00
|
|
|
if obj.__doc__ == None:
|
2010-11-19 17:00:55 +01:00
|
|
|
return obj
|
2014-05-31 14:56:28 +02:00
|
|
|
plugin_module = sys.modules[obj.__module__]
|
|
|
|
if '_' in plugin_module.__dict__:
|
2010-11-19 17:00:55 +01:00
|
|
|
internationalizedCommands.update({hash(obj): obj})
|
|
|
|
try:
|
2014-05-31 14:56:28 +02:00
|
|
|
obj.__doc__ = plugin_module._.__call__(obj.__doc__)
|
2010-11-19 17:00:55 +01:00
|
|
|
# We use _.__call__() instead of _() because of a pygettext warning.
|
|
|
|
except AttributeError:
|
|
|
|
# attribute '__doc__' of 'type' objects is not writable
|
|
|
|
pass
|
2010-10-10 14:45:25 +02:00
|
|
|
return obj
|