Hugo Yu
Hugo Yu

Reputation: 105

Django unable to translate language

I want to translate my website to CH, but no response after I followed the online instruction. Did I miss something?

I have changed the language code in settings.py.

settings.py:

import os
import django_heroku
from django.utils.translation import gettext_lazy as _

LANGUAGES = (
  ('en', ('English')),
  ('zh-Hant', _('Traditional Chinese')),
)
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
...
]
TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [TEMP_DIR],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.i18n',
            'django.template.context_processors.debug',
             ....]}

# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-Hant'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

LOCALE_PATHS = [
 os.path.join(BASE_DIR, 'locale')
]

Added the tag for translation in html

base.html:

{% load i18n %}
<!DOCTYPE html>
<html>
....

home.html:

{% extends "base.html" %}
{% load i18n %}
{% block content %}
{% trans "Let's translate this" %}
....

Updated msgstr in ....\locale\zh-Hant\LC_MESSAGESdjango.po:

msgid "Let's translate this"
msgstr "來翻譯這個"

Upvotes: 0

Views: 1010

Answers (2)

NKSM
NKSM

Reputation: 5854

Updated msgstr in ....\locale\zh-Hant\LC_MESSAGESdjango.po:

For traditional Chinese:

zh-hant in your config, and your directory should be named zh_Hant.

See the locale directory: https://github.com/django/django/tree/master/django/contrib/auth/locale

locale name

A locale name, either a language specification of the form ll or a combined language and country specification of the form ll_CC. Examples: it, de_AT, es, pt_BR, sr_Latn. The language part is always in lowercase. The country part is in titlecase if it has more than 2 characters, otherwise it’s in uppercase. The separator is an underscore.

and languages keys with lowercase:

LANGUAGES = (
  ('en', _('English')),
  ('zh-hant', _('Traditional Chinese')),
)

Upvotes: 1

markwalker_
markwalker_

Reputation: 12849

Change your language code so that you have LANGUAGES like this;

LANGUAGES = (
    ('en', gettext('English')),
    ('ja', gettext('Japanese')),
    ('it', gettext('Italian')),
    ('zh-hant', gettext('Chinese')),
)

I've tested this on a multi-lingual site that I have.

python manage.py makemessages -l zh-hant
python manage.py compilemessages

Django admin then looks like this by default;

enter image description here

Upvotes: 0

Related Questions