Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ (core): Added simply register of fields for translation
- Loading branch information
Showing
11 changed files
with
99 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import django | ||
|
||
from meringue.core import options # nowa: F401 | ||
|
||
|
||
if django.VERSION < (3, 2): | ||
default_app_config = "meringue.core.apps.Config" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ | |
|
||
options.DEFAULT_NAMES = ( | ||
*options.DEFAULT_NAMES, | ||
"translate_fields", | ||
"m_translate_fields", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from test_project.models import TranslatedModel | ||
|
||
|
||
def test_translate_fields(): | ||
""" | ||
Checking the registration of fields for translation | ||
""" | ||
|
||
instance = TranslatedModel() | ||
assert hasattr(instance, "name") | ||
assert hasattr(instance, "name_ru") | ||
assert hasattr(instance, "name_en") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,18 @@ | ||
from importlib import import_module | ||
|
||
from django.apps import apps | ||
from django.conf import settings | ||
|
||
from modeltranslation.translator import TranslationOptions | ||
from modeltranslation.translator import translator | ||
|
||
|
||
try: | ||
from polymorphic.utils import get_base_polymorphic_model | ||
except ImportError: | ||
pass | ||
else: | ||
for app in settings.INSTALLED_APPS: | ||
try: | ||
models = import_module("%s.models" % app) | ||
except ImportError: | ||
bits = app.split(".") | ||
app_name = import_module(bits[0]) | ||
|
||
for bit in bits[1:]: | ||
app_name = getattr(app_name, bit) | ||
|
||
if hasattr(app_name, "name"): | ||
try: | ||
models = import_module("%s.models" % app_name.name) | ||
except ImportError: | ||
continue | ||
else: | ||
continue | ||
|
||
except AttributeError: | ||
continue | ||
|
||
for model in apps.get_models(): | ||
fields = getattr(model._meta, "translate_fields", []) | ||
force = False | ||
|
||
if not fields and get_base_polymorphic_model and get_base_polymorphic_model(model): | ||
force = True | ||
for model in apps.get_models(): | ||
fields = getattr(model._meta, "m_translate_fields", []) | ||
|
||
if (fields or force) and model not in translator.get_registered_models(): | ||
translator.register( | ||
model, | ||
type( | ||
str(model.__name__ + "Translation"), | ||
(TranslationOptions,), | ||
{"fields": fields}, | ||
), | ||
) | ||
if fields and model not in translator.get_registered_models(): | ||
translator.register( | ||
model, | ||
type( | ||
str(model.__name__ + "Translation"), | ||
(TranslationOptions,), | ||
{"fields": fields}, | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters