doniyor
doniyor

Reputation: 37914

django {% trans "Hello" %} isnot working

I know, this thing was asked like trillion times, but i cannot still get my django templates translated.

I created locale folder in project tree.

I added in settings.py

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

settings.py default language is English:

LANGUAGE_CODE = 'en-us'

I added more languages like:

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

and added into TEMPLATE_CONTEXT_PROCESSORS

'django.core.context_processors.i18n',

and into 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" (it is german) and did

python manage.py compilemessages

it created .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: first of all, thanks to Liarez for standing by me during this horrible time, I finally found my mistake.

I was doing:

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

which went one step deeper in project tree where there are settings.py.

i changed this to

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

and it is working like in fairy tales.

Upvotes: 3

Views: 87

Answers (1)

AlvaroAV
AlvaroAV

Reputation: 10563

This is an easy way to manage languages, and change active language via URL:

in the urls.py add:

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

In your views.py:

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

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

In any template:

You should ask (just for testing) in any place of your template: {{LANGUAGE_CODE}} to know wich is the actual language and check if translations are working

Upvotes: 1

Related Questions