1

I am having problem with adding my custom languages in Django. Here is the configuration of the settings.py:

LANGUAGES = [
('en', 'English'),
('ru', 'Russian'),
('uz', 'Uzbek'),
]

EXTRA_LANG_INFO = {
'uz': {
    'bidi': False,
    'code': 'uz',
    'name': 'Uzbek',
    'name_local': "O'zbek",
},
}

import django.conf.locale
LANG_INFO = dict(django.conf.locale.LANG_INFO.items() +   EXTRA_LANG_INFO.items())
django.conf.locale.LANG_INFO = LANG_INFO
global_settings.LANGUAGES = global_settings.LANGUAGES + [("uz", 'Uzbek')]

but I am having the following errror:

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

I am using django version 2.1 and pycharm as an IDE on ubuntu 18.04. Actually according to the instructions it should work.

ayhan
  • 70,170
  • 20
  • 182
  • 203
Major Mail
  • 78
  • 8
  • 3
    Possible duplicate of [How to merge two dictionaries in a single expression?](https://stackoverflow.com/questions/38987/how-to-merge-two-dictionaries-in-a-single-expression) – ayhan Oct 02 '18 at 10:34
  • 1
    That method (summing two `.items()`) doesn't work on Python 3. See the duplicate for alternatives. Probably the easiest one is to use update: `LANG_INFO.update(EXTRA_LANG_INFO)` – ayhan Oct 02 '18 at 10:35

1 Answers1

1

Basically you do not have to first convert the LANG_INFO dictionary into a key-value pair list and then add them together. What you can do is simply to create a new dictionary with old and new key-value pair dictionaries. I would also recommend decorating name in LANGUAGES for future translations. I am writing your code in the modified version below:

from django.utils.translation import gettext_noop

LANGUAGES = [
    ('en', gettext_noop('English')),
    ('ru', gettext_noop('Russian')),
    ('uz', gettext_noop('Uzbek')),
]

EXTRA_LANG_INFO = {
    'uz': {
        'bidi': False,  # right-to-left
        'code': 'uz',
        'name': 'Uzbek',
        'name_local': "O'zbek",
    },
}

# Add custom languages not provided by Django
import django.conf.locale
LANG_INFO = dict(django.conf.locale.LANG_INFO, **EXTRA_LANG_INFO)
django.conf.locale.LANG_INFO = LANG_INFO

And one more thing you do not have to override LANGUAGES attribute in Django global_settings , because you have already override it in your project settings. So this line can be ignored:

global_settings.LANGUAGES = global_settings.LANGUAGES + [("uz", 'Uzbek')]
Bedilbek
  • 849
  • 7
  • 17