grnet/ganetimgr

LANGUAGE_CODE = 'ru-RU' returns AttributeError: 'NoneType' object has no attribute '_info'

Closed this issue · 15 comments

Dear devs,

Testing v1.6.0 with localization to Russian.
Changing settings.py
LANGUAGE_CODE = 'ru-Ru'
returns an AttributeError: 'NoneType' object has no attribute '_info'

The issue #51 seems to related to this one.

I found one solution which is specific to Django Version 1.4.5:
https://code.djangoproject.com/ticket/18192
where one has to fix the file
ganetimgr/lib/python2.7/site-packages/django/utils/translation/trans_real.py
like this:

def _translation(path):
            try:
                t = gettext_module.translation('django', path, [loc], DjangoTranslation)
                t.set_language(lang)
                return t
            except IOError:
                return None

        res = _translation(globalpath)

        ### salsa: THIS CODE I have to add before running syncdb
        if res is None:
            return gettext_module.NullTranslations()

        # We want to ensure that, for example,  "en-gb" and "en-us" don't share
        # the same translation object (thus, merging en-us with a local update
        # doesn't affect en-gb), even though they will both use the core "en"
        # translation. So we have to subvert Python's internal gettext caching.
        base_lang = lambda x: x.split('-', 1)[0]
        if base_lang(lang) in [base_lang(trans) for trans in _translations]:
            res._info = res._info.copy()   ### salsa: HERE it throws AttributeError 
            res._catalog = res._catalog.copy()
safts commented

Let me check this. I will get back to you as soon as possible.

I use these settings:

LANGUAGES = (
    ('ru', u'Russian'),
    ('en', _('English')),
)

LANGUAGE_CODE = 'ru-RU'

For simplicity copy tranlation files:

cp -r ganetimgr/locale/el ganetimgr/locale/ru

After configuration and startup of all the services I get error on the debug page:

AttributeError at /user/login/

'NoneType' object has no attribute '_info'

Request Method:     GET
Request URL:    http://xx.xx.xxx.xx/user/login/
Django Version:     1.4.5
Exception Type:     AttributeError
Exception Value:    

'NoneType' object has no attribute '_info'

Exception Location:     /opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/utils/translation/trans_real.py in _fetch, line 148
Python Executable:  /opt/skifport/embedded/service/ganetimgr/bin/python
Python Version:     2.7.9
Python Path:    

['/opt/skifport/embedded/service/ganetimgr/ganetimgr',
 '/opt/skifport/embedded/service/ganetimgr/bin',
 '/opt/skifport/embedded/service/ganetimgr/lib/python27.zip',
 '/opt/skifport/embedded/service/ganetimgr/lib/python2.7',
 '/opt/skifport/embedded/service/ganetimgr/lib/python2.7/plat-linux2',
 '/opt/skifport/embedded/service/ganetimgr/lib/python2.7/lib-tk',
 '/opt/skifport/embedded/service/ganetimgr/lib/python2.7/lib-old',
 '/opt/skifport/embedded/service/ganetimgr/lib/python2.7/lib-dynload',
 '/opt/skifport/embedded/lib/python2.7',
 '/opt/skifport/embedded/lib/python2.7/plat-linux2',
 '/opt/skifport/embedded/lib/python2.7/lib-tk',
 '/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages']

Server time:    Tue, 7 Jun 2016 19:24:49 +0300
Environment:


Request Method: GET
Request URL: http://xx.xx.xxx.xx/user/login/

Django Version: 1.4.5
Python Version: 2.7.9
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.flatpages',
 'django.contrib.messages',
 'django.contrib.admin',
 'django.contrib.staticfiles',
 'django_markdown',
 'accounts',
 'south',
 'registration',
 'ganeti',
 'apply',
 'notifications',
 'stats',
 'auditlog')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'middleware.ForceLogout.ForceLogoutMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
 'middleware.UserMessages.UserMessageMiddleware')


Traceback:
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  89.                     response = middleware_method(request)
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/middleware/locale.py" in process_request
  24.         translation.activate(language)
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/utils/translation/__init__.py" in activate
  105.     return _trans.activate(language)
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/utils/translation/trans_real.py" in activate
  195.     _active.value = translation(language)
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/utils/translation/trans_real.py" in translation
  185.     current_translation = _fetch(language, fallback=default_translation)
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/utils/translation/trans_real.py" in _fetch
  148.             res._info = res._info.copy()

