2009-08-04 9 views
9

Mam plik ustawień narodowych/es/LC_MESSAGES/django.mo (i .po), uruchomiłem makreessages i kompilezje. Zdecydowanie wszystkie wiadomości są tłumaczoneNie można zmienić środowiska językowego w Django

w settings.py mają:

USE_I18N = True 
LANGUAGE_CODE = 'es' 

Wciąż django uparcie trwa ciągi z angielskiego plik .po ... Dlaczego to może być?

Musi być trochę grosz ... Dzięki.

EDIT wydaje się, że tak jest tylko wtedy, gdy aktywne jest LocaleMiddleware.

Odpowiedz

9

Zgodnie z docs django

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

LocaleMiddleware próbuje określić preferencję użytkownika użytkownika przez naśladują tego algorytmu:

* 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. 

Jeśli potrzebne jest tylko jeden język, 'es', można wyłączyć middleware. Jeśli naprawdę potrzebujesz LocaleMiddleware aktywny, spróbuj tego przepisu przesłonić nagłówki z przeglądarki klienta 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

dla osób korzystających Pythona 3.x, 'jeśli request.META.has_key („HTTP_ACCEPT_LANGUAGE”):' potrzeby należy zmienić na 'if 'HTTP_ACCEPT_LANGUAGE' w request.META:'. – reinaldoluckman

0

Używam tego w moich ustawień:

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

Więc należy użyć coś jak 'es-es'

Powiązane problemy