Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added available languages for models #678

Open
wants to merge 62 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
0af6c00
Added available languages for models
text-adi Apr 12, 2023
3d505d2
Update modeltranslation/translator.py
text-adi Apr 12, 2023
ecf226d
Revert "chore: Update dev deps"
text-adi Apr 12, 2023
8dd83f5
Added available languages for models
text-adi Apr 12, 2023
d689771
Merge remote-tracking branch 'origin/add_model_available_languages' i…
text-adi Apr 12, 2023
14d3ac6
Documentation update
text-adi Apr 12, 2023
ef9d67d
Documentation changes reverted
text-adi Apr 12, 2023
023c102
Update docs
text-adi Apr 12, 2023
d422508
Update docs/modeltranslation/registration.rst
last-partizan Apr 12, 2023
02560e1
Update modeltranslation/translator.py
last-partizan Apr 12, 2023
a879e1a
Poetry.lock changes reverted
text-adi Apr 12, 2023
2543bf9
Merge remote-tracking branch 'origin/add_model_available_languages' i…
text-adi Apr 12, 2023
3306850
Add tests
text-adi May 15, 2023
077f90e
Rollout changes
text-adi May 15, 2023
8257c16
Rollout changes
text-adi May 15, 2023
abefddc
Rollout changes
text-adi May 15, 2023
05b3f68
Rollout changes
text-adi May 15, 2023
4393768
Fixed get code languages
text-adi May 16, 2023
c99c5b3
Removing not using variable
text-adi May 16, 2023
17795e7
Fixed conflict fields
text-adi May 16, 2023
82a2a9f
formated the code
text-adi May 16, 2023
2f4ec57
Add field in class
text-adi May 16, 2023
4703436
Fixed register class
text-adi May 16, 2023
39d5dc1
Fixed test
text-adi May 16, 2023
318ba26
For run test
text-adi May 16, 2023
d69bb6d
Delete code
text-adi May 16, 2023
b9fa97a
Delete code
text-adi May 16, 2023
4a39eb7
Remove field
text-adi May 16, 2023
f5b9c33
Merge branch 'add_model_available_languages' of https://github.com/te…
text-adi May 16, 2023
3af116f
Delete field
text-adi May 16, 2023
11a0976
Merge branch 'add_model_available_languages' of https://github.com/te…
text-adi May 16, 2023
13a06d9
Rollout changes
text-adi May 16, 2023
ef6ae6a
Changes count models
text-adi May 16, 2023
81836d3
Update file migration
text-adi May 16, 2023
700183b
Update file migration
text-adi May 16, 2023
ea11dac
Edit structure models
text-adi May 16, 2023
f6a87e9
Fixed test
text-adi May 16, 2023
83604b4
Fixed test
text-adi May 16, 2023
78313e5
Fixed test
text-adi May 16, 2023
260764b
Fixed test
text-adi May 16, 2023
32f6ced
Fixed test
text-adi May 16, 2023
6a8dc5b
Fixed test
text-adi May 16, 2023
cf5fb22
Fixed test
text-adi May 16, 2023
e76eab1
Clear code
text-adi May 16, 2023
7c9c7c8
Clear code
text-adi May 16, 2023
38eecf3
Rollout code
text-adi May 16, 2023
f8f3e7f
Test
text-adi May 16, 2023
ab0bd7e
Fixet pass test
text-adi May 16, 2023
0c43a4d
Formatted code
text-adi May 16, 2023
08f6f97
Rollout changes
text-adi May 16, 2023
7f6b299
Update docs
text-adi May 16, 2023
15b44e1
Update code
text-adi May 16, 2023
a4128d7
Reformatted code
text-adi May 16, 2023
b796176
Rollout changes
text-adi May 17, 2023
3b904ff
Rollout changes
text-adi May 17, 2023
89659b3
Apply suggestions from code review
last-partizan May 18, 2023
113ebb1
Optimazation code
text-adi May 18, 2023
9181c2a
Update docs/modeltranslation/registration.rst
last-partizan May 19, 2023
9b8db2a
Update docs/modeltranslation/registration.rst
text-adi May 19, 2023
d92ed0f
Update docs
text-adi May 19, 2023
c40cc00
Update modeltranslation/management/commands/update_translation_fields.py
text-adi May 19, 2023
781c0ae
Merge branch 'add_model_available_languages' of https://github.com/te…
text-adi May 19, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/modeltranslation/registration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,40 @@ the translated field ``image`` to the ``NewsWithImage`` model.
inheritance` broke the project (if you had always subclassed
``TranslationOptions`` only, there is no risk).

.. versionadded:: 0.19.0

If you need to specify the languages available for each model, you can do so using the ``languages``
argument::

from modeltranslation.translator import translator, TranslationOptions
from news.models import News, NewsWithImage

class NewsTranslationOptions(TranslationOptions):
fields = ('title', 'text',)

text-adi marked this conversation as resolved.
Show resolved Hide resolved
class NewsWithImageTranslationOptions(TranslationOptions):
fields = ('image',)

translator.register(
News, NewsTranslationOptions,
languages=('en','uk')
)
translator.register(NewsWithImage, NewsWithImageTranslationOptions)


In this case, for the ``News`` model, translations will be available in the languages with the codes
``en`` and ``uk``.
For the ``NewsWithImage`` model, since the ``languages`` argument was not passed, the value from
``settings.MODELTRANSLATION_LANGUAGES`` will be used.

Also, if you wish, you can pass the ``languages`` argument in the ``register`` decorator::

from modeltranslation.translator import register, TranslationOptions
from news.models import News

@register(News, languages=('en','uk'))
class NewsTranslationOptions(TranslationOptions):
fields = ('title', 'text',)

Changes Automatically Applied to the Model Class
------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions modeltranslation/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ def add_translation_fields(model, opts):
model_empty_values = getattr(opts, 'empty_values', NONE)
for field_name in opts.local_fields.keys():
field_empty_value = parse_field(model_empty_values, field_name, NONE)
for lang in mt_settings.AVAILABLE_LANGUAGES:
available_languages = getattr(opts, 'languages', mt_settings.AVAILABLE_LANGUAGES)
for lang in available_languages:
text-adi marked this conversation as resolved.
Show resolved Hide resolved
# Create a dynamic translation field
translation_field = create_translation_field(
model=model, field_name=field_name, lang=lang, empty_value=field_empty_value
Expand Down Expand Up @@ -664,6 +665,5 @@ def lazy_operation(self, func: Callable, *args, **kwargs) -> None:
# This global object represents the singleton translator object
translator = Translator()

last-partizan marked this conversation as resolved.
Show resolved Hide resolved

# Re-export the decorator for convenience
from modeltranslation.decorators import register # NOQA re-export
Loading