Need to pass a class instance to a gettext fallback
Josef Meile
jmeile at hotmail.com
Fri Sep 8 04:22:14 EDT 2017
Hi Peter
Your code worked, but I did some changes. First of all: I decided that I don't really needed to pass the py_plugin instance. I only needed an attribute called: self._language. Anyway, if I passed it without doing anything else, I got:
TypeError: __init__() got an unexpected keyword argument 'language'
In this call inside the MissingTranslationsFallback class.
super(MissingTranslationsFallback, self).__init__(*args, **kwargs)
The error is clear, the constructor of: "gettext.GNUTranslations" isn't expecting an argument called: 'language', so, I removed it before doing the call:
language = kwargs['language']
del kwargs['language']
super(MissingTranslationsFallback, self).__init__(*args, **kwargs)
And it worked. Thanks a lot for your help. Now it looks nice without those nasty global variables :-)
Here the resulting code:
class TranslationService(object):
"""...__init__ and other methods are defined here"""
def install(self):
"""...some code goes here..."""
current_catalog = gettext.translation(plugin_name, localedir = locale_folder,
class_= functools.partial(
MissingTranslationsFallback,
language = self._language
),
languages = [current_language]
)
current_catalog.install()
class MissingTranslationsFallback(gettext.GNUTranslations, object):
def __init__(self, *args, **kwargs):
language = kwargs['language']
del kwargs['language']
super(MissingTranslationsFallback, self).__init__(*args, **kwargs)
self.add_fallback(MissingTranslationsLogger(language))
Best regards
Josef
-----Original Message-----
From: Python-list [mailto:python-list-bounces+jmeile=hotmail.com at python.org] On Behalf Of Peter Otten
Sent: Freitag, 8. September 2017 08:15
To: python-list at python.org
Subject: Re: Need to pass a class instance to a gettext fallback
Josef Meile wrote:
> Hi
>
> 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.
Provided the class_ argument to gettext.translation() accepts an arbitrary callable the following may work:
import functools
> #Defining a global dict, which is ugly, I know MY_GLOBALS = {}
>
> class TranslationService(object):
> """...__init__ and other methods are defined here"""
>
> def install(self):
> """...some code goes here..."""
current_catalog = gettext.translation(
plugin_name,
localedir=locale_folder,
class_=functools.partial(
MissingTranslationsFallback,
py_plugin=self._py_plugin
),
languages=[current_language]
)
> current_catalog.install()
>
> class MissingTranslationsFallback(gettext.GNUTranslations, object):
> def __init__(self, *args, **kwargs):
py_plugin = kwargs.pop("py_plugin")
> super(MissingTranslationsFallback, self).__init__(*args, **kwargs)
> i18n_service = py_plugin.get_i18n_service()
> #Adds an instance to the class that will handle the missing
> #translations
>
self.add_fallback(MissingTranslationsLogger(i18n_service.get_language()))
--
https://mail.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list