Exception Type: AttributeError at /user/login/
Exception Value: 'NoneType' object has no attribute '_info'

There is something clearly wrong when using default English language with LANGUAGE_CODE = 'ru-RU'
Translation method in trans_real.py throws an error having empty res.

I had a half-working config with:

LANGUAGES = (
    ('ru', u'Russian'),
    ('el', u'Ελληνικά'),
    ('en', _('English')),
)

LANGUAGE_CODE = 'en-us'

All the two languages were showing properly.
But when selecting English it returned the same AttributeError in _fetch method which means res = _translation(globalpath) is None.

Then editing trans_real.py with the fix described in first message of this issue I did have all languages to show English default translation.

Hope that helps.

I switch my installation to this config for now which works fine for me:

LANGUAGES = (
)

LANGUAGE_CODE = 'ru-RU'

Have to go on with translation. Will come back to this as long as you find something.
Cheers!

Oh, well! It seems that using only LANGUAGE_CODE = 'ru-RU' doesn't work properly in the admin panel when adding config items although it works everywhere in the web-interface.

AttributeError at /admin/ganeti/cluster/add/

'NoneType' object has no attribute '_info'

Request Method:     GET
Request URL:    http://xx.xx.xxx.xx/admin/ganeti/cluster/add/
Django Version:     1.4.5
Exception Type:     AttributeError
Exception Value:    

'NoneType' object has no attribute '_info'

the same is for http://xx.xx.xxx.xx/admin/ganeti/network/add/ and all other "add" operations.

hope that helps.

So far I found out collision using this config with Greek language:

LANGUAGES = (
    ('el', u'Ελληνικά'),
    ('en', _('English')),
)

LANGUAGE_CODE = 'el-GR'

If English is used everything is fine. If Greek is set it throws the same AttributeError.

safts commented

Just to make sure.. Are you certain your locale files are in the directories specified in your LOCALE_PATHS setting?

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)
root@dev1:~# ls /opt/skifport/embedded/service/ganetimgr/ganetimgr/locale/
el  ru

Greek / Russian translations work as expected except the case when LANGUAGE_CODE = 'el-GR' / 'ru-RU'

Did you manage to reproduce this AttributeError?

safts commented

No, not yet. I am using the following

LANGUAGES = (
    ('ru', u'Russian'),
    ('el', u'Ελληνικά'),
    ('en', _('English')),
)

LANGUAGE_CODE = 'en-us'

I can confirm that I do not get the AttributeError you are describing. Also, I switched to LANGUAGE_CODE='el-GR' and still no error.
I can also confirm that translations work in admin as expected.

It seems a bit weird that changing LANGUAGE_CODE setting has an effect on your project, as:
(django docs)

It serves two purposes:
If the locale middleware isn’t in use, it decides which translation is served to all users.
If the locale middleware is active, it provides a fallback language in case the user’s preferred language can’t be determined or is not supported by the website. It also provides the fallback translation when a translation for a given literal doesn’t exist for the user’s preferred language.

Are you sure you have correctly enabled the referenced middleware? Have you tried ommiting LANGUAGE_CODE entirely?

Please give me some more information about the versions of the packages you have installed and the installation procedure you followed.

I follow instructions.

All the funcionality of GanetiMGR works perfect. The referenced middleware is enabled.

The software with all the requirements are installed in virtualenv.

Do you use v1.6.0 ?

safts commented

We are currently using the master branch (this is stable). As far as I am concerned though, there are no changes in language/locale between v1.6 and the master branch.

I will check the installation procedures.

The main difference is that I install into virtualenv and use PostgreSQL (but again everything works fine except translations).

Installation procedures seem to be correct. A working application also proves it.

I think I got close to the problem.

Some debug info first:

Environment:

Request Method: GET
Request URL: http://xx.xx.xxx.xx/admin/ganeti/cluster/add/

Django Version: 1.4.5
Python Version: 2.7.9
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.flatpages',
 'django.contrib.messages',
 'django.contrib.admin',
 'django.contrib.staticfiles',
 'django_markdown',
 'accounts',
 'south',
 'registration',
 'ganeti',
 'apply',
 'notifications',
 'stats',
 'auditlog')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'middleware.ForceLogout.ForceLogoutMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
 'middleware.UserMessages.UserMessageMiddleware')


