Skip to content

Commit

Permalink
Add MODELTRANS_DEFAULT_LANGUAGE setting (#117)
Browse files Browse the repository at this point in the history
fixes: #116
  • Loading branch information
jacobwegner authored Jul 1, 2024
1 parent 5fe082f commit 6d66c21
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 deletions.
4 changes: 2 additions & 2 deletions docs/pages/inner-workings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ language is defined::
# 'Valk', None

The virtual field ``<field>_i18n`` returns the translated value for the current
active language and falls back to the language in ``LANGUAGE_CODE``::
active language and falls back to the language in ``MODELTRANS_DEFAULT_LANGUAGE``::

with override("nl"):
print(b.title_i18n)
Expand All @@ -88,7 +88,7 @@ active language and falls back to the language in ``LANGUAGE_CODE``::

with override("fr"):
print(b.title_i18n)
# 'Falcon' (no french translation available, falls back to LANGUAGE_CODE)
# 'Falcon' (no french translation available, falls back to MODELTRANS_DEFAULT_LANGUAGE)

Django-modeltrans also allows ordering on translated values. Ordering on
``<field>_i18n`` probably makes most sense, as it more likely that there is a
Expand Down
21 changes: 16 additions & 5 deletions docs/pages/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,25 @@ Settings Reference
django-modeltrans allows some configuration to define its behavior.
By default, it tries to use sensible defaults derived from the default django settings.


``MODELTRANS_DEFAULT_LANGUAGE``
----------------------------------
A string representing the default language. By default, the default language is set to
django's `LANGUAGE_CODE setting <https://docs.djangoproject.com/en/stable/ref/settings/#language-code>`_.

Customize this setting to change the default language used by django-modeltrans without impacting
Django's translation / locale middlewares.

See `#116 <https://github.com/zostera/django-modeltrans/issues/116>`_ for additional context.


``MODELTRANS_AVAILABLE_LANGUAGES``
----------------------------------
A list of language codes to allow model fields to be translated in. By default,
the language codes extracted from django's `LANGUAGES setting <https://docs.djangoproject.com/en/stable/ref/settings/#languages>`_.

Note that
- the default language, defined in django's `LANGUAGE_CODE setting <https://docs.djangoproject.com/en/stable/ref/settings/#language-code>`_,
should not be added to this list (will be ignored).
- the default language, defined in `MODELTRANS_DEFAULT_LANGUAGE` should not be added to this list (will be ignored).
- order is not important

A custom definition might be::
Expand All @@ -23,18 +34,18 @@ A custom definition might be::

``MODELTRANS_FALLBACK``
-----------------------
A dict of fallback chains as lists of languages. By default, it falls back to the language defined in django setting `LANGUAGE_CODE`.
A dict of fallback chains as lists of languages. By default, it falls back to the language defined in `MODELTRANS_DEFAULT_LANGUAGE`.

For example, django-modeltrans will fall back to:
- english when the active language is 'nl'
- fist dutch and finally english with active language is 'fy'

If configured like this::

LANGUAGE_CODE = 'en'
MODELTRANS_DEFAULT_LANGUAGE = 'en'
MODELTRANS_AVAILABLE_LANGUAGES = ('nl', 'fy')
MODELTRANS_FALLBACK = {
'default': (LANGUAGE_CODE, ),
'default': (MODELTRANS_DEFAULT_LANGUAGE, ),
'fy': ('nl', 'en')
}

Expand Down
4 changes: 2 additions & 2 deletions docs/pages/working-with-models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Custom fallback language
------------------------

By default, fallback is centrally configured with :ref:`settings_fallback`.
That might not be sufficient, for example if part of the content is created for a single language which is not ``LANGUAGE_CODE``.
That might not be sufficient, for example if part of the content is created for a single language which is not ``MODELTRANS_DEFAULT_LANGUAGE``.

In that case, it can be configured per-record using the ``fallback_language_field`` argument to ``TranslationField``::

Expand All @@ -27,7 +27,7 @@ You can traverse foreign key relations too::

Note that
- if in this example no `newsroom` is set yet, the centrally configured fallback is used.
- the original field _always_ contains the language as configured by ``LANGUAGE_CODE``.
- the original field _always_ contains the language as configured by ``MODELTRANS_DEFAULT_LANGUAGE``.

With the models above::

Expand Down
1 change: 1 addition & 0 deletions docs/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ForeignKey
fy
german
mixin
middlewares
modeltrans
modeltranslation
nl
Expand Down
3 changes: 2 additions & 1 deletion modeltrans/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ def get_modeltrans_setting(key):
settings, "MODELTRANS_FALLBACK", {"default": (get_default_language(),)}
),
"MODELTRANS_ADD_FIELD_HELP_TEXT": getattr(settings, "MODELTRANS_ADD_FIELD_HELP_TEXT", True),
"MODELTRANS_DEFAULT_LANGUAGE": get_default_language(),
}
return modeltrans_settings.get(key)


def get_default_language():
return settings.LANGUAGE_CODE
return getattr(settings, "MODELTRANS_DEFAULT_LANGUAGE", getattr(settings, "LANGUAGE_CODE"))


def get_available_languages_setting():
Expand Down
23 changes: 23 additions & 0 deletions tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from django.test import TestCase, override_settings

from modeltrans.conf import check_fallback_chain, get_available_languages_setting
from modeltrans.translator import check_languages, get_i18n_field, get_i18n_field_param

from .app.models import Person


class FallbackConfTest(TestCase):
Expand Down Expand Up @@ -37,3 +40,23 @@ def test_available_languages_should_be_str(self):
message = "MODELTRANS_AVAILABLE_LANGUAGES should be an iterable of strings"
with self.assertRaisesMessage(ImproperlyConfigured, message):
get_available_languages_setting()


class DefaultLanguageConfTest(TestCase):
@override_settings(LANGUAGE_CODE="es", MODELTRANS_AVAILABLE_LANGUAGES=("nl", "de", "fr"))
def test_django_language_code_not_available_language(self):
message = 'Language "en" is in required_languages on Model "Person" but not in settings.MODELTRANS_AVAILABLE_LANGUAGES.'
i18n_field = get_i18n_field(Person)
required_languages = get_i18n_field_param(Person, i18n_field, "required_languages")
with self.assertRaisesMessage(ImproperlyConfigured, message):
check_languages(required_languages, Person)

@override_settings(
LANGUAGE_CODE="es",
MODELTRANS_DEFAULT_LANGUAGE="en",
MODELTRANS_AVAILABLE_LANGUAGES=("nl", "de", "fr"),
)
def test_default_language_code_is_available_language(self):
i18n_field = get_i18n_field(Person)
required_languages = get_i18n_field_param(Person, i18n_field, "required_languages")
check_languages(required_languages, Person)

0 comments on commit 6d66c21

Please sign in to comment.