Django {% trans "Hello"%} doesn't work

I know this thing has been asked a trillion times, but I cannot translate my django templates yet.

I have created a folder locale

in the project tree.

I added to settings.py

LOCALE_PATHS = (
  os.path.join(PROJECT_PATH,'locale'),
)

      

settings.py default language is English:

LANGUAGE_CODE = 'en-us'

      

I added some more languages:

LANGUAGES = ( 
  ('de', _('German')),
  ('fr', _('French')),
  ('es', _('Spanish')),
  ('pt', _('Portuguese'))
)

      

and added to TEMPLATE_CONTEXT_PROCESSORS

'django.core.context_processors.i18n',

      

and in MIDDLEWARE_CLASSES

'django.middleware.locale.LocaleMiddleware',

      

NOW : I added this to my index.html

{% load i18n %}
{% trans "it is me" as me %}
<title>Newsportal {{ me }}</title>

      

and did:

python manage.py makemessages -a

      

and translated "it is me"

into "das bin ich"

(he is German) and did

python manage.py compilemessages

      

it created a .mo file. everything looks fantastic

and I changed the language of my Chrome browser to German.

BUT : it still shows the text as "it is me"

.

what am I doing wrong?

SOLUTION : Firstly, thanks to Lares for standing with me at this terrible time, I finally found my mistake.

I did:

PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
LOCALE_PATHS = (
   os.path.join(PROJECT_PATH, 'locale'),
)

      

which went one step deeper in the project tree where settings.py is.

i changed this to

LOCALE_PATHS = (
   os.path.realpath('locale'),
) 

      

and it works like in fairy tales.

+3


source to share


1 answer


This is an easy way to manage languages ​​and change the active language by url:

in urls.py add:

url(r'^set_language/(?P<language_code>[\w-]+)/?', 'YOUR_PROJECT.views.set_language', name='set_language'),

      


In your view.py:



def set_language(request, language_code):
    ''' Change language '''

    translation.activate(language_code)
    return HttpResponseRedirect('/')

      


In any template:

You should ask (just for testing) anywhere in your template: {{LANGUAGE_CODE}}

know which language is the actual one and check if the translations work.

+1


source







All Articles