Traceback:
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
  366.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  89.         response = view_func(request, *args, **kwargs)
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
  196.             return view(request, *args, **kwargs)
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  25.             return bound_func(*args, **kwargs)
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  21.                 return func(self, *args2, **kwargs2)
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/db/transaction.py" in inner
  224.                 return func(*args, **kwargs)
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view
  1008.         return self.render_change_form(request, context, form_url=form_url, add=True)
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/contrib/admin/options.py" in render_change_form
  750.             'content_type_id': ContentType.objects.get_for_model(self.model).id,
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/contrib/contenttypes/models.py" in get_for_model
  42.                 defaults = {'name': smart_unicode(opts.verbose_name_raw)},
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/db/models/options.py" in verbose_name_raw
  212.         activate(lang)
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/utils/translation/__init__.py" in activate
  105.     return _trans.activate(language)
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/utils/translation/trans_real.py" in activate
  199.     _active.value = translation(language)
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/utils/translation/trans_real.py" in translation
  189.     current_translation = _fetch(language, fallback=default_translation)
File "/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/utils/translation/trans_real.py" in _fetch
  152.             res._info = res._info.copy()

Exception Type: AttributeError at /admin/ganeti/cluster/add/
Exception Value: 'NoneType' object has no attribute '_info'

Then I see debug info:

/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/db/models/options.py in verbose_name_raw
212.        activate(lang)

 Local vars
Variable    Value
lang    'ru-ru'
raw     u'cluster'
self    <Options for Cluster>

/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/utils/translation/__init__.py in activate
105.    return _trans.activate(language)
Local vars
Variable    Value
language    'ru-ru'

/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/utils/translation/trans_real.py in activate
199.    _active.value = translation(language)
Local vars
Variable    Value
language    'ru-ru'

/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/utils/translation/trans_real.py in translation
189.     current_translation = _fetch(language, fallback=default_translation)

Local vars
Variable    Value
default_translation     <DjangoTranslation lang:ru-RU>
language    'ru-ru'
settings    <django.conf.LazySettings object at 0x7ff848b4d310>
projectpath     '/opt/skifport/embedded/service/ganetimgr/ganetimgr/ganetimgr/locale'
globalpath  '/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/conf/locale'
project     <module 'ganetimgr' from '/opt/skifport/embedded/service/ganetimgr/ganetimgr/ganetimgr/__init__.pyc'>
parts   ['ganetimgr', 'settings']
t   None
_fetch  <function _fetch at 0x7ff842039a28>


/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/utils/translation/trans_real.py in _fetch
152.             res._info = res._info.copy()
Local vars
Variable    Value
lang    'ru-ru'
loc     'ru_RU'
settings    <django.conf.LazySettings object at 0x7ff848b4d310>
res     None
globalpath  '/opt/skifport/embedded/service/ganetimgr/lib/python2.7/site-packages/django/conf/locale'
_translation    <function _translation at 0x7ff842039aa0>
projectpath     '/opt/skifport/embedded/service/ganetimgr/ganetimgr/ganetimgr/locale'
fallback    <DjangoTranslation lang:ru-RU>
trans   'ru-RU'
base_lang   <function <lambda> at 0x7ff8420399b0>

So I change settings.py to

LANGUAGES = (
    ('en', _('English')),
    ('ru', u'Russian'),
    ('el', u'Ελληνικά'),
)

LANGUAGE_CODE = 'ru'

And everything works fine except an issue #51
Add functions in admin panel work as expected ( http://xx.xx.xxx.xx/admin/ganeti/cluster/add/ ), translations are made for every language except the default English.

This is a strange behavior but it seems to be specific to Django 1.4 the application depends on.

So the final solution for me with respect to an issue #51

LANGUAGES = (
    ('en', u'English'),
    ('ru', u'Russian'),
    ('el', u'Ελληνικά'),
)

LANGUAGE_CODE = 'ru'

And making a directory with translation files:
ganetimgr/locale/en/LC_MESSAGES/django.*
which I will have to support. weird though :)

Thank you for being responsive!