documentation: inaccurate for MODELTRANSLATION_LANGUAGES
Documentation section on MODELTRANSLATION_LANGUAGES: github installation.rst / readthedocs installation.html states:
Default: same as LANGUAGES
This is not true if LANGUAGES is of following form:
LANGUAGES = (
('en', 'English'),
('de', 'German'),
('pl', 'Polish'),
)
Then MODELTRANSLATION_LANGUAGES = LANGUAGES does not work.
settings.py treats MODELTRANSLATION_LANGUAGES and LANGUAGES differently.
So please either:
A) reword the documentation (MODELTRANSLATION_LANGUAGES needs to be a tuple (or list) of language codes)
or
B) update above code point to accept also the same structure (tuple of tuples) as LANGUAGES does.
It's a great idea. It happens that modified language code is used during development:
LANGUAGES = (
('en', 'English'),
('ge', 'German'),
('po', 'Polish'),
)
instead of
LANGUAGES = (
('en', 'English'),
('de', 'German'),
('pl', 'Polish'),
)
When using modified language codes, an error occurs because the language code is not found.
...
Traceback (most recent call last):
File "/project/site/venv/lib/python3.10/site-packages/django/utils/translation/__init__.py", line 272, in get_language_info
lang_info = LANG_INFO[lang_code]
KeyError: 'de'
...
File "/project/site/venv/lib/python3.10/site-packages/modeltranslation/utils.py", line 35, in get_language_bidi
lang_info = get_language_info(lang)
File "/project/site/venv/lib/python3.10/site-packages/django/utils/translation/__init__.py", line 279, in get_language_info
raise KeyError("Unknown language code %s." % lang_code)
KeyError: 'Unknown language code de.'
[15/May/2023 11:13:36] "GET /admin/site/promotion/add/ HTTP/1.1" 500 196183
I plan to add this functionality in the near future.
@fvgoto
MODELTRANSLATION_LANGUAGES = LANGUAGES
You don't need to do this. It's automatically populated from LANGUAGES.
@text-adi what is "modified language code", and why are you using it?
@text-adi what is "modified language code", and why are you using it?
Because while browsing the site, the user can change the current language of the site. The URL of the page that will be generated for the language "German" depends on the language code that is written in LANGUAGES. For example, in the case of the language "German", "de" is used instead of the code "ge". That is, the url of the page with the language "German" will look like https://site.com/de/ instead of https://site.com/ge/. In this case, an error occurs when you try to open the model editing in the admin panel, where the fields generated by this library are used.
Traceback (most recent call last):
File "/project/site/venv/lib/python3.10/site-packages/django/utils/translation/__init__.py", line 272, in get_language_info
lang_info = LANG_INFO[lang_code]
KeyError: 'de'
because the library is trying to get information about the "German" language with the corresponding new "de" language key. Since the standard language code has been changed, this key is not in the full list of LANG_INFO languages. There is still "ge".
Well, there is no "ge" language code. There is only "de".
The problem is that you're using wrong language code and expect it to works.
In the case of django, the changed language code in the LANGUAGES variable works correctly. If you need to change the language url to an arbitrary one, this feature would be useful.