1. Branko Vukelic
  2. django-i18n-model

Commits

Hajime Yamasaki  committed f7eb0bf

Added lang method as a more general way of retrieving arbitrary translations (including ones for current language)

  • Participants
  • Parent commits 9873ad6
  • Branches master

Comments (0)

Files changed (3)

File README.rst

View file
  • Ignore whitespace
 
     translation = my_source.translations.sr().get()
 
-Getting translation for current language
+Retrieving translations programmatically
 ========================================
 
-You can use the custom manager's ``current_language`` method to retrieve
-translations for the currently active language::
+Although the hard-coded locale methods are useful in templates, you may
+sometimes need to retrieve translations with variable locale. In that case, you
+may want to use the ``lang`` manager method instead. Here's an example::
 
-    SourceI18N.objects.current_language()
+    SourceI18N.objects.lang('de').all()
 
-This also works on related objects::
+or::
+
+    my_source.translations.lang('de').get()
+
+Using the ``lang`` method without any language code will filter languages for
+the currently active language::
+
+    translation.activate('de')
+    my_source.translations.lang().get()  # Gets translation for 'de' language
+
+The ``current_language`` manager method is a deprecated alias for the last form.
 
-    my_source.translations.current_language().get()
 
 .. _django-hvad: http://django-hvad.readthedocs.org/en/latest/index.html
 .. _django-modeltranslation: https://github.com/deschler/django-modeltranslation

File i18n_model/models.py

View file
  • Ignore whitespace
 
     use_for_related_fields = True
 
-    def current_language(self):
-        language_code = get_language()
+    def lang(self, language_code=None):
+        language_code = language_code or get_language()
         return self.filter(i18n_language=language_code)
 
+    def current_language(self):
+        return self.lang()
+
     def __new__(cls, *args, **kwargs):
         for language in settings.LANGUAGES:
             language_code = language[0]

File tests/test_translation_method.py

View file
  • Ignore whitespace
         translation_de = PostI18N.translate(self.post, 'de')
 
         self.assertEqual(
-            self.post.translations.current_language().get(),
+            self.post.translations.lang().get(),
             translation_de
+        )
+
+    def test_get_arbitrary_langauge_programmatically(self):
+        """
+        Test getting language using the ``langauge`` manager method
+        """
+
+        PostI18N.translate(
+            self.post,
+            'de',
+            title='Test-Titel',
+            body='Dies ist ein Test Beitrag',
+        )
+        PostI18N.translate(
+            self.post,
+            'it',
+            title='Titolo di prova',
+            body='Questo è un post di prova'
+        )
+
+        translation.activate('it')
+        translation_de = PostI18N.translate(self.post, 'de')
+
+        self.assertEqual(
+            self.post.translations.lang('de').get(),
+            translation_de
+        )
+
+        self.assertEqual(
+            self.post.translations.de().get(),
+            self.post.translations.lang('de').get()
         )