potemkin
potemkin

Reputation: 31

how to add a language in Django that its not supported by default?

Im trying to make a website in 5 languages. The main language should be kurdish but it is not supported by Django default. I tried already the how to add new languages into Django?

but it didnt work for me. I receive an error mesagge

**LANG_INFO = dict(django.conf.locale.LANG_INFO.items() + EXTRA_LANG_INFO.items())

TypeError: unsupported operand type(s) for +: 'dict_items' and 'dict_items'**

I also tried already to copy an english po file and rename it "ku" (kurdish) and i added into django/conf/init.py the language info.

'ku': {
    'bidi': False,
    'code': 'ku',
    'name': 'Kurdish',
    'name_local': 'Kurdî',
},

I can see the language by languages option by template but when i click it i receive an error message.

File "/usr/lib/python3.6/gettext.py", line 91, in _tokenize
raise ValueError('invalid token in plural form: %s' % value)

ValueError: invalid token in plural form: EXPRESSION

Does anybody know how can i fix it? Thanks so much!

Upvotes: 2

Views: 1236

Answers (2)

chrisbunney
chrisbunney

Reputation: 5946

ValueError: invalid token in plural form: EXPRESSION is probably caused by an incorrect Plural-Forms value in the .po header.

Each .po file should have a header at the top that looks something like this:

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-02-26 12:51+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

The Plural-Forms tells the translation tools how this particular language pluralises the singular. Specifically how many different ways there are of forming plurals. This is used to generate the correct number of msgstr entries for translating strings involving plurals.

The value you have includes the invalid string EXPRESSION, which suggests it's just a placeholder and you haven't set the correct plural form.

This webpage lists the plural forms expression for most if not all languages: https://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html

According to the site, the correct value for Kurdish is nplurals=2; plural=(n != 1);

Upvotes: 0

ThunderHorn
ThunderHorn

Reputation: 2035

in your settings.py

LANGUAGE_CODE = 'ku'
LANGUAGES = [('en', 'English'), ('ku', 'Kurdish')]

add 'django.middleware.locale.LocaleMiddleware' in your settings.MIDDLEWARE

Add LOCALE_PATHS in settings.py, this is where your translation files will be stored:

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

Enable i18N

USE_I18N = True

in settings.TEMPLATES add

'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'social_django.context_processors.backends',
                'social_django.context_processors.login_redirect',
            ],
        },

in urls.py add

url(r'^i18n/', include('django.conf.urls.i18n')),

then in the template you will have let's say 'Hello world'

you must load il18n

{% load i18n %}
<p>{% trans "cîhana hello" %}</p>

then in the console you must make the messages, it will take all strings with the tag trans and place it in your locale folder

./manage.py makemessages -l en #you will translate it to en since your default language is 'ku'

And now all is left is to go into your /locales folder, and edit each of the .po files. Fill in the data for each msgstr. Here is one such example of that:

msgid "cîhana hello"
msgstr "Hello world"

then you run ./manage.py compilemessages

and everything will work like a charm

if you need more languages you can go

   LANGUAGES = [('en', 'English'), ('ku', 'Kurdish', ('de', 'Deutsch'), ('ru', 'Russian'), ('fr', 'French'))]

every time you must edit the messages in

locale/[language-code]/LC_MESSAGES/django.po

#expect to find something like this

#: templates/index.html:35
msgid "Email"
msgstr "Имейл"

Upvotes: 1

Related Questions