Chemical Programmer
Chemical Programmer

Reputation: 4520

Django : Template translation not working

My web application's default language code is Korean.

I'd like to translate it into English when client's Accept-language is en-us.

I prefer to use template translation, not the python code or i18n_urlpatterns.

Here is my code.


Django version

    1.7.7

settings.py

    ...

    def ABS_PATH(*args):
        return os.path.join(PROJECT_DIR, *args)    

    LANGUAGE_CODE = 'ko'
    ugettext = lambda s: s
    LANGUAGES = (
        ('en', ugettext('English')),
        ('ko', ugettext('Korean')),
    )
    LOCALE_PATHS = (
        ABS_PATH('locale'),     # /var/www/mysite.com/locale
    )
    USE_I18N = True
    USE_L10N = True

    ...

    MIDDLEWARE_CLASSES = (
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.middleware.locale.LocaleMiddleware',
        'django.middleware.common.CommonMiddleware',
        ...
    )

    # `TEMPLATE_CONTEXT_PROCESSORS` have `django.core.context_processors.i18n` as default.

views.py

    def home(request):
        return render_to_response('index.html', locals(), context_instance=RequestContext(request))

index.html

    {% load i18n %}
    ...
    {% trans "Hello" %}
    ...

Make message file on shell

$ django-admin.py makemessages -l en


Edit message file /var/www/mysite.com/locale/en/LC_MESSAGES/django.po

    #: .../index.html:14
    msgid "Hello"
    msgstr "안녕"

Compile message file on shell

$ django-admin.py compilemessages

Check file at /var/www/mysite.com/locale/en/LC_MESSAGES/django.mo


Restart test server

$ manage.py runserver


Check through browser

Accept-language : en-US,en;q=0.8,ko;q=0.6

    Expectation : 안녕

    Result : Hello

What should I do to show English template when client browser's Accept-language header is en-us ?

Upvotes: 1

Views: 1033

Answers (1)

Chemical Programmer
Chemical Programmer

Reputation: 4520

Sorry, it was my mistake. How stupid I am ..

ABS_PATH('locale') returned /var/www/mysite.com/mysite/locale

Changing ABS_PATH('locale') into /var/www/mysite.com/locale solved problem

I hope above steps could be helpful for others.

Upvotes: 1

Related Questions