Skip to content

Commit

Permalink
Feat: Add build_lang helper in utils
Browse files Browse the repository at this point in the history
  • Loading branch information
koleror authored and last-partizan committed May 16, 2024
1 parent d7a4d0b commit bdee9ff
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Contributors
* PetrDlouhy
* dmarcelino
* GreyZmeem
* Hugo Defrance
* And many more ... (if you miss your name here, please let us know!)

.. _django-linguo: https://github.com/zmathew/django-linguo
7 changes: 7 additions & 0 deletions modeltranslation/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from modeltranslation.utils import (
auto_populate,
build_css_class,
build_lang,
build_localized_fieldname,
fallbacks,
)
Expand Down Expand Up @@ -1182,6 +1183,12 @@ def test_indonesian(self):
field = models.ForeignKeyModel._meta.get_field("test")
assert field.attname != build_localized_fieldname(field.name, "id")

def test_build_lang(self):
assert build_lang("en") == "en"
assert build_lang("en_en") == "en_en"
assert build_lang("en-en") == "en_en"
assert build_lang("id") == "ind"


class ManyToManyFieldsTest(ModeltranslationTestBase):
@classmethod
Expand Down
8 changes: 6 additions & 2 deletions modeltranslation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ def get_translation_fields(field: str) -> list[str]:
return [build_localized_fieldname(field, lang) for lang in settings.AVAILABLE_LANGUAGES]


def build_localized_fieldname(field_name: str, lang: str) -> str:
def build_lang(lang: str) -> str:
if lang == "id":
# The 2-letter Indonesian language code is problematic with the
# current naming scheme as Django foreign keys also add "id" suffix.
lang = "ind"
return str("%s_%s" % (field_name, lang.replace("-", "_")))
return lang.replace("-", "_")


def build_localized_fieldname(field_name: str, lang: str) -> str:
return str("%s_%s" % (field_name, build_lang(lang)))


def _build_localized_verbose_name(verbose_name: Any, lang: str) -> str:
Expand Down

0 comments on commit bdee9ff

Please sign in to comment.