From e8bc966827fbeecb082d39b7009c655e26c70641 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Thu, 28 Oct 2010 17:28:27 +0200 Subject: [PATCH] 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 --- src/i18n.py | 32 ++++++++++++++++++++++++++------ src/utils/str.py | 11 ++++++++--- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/i18n.py b/src/i18n.py index 60a6a3190..1236d2a74 100644 --- a/src/i18n.py +++ b/src/i18n.py @@ -67,6 +67,14 @@ def get_plugin_dir(plugin_name): return filename[0:-len(allowed_file)] 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 = {} internationalizedCommands = {} @@ -93,13 +101,11 @@ class PluginInternationalization: elif localeName is None: localeName = 'en' 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: - 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 self.translations = {} return @@ -175,6 +181,20 @@ class PluginInternationalization: except KeyError: 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): # FIXME: check if the plugin has an _ object diff --git a/src/utils/str.py b/src/utils/str.py index 2c6fef345..73be108ba 100644 --- a/src/utils/str.py +++ b/src/utils/str.py @@ -42,6 +42,9 @@ import textwrap from iter import all, any from structures import TwoWayDictionary +from supybot.i18n import PluginInternationalization +_ = PluginInternationalization() + curry = new.instancemethod chars = string.maketrans('', '') @@ -253,12 +256,12 @@ def matchCase(s1, s2): L[i] = L[i].upper() return ''.join(L) -consonants = 'bcdfghjklmnpqrstvwxz' -_pluralizeRegex = re.compile('[%s]y$' % consonants) def pluralize(s): """Returns the plural of s. Put any exceptions to the general English rule of appending 's' in the plurals dictionary. """ + consonants = 'bcdfghjklmnpqrstvwxz' + _pluralizeRegex = re.compile('[%s]y$' % consonants) lowered = s.lower() # Exception dictionary if lowered in plurals: @@ -275,9 +278,9 @@ def pluralize(s): else: return matchCase(s, s+'s') -_depluralizeRegex = re.compile('[%s]ies' % consonants) def depluralize(s): """Returns the singular of s.""" + _depluralizeRegex = re.compile('[%s]ies' % consonants) lowered = s.lower() if lowered in plurals: return matchCase(s, plurals[lowered]) @@ -291,6 +294,8 @@ def depluralize(s): else: return s # Don't know what to do. +pluralize, depluralize = _.getPluralizers(pluralize, depluralize) + def nItems(n, item, between=None): """Works like this: