コード例 #1
0
 def test_get_fallbacks(self):
     self.assertEqual(languages.get_fallbacks('cs'), [])
     # Languages has default as fallback
     self.assertEqual(languages.get_fallbacks('en'), ['cs'])
     self.assertEqual(languages.get_fallbacks('fr'), ['cs'])
     # Short language version is a fallback for language
     self.assertEqual(languages.get_fallbacks('en-us'), ['en', 'cs'])
コード例 #2
0
    def _get_translation(self, language_code, fallback=False, can_create=False):
        """
        Returns translation instance for given 'language_code'.

        If the translation does not exist:
        1. if 'can_create' is True, this function will create one
        2. otherwise, if 'fallback' is True, this function will try fallback languages
        3. if all of the above fails to find a translation, raise the
            TranslationModel.DoesNotExist exception
        """
        self._fill_translation_cache(language_code)

        translation = self._translation_cache.get(language_code)
        if translation is not None:
            return translation

        if can_create:
            translation = self._meta.translation_model(master=self, language_code=language_code)
            self._translation_cache[language_code] = translation
            return translation

        elif fallback:
            for language_code in get_fallbacks(language_code):
                self._fill_translation_cache(language_code)
                translation = self._translation_cache.get(language_code)
                if translation is not None:
                    return translation

        raise self._meta.translation_model.DoesNotExist(language_code)
コード例 #3
0
    def __get__(self, instance, instance_type=None):
        """
        Returns field translation or None
        """
        if instance is None:
            return self

        translation_model = self.model._meta.translation_model

        # Find language codes to be tried
        lang_codes = [self.language_code]
        if self._fallback:
            lang_codes += get_fallbacks(self.language_code)

        for lang_code in lang_codes:
            # Find translation
            translation_name = '%s_%s' % (TRANSLATION_FIELD_NAME, sanitize_language_code(lang_code))
            try:
                # Translation is nullable, so it may return None
                translation = getattr(instance, translation_name)
            except translation_model.DoesNotExist:
                translation = None

            if translation is None:
                # Translation does not exist, try another language
                continue

            # Once we have the translation object we return what's there
            return getattr(translation, self._field_name)

        return None
コード例 #4
0
 def test_07_implicit_fallbacks(self):
     self.assertEqual(languages.get_fallbacks('en-us'), ['en-us', 'en'])
コード例 #5
0
 def test_06_get_fallbacks(self):
     self.assertEqual(languages.get_fallbacks('en'), ['en', 'ja'])
     self.assertEqual(languages.get_fallbacks('ja'), ['ja', 'en'])
コード例 #6
0
 def test02_fallbacks(self):
     self.assertEqual(languages.get_fallbacks('cs'), [])
     self.assertEqual(languages.get_fallbacks('en'), ['cs'])
     self.assertEqual(languages.get_fallbacks('en-us'), ['en', 'cs'])
コード例 #7
0
 def test_07_implicit_fallbacks(self):
     self.assertEqual(languages.get_fallbacks('en-us'), ['en-us', 'en'])
コード例 #8
0
 def test_06_get_fallbacks(self):
     self.assertEqual(languages.get_fallbacks('en'), ['en', 'ja'])
     self.assertEqual(languages.get_fallbacks('ja'), ['ja', 'en'])