diff --git a/README.rst b/README.rst index 12c28a5f..65f97c9f 100644 --- a/README.rst +++ b/README.rst @@ -45,6 +45,9 @@ Currently implemented: * ``django_codemod.commands.django_40.UGetTextNoopToGetTextNoopCommand``: migrate deprecated ``ugettext_noop()`` function to ``gettext_noop()``. +* ``django_codemod.commands.django_40.UNGetTextToNGetTextCommand``: migrate deprecated + ``ungettext()`` function to ``ngettext()``. + Credits ------- diff --git a/django_codemod/commands/django_40.py b/django_codemod/commands/django_40.py index 21a611e0..c2572fde 100644 --- a/django_codemod/commands/django_40.py +++ b/django_codemod/commands/django_40.py @@ -107,3 +107,11 @@ class UGetTextNoopToGetTextNoopCommand(UGetTextToGetTextCommand): DESCRIPTION: str = "Replaces ugettext_noop() by gettext_noop()." old_name = "ugettext_noop" new_name = "gettext_noop" + + +class UNGetTextToNGetTextCommand(UGetTextToGetTextCommand): + """Help resolve deprecation of django.utils.translation.ungettext.""" + + DESCRIPTION: str = "Replaces ungettext() by ngettext()." + old_name = "ungettext" + new_name = "ngettext" diff --git a/tests/commands/test_django_40.py b/tests/commands/test_django_40.py index a81a344a..c2f169cf 100644 --- a/tests/commands/test_django_40.py +++ b/tests/commands/test_django_40.py @@ -6,6 +6,7 @@ UGetTextToGetTextCommand, UGetTextLazyToGetTextLazyCommand, UGetTextNoopToGetTextNoopCommand, + UNGetTextToNGetTextCommand, ) @@ -277,3 +278,53 @@ def test_already_imported_substitution(self) -> None: result = gettext_noop(content) """ self.assertCodemod(before, after) + + +class TestUNGetTextToNGetTextCommand(CodemodTest): + + TRANSFORM = UNGetTextToNGetTextCommand + + def test_noop(self) -> None: + """Test when nothing should change.""" + before = """ + from django import conf + from django.utils import translation + + foo = ngettext("bar", "bars", count) + """ + after = """ + from django import conf + from django.utils import translation + + foo = ngettext("bar", "bars", count) + """ + + self.assertCodemod(before, after) + + def test_simple_substitution(self) -> None: + """Check simple use case.""" + before = """ + from django.utils.translation import ungettext + + result = ungettext(content, plural_content, count) + """ + after = """ + from django.utils.translation import ngettext + + result = ngettext(content, plural_content, count) + """ + self.assertCodemod(before, after) + + def test_already_imported_substitution(self) -> None: + """Test case where ngettext is already in the imports.""" + before = """ + from django.utils.translation import ungettext, ngettext + + result = ungettext(content, plural_content, count) + """ + after = """ + from django.utils.translation import ngettext + + result = ngettext(content, plural_content, count) + """ + self.assertCodemod(before, after)