[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Need to pass a class instance to a gettext fallback


I'm working with gettext and need to define a language Fallback. I got this working, but with a global variable. I don't really like this and I would like to pass this variable to the gettext Fallback's contructor, but I don't know how. For simplicity, I won't put the whole code here, just the important parts.

Before you look at it, I want to ask you: how can I pass the variable: "my_plugin" to the constructor of the "MissingTranslationsFallback" class? If you see, an instance of this class will be created automatically by gettext. I don't create it. When calling the " add_fallback" method of the gettext.GNUTranslations class, you have to pass a class and not an instance.

#Defining a global dict, which is ugly, I know

class TranslationService(object):
  """...__init__ and other methods are defined here"""

  def install(self):
    """...some code goes here..."""
    #Now the ugly part :-(
    MY_GLOBALS['py_plugin'] = self._py_plugin
    current_catalog = gettext.translation(plugin_name, localedir = locale_folder,
        class_= MissingTranslationsFallback, languages = [current_language])

class MissingTranslationsFallback(gettext.GNUTranslations, object):
  def __init__(self, *args, **kwargs):
    super(MissingTranslationsFallback, self).__init__(*args, **kwargs)

    #And here is where I need to access py_plugin :-(
    py_plugin = MY_GLOBALS['py_plugin']
    i18n_service = py_plugin.get_i18n_service()
    #Adds an instance to the class that will handle the missing translations

class MissingTranslationsLogger(gettext.NullTranslations):
  def __init__(self, language):
    self._language = language
    self._missing = set()

  def gettext(self, message):
    if (message not in self._missing):
      print "Missing message: " + message + " current language: " + self._language
    return message

Any help would be appreciated.

Best regards