|  | 
| 2 | 2 | import os | 
| 3 | 3 | from collections.abc import Iterable | 
| 4 | 4 | 
 | 
|  | 5 | +import django | 
| 5 | 6 | import pytest | 
| 6 | 7 | from django.db.models import QuerySet | 
| 7 | 8 | from django.urls import reverse | 
| @@ -822,3 +823,73 @@ def test_dependent_fields_clear_after_change_parent( | 
| 822 | 823 |             ) | 
| 823 | 824 |         ) | 
| 824 | 825 |         assert city2_container.text == "" | 
|  | 826 | + | 
|  | 827 | + | 
|  | 828 | +@pytest.fixture( | 
|  | 829 | +    name="widget", | 
|  | 830 | +    params=[ | 
|  | 831 | +        (Select2Widget, {}), | 
|  | 832 | +        (HeavySelect2Widget, {"data_view": "heavy_data_1"}), | 
|  | 833 | +        (HeavySelect2MultipleWidget, {"data_view": "heavy_data_1"}), | 
|  | 834 | +        (ModelSelect2Widget, {}), | 
|  | 835 | +        (ModelSelect2TagWidget, {}), | 
|  | 836 | +    ], | 
|  | 837 | +    ids=lambda p: p[0], | 
|  | 838 | +) | 
|  | 839 | +def widget_fixture(request): | 
|  | 840 | +    widget_class, widget_kwargs = request.param | 
|  | 841 | +    return widget_class(**widget_kwargs) | 
|  | 842 | + | 
|  | 843 | + | 
|  | 844 | +@pytest.mark.skipif(django.VERSION < (4, 1), reason="Only for Django 4.1+") | 
|  | 845 | +@pytest.mark.parametrize( | 
|  | 846 | +    "locale,expected", | 
|  | 847 | +    [ | 
|  | 848 | +        ("fr-FR", "fr"), | 
|  | 849 | +        # Some locales with a country code are natively supported by select2's i18n | 
|  | 850 | +        ("pt-BR", "pt-BR"), | 
|  | 851 | +        ("sr-Cyrl", "sr-Cyrl"), | 
|  | 852 | +    ], | 
|  | 853 | +    ids=repr, | 
|  | 854 | +) | 
|  | 855 | +def test_i18n_name_property_with_country_code_in_locale(widget, locale, expected): | 
|  | 856 | +    """Test we fall back to the language code if the locale contain an unsupported country code.""" | 
|  | 857 | +    with translation.override(locale): | 
|  | 858 | +        assert widget.i18n_name == expected | 
|  | 859 | + | 
|  | 860 | + | 
|  | 861 | +@pytest.mark.skipif(django.VERSION < (4, 1), reason="Only for Django 4.1+") | 
|  | 862 | +def test_i18n_media_js_with_country_code_in_locale(widget): | 
|  | 863 | +    translation.activate("fr-FR") | 
|  | 864 | +    assert tuple(widget.media._js) == ( | 
|  | 865 | +        "admin/js/vendor/select2/select2.full.min.js", | 
|  | 866 | +        "admin/js/vendor/select2/i18n/fr.js", | 
|  | 867 | +        "django_select2/django_select2.js", | 
|  | 868 | +    ) | 
|  | 869 | + | 
|  | 870 | + | 
|  | 871 | +@pytest.mark.skipif(django.VERSION >= (4, 1), reason="Only for Django 4.0 and previous") | 
|  | 872 | +@pytest.mark.parametrize( | 
|  | 873 | +    "locale,expected", | 
|  | 874 | +    [ | 
|  | 875 | +        ("fr-FR", None), | 
|  | 876 | +        # Some locales with a country code are natively supported by select2's i18n | 
|  | 877 | +        ("pt-BR", "pt-BR"), | 
|  | 878 | +        ("sr-Cyrl", "sr-Cyrl"), | 
|  | 879 | +    ], | 
|  | 880 | +) | 
|  | 881 | +def test_i18n_name_property_with_country_code_in_locale_for_older_django( | 
|  | 882 | +    widget, locale, expected | 
|  | 883 | +): | 
|  | 884 | +    """No fallback for locale with an unsupported country code.""" | 
|  | 885 | +    with translation.override(locale): | 
|  | 886 | +        assert widget.i18n_name == expected | 
|  | 887 | + | 
|  | 888 | + | 
|  | 889 | +@pytest.mark.skipif(django.VERSION >= (4, 1), reason="Only for Django 4.0 and previous") | 
|  | 890 | +def test_i18n_media_js_with_country_code_in_locale_for_older_django(widget): | 
|  | 891 | +    translation.activate("fr-FR") | 
|  | 892 | +    assert tuple(widget.media._js) == ( | 
|  | 893 | +        "admin/js/vendor/select2/select2.full.min.js", | 
|  | 894 | +        "django_select2/django_select2.js", | 
|  | 895 | +    ) | 
0 commit comments