def test_translate_fallback():
        """Tests whether the :see:LocalizedValue
        class's translate()'s fallback functionality
        works properly."""

        test_value = 'myvalue'

        localized_value = LocalizedValue({settings.LANGUAGE_CODE: test_value})

        other_language = settings.LANGUAGES[-1][0]

        # make sure that, by default it returns
        # the value in the default language
        assert localized_value.translate() == test_value

        # make sure that it falls back to the
        # primary language when there's no value
        # available in the current language
        translation.activate(other_language)
        assert localized_value.translate() == test_value

        # make sure that it's just __str__ falling
        # back and that for the other language
        # there's no actual value
        assert localized_value.get(other_language) != test_value
Пример #2
0
    def test_translate_none():
        """Tests whether the :see:LocalizedValue class's translate() method
        properly returns None when there is no value."""

        # with no value, we always expect it to return None
        localized_value = LocalizedValue()
        assert localized_value.translate() is None
        assert str(localized_value) == ""

        # with no value for the default language, the default
        # behavior is to return None, unless a custom fallback
        # chain is configured, which there is not for this test
        other_language = settings.LANGUAGES[-1][0]
        localized_value = LocalizedValue({other_language: "hey"})

        translation.activate(settings.LANGUAGE_CODE)
        assert localized_value.translate() is None
        assert str(localized_value) == ""
Пример #3
0
    def test_translate_custom_language():
        """Tests whether the :see:LocalizedValue class's translate() ignores
        the active language when one is specified explicitely."""

        localized_value = LocalizedValue(
            {settings.LANGUAGE_CODE: settings.LANGUAGE_CODE, "ro": "ro"}
        )

        with translation.override("en"):
            assert localized_value.translate("ro") == "ro"
Пример #4
0
    def test_translate():
        """Tests whether the :see:LocalizedValue class's __str__ works
        properly."""

        keys = get_init_values()
        localized_value = LocalizedValue(keys)

        for language, value in keys.items():
            translation.activate(language)
            assert localized_value.translate() == value
Пример #5
0
    def load(self):
        if is_iterable_and_no_string(self.data):
            if not 0 < len(self.data) < 3:
                raise DataSourceException(
                    f"Failed to parse data:\n{self.data}")
            elif len(self.data) == 1:
                return str(self.data[0]), str(self.data[0])
            elif isinstance(self.data[1], dict):
                label = LocalizedValue(self.data[1])
                return (str(self.data[0]), str(label.translate()))
            return str(self.data[0]), str(self.data[1])

        return str(self.data), str(self.data)
Пример #6
0
    def test_translate_fallback_custom_fallback():
        """Tests whether the :see:LocalizedValue class's translate()'s fallback
        functionality properly respects the LOCALIZED_FIELDS_FALLBACKS
        setting."""

        fallbacks = {"nl": ["ro"]}

        localized_value = LocalizedValue(
            {settings.LANGUAGE_CODE: settings.LANGUAGE_CODE, "ro": "ro"}
        )

        with override_settings(LOCALIZED_FIELDS_FALLBACKS=fallbacks):
            with translation.override("nl"):
                assert localized_value.translate() == "ro"