Add locale abitrary code

Add the capability for Supybot to load code depending on the locale
src/utils/str.py now imports pluralize and depluralize from this code
This commit is contained in:
Valentin Lorentz 2010-10-28 17:28:27 +02:00
parent 97f51ab08e
commit e8bc966827
2 changed files with 34 additions and 9 deletions

View File

@ -67,6 +67,14 @@ def get_plugin_dir(plugin_name):
return filename[0:-len(allowed_file)] return filename[0:-len(allowed_file)]
return return
def getLocalePath(name, localeName, extension):
if name != 'supybot':
directory = get_plugin_dir(name) + 'locale'
else:
import ansi # Any Supybot plugin could fit
directory = ansi.__file__[0:-len('ansi.pyc')] + 'locale'
return '%s/%s.%s' % (directory, localeName, extension)
i18nClasses = {} i18nClasses = {}
internationalizedCommands = {} internationalizedCommands = {}
@ -93,13 +101,11 @@ class PluginInternationalization:
elif localeName is None: elif localeName is None:
localeName = 'en' localeName = 'en'
self.currentLocaleName = localeName self.currentLocaleName = localeName
if self.name != 'supybot':
directory = get_plugin_dir(self.name) + 'locale'
filename = '%s/%s.po' % (directory, localeName)
else:
filename = 'locale/%s.po' % localeName
try: try:
translationFile = open(filename, 'ru') translationFile = open(getLocalePath(self.name, localeName, 'po'),
'ru') # ru is the mode, not the beginning
# of 'russian' ;)
except IOError: # The translation is unavailable except IOError: # The translation is unavailable
self.translations = {} self.translations = {}
return return
@ -175,6 +181,20 @@ class PluginInternationalization:
except KeyError: except KeyError:
return untranslated % args return untranslated % args
def _getL10nCode(self):
return getLocalePath('supybot', self.currentLocaleName, 'py')
def getPluralizers(self, current_pluralize, current_depluralize):
# This should be used only by src/utils/str.py
try:
execfile(self._getL10nCode())
except IOError:
pass # Handled by the else v-
if locals().has_key('pluralize') and locals().has_key('depluralize'):
return (pluralize, depluralize)
else:
return (current_pluralize, current_depluralize)
def internationalizeDocstring(obj): def internationalizeDocstring(obj):
# FIXME: check if the plugin has an _ object # FIXME: check if the plugin has an _ object

View File

@ -42,6 +42,9 @@ import textwrap
from iter import all, any from iter import all, any
from structures import TwoWayDictionary from structures import TwoWayDictionary
from supybot.i18n import PluginInternationalization
_ = PluginInternationalization()
curry = new.instancemethod curry = new.instancemethod
chars = string.maketrans('', '') chars = string.maketrans('', '')
@ -253,12 +256,12 @@ def matchCase(s1, s2):
L[i] = L[i].upper() L[i] = L[i].upper()
return ''.join(L) return ''.join(L)
consonants = 'bcdfghjklmnpqrstvwxz'
_pluralizeRegex = re.compile('[%s]y$' % consonants)
def pluralize(s): def pluralize(s):
"""Returns the plural of s. Put any exceptions to the general English """Returns the plural of s. Put any exceptions to the general English
rule of appending 's' in the plurals dictionary. rule of appending 's' in the plurals dictionary.
""" """
consonants = 'bcdfghjklmnpqrstvwxz'
_pluralizeRegex = re.compile('[%s]y$' % consonants)
lowered = s.lower() lowered = s.lower()
# Exception dictionary # Exception dictionary
if lowered in plurals: if lowered in plurals:
@ -275,9 +278,9 @@ def pluralize(s):
else: else:
return matchCase(s, s+'s') return matchCase(s, s+'s')
_depluralizeRegex = re.compile('[%s]ies' % consonants)
def depluralize(s): def depluralize(s):
"""Returns the singular of s.""" """Returns the singular of s."""
_depluralizeRegex = re.compile('[%s]ies' % consonants)
lowered = s.lower() lowered = s.lower()
if lowered in plurals: if lowered in plurals:
return matchCase(s, plurals[lowered]) return matchCase(s, plurals[lowered])
@ -291,6 +294,8 @@ def depluralize(s):
else: else:
return s # Don't know what to do. return s # Don't know what to do.
pluralize, depluralize = _.getPluralizers(pluralize, depluralize)
def nItems(n, item, between=None): def nItems(n, item, between=None):
"""Works like this: """Works like this: