2009-08-04 11 views
9

Ho file locale/es/LC_MESSAGES/django.mo (e .po), eseguito makemessages e compilemessages. Sicuramente tutti i messaggi vengono convertitiImpossibile cambiare l'ambiente della lingua in Django

in settings.py hanno:

USE_I18N = True 
LANGUAGE_CODE = 'es' 

Ancora Django prende ostinatamente le stringhe dal file .po inglese ... Perché può essere?

Deve esserci qualche trucco ... Grazie.

EDIT questo sembra essere il caso solo quando LocaleMiddleware è attivo.

risposta

9

Secondo la documentazione Django

http://docs.djangoproject.com/en/dev/topics/i18n/#id2

LocaleMiddleware prova a determinare la lingua preferita dell'utente da seguendo questo algoritmo:

* First, it looks for a django_language key in the current user's session. 

* Failing that, it looks for a cookie. 

[...]

*Failing that, it looks at the Accept-Language HTTP header. This header is sent by your browser and tells the server which language(s) you prefer, in order by priority. Django > tries each language in the header until it finds one with available translations. 

* Failing that, it uses the global LANGUAGE_CODE setting. 

Se avete solo bisogno una lingua, 'es', è possibile disattivare il middleware. Se si ha realmente bisogno LocaleMiddleware attiva, provare questa ricetta per ignorare le intestazioni dal browser del client http://www.djangosnippets.org/snippets/218/:

enter code here 

class ForceDefaultLanguageMiddleware(object): 
    """ 
    Ignore Accept-Language HTTP headers 

    This will force the I18N machinery to always choose settings.LANGUAGE_CODE 
    as the default initial language, unless another one is set via sessions or cookies 

    Should be installed *before* any middleware that checks request.META['HTTP_ACCEPT_LANGUAGE'], 
    namely django.middleware.locale.LocaleMiddleware 
    """ 
    def process_request(self, request): 
     if request.META.has_key('HTTP_ACCEPT_LANGUAGE'): 
      del request.META['HTTP_ACCEPT_LANGUAGE'] 
+0

Per coloro che utilizzano Python 3.X, 'se request.META.has_key ('HTTP_ACCEPT_LANGUAGE'):' esigenze da modificare in 'se 'HTTP_ACCEPT_LANGUAGE' in request.META:'. – reinaldoluckman

0

Io uso questo nelle mie impostazioni:

TIME_ZONE = 'Europe/Paris' 
LANGUAGE_CODE = 'fr-FR' 
SITE_ID = 1 
USE_I18N = True 

così si dovrebbe usare qualcosa come 'es-ES'

Problemi correlati