1. Branko Vukelic
  2. django-i18n-model

Commits

Hajime Yamasaki  committed decc9a2

Implemented current_language manager method

  • Participants
  • Parent commits 8076895
  • Branches master
  • Tags 0.0.1

Comments (0)

Files changed (3)

File README.rst

View file
  • Ignore whitespace
 
     translation = my_source.translations.sr().get()
 
+Getting translation for current language
+========================================
+
+You can use the custom manager's ``current_language`` method to retrieve
+translations for the currently active language::
+
+    SourceI18N.objects.current_language()
+
+This also works on related objects::
+
+    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
 from django.db.models.base import ModelBase, Model
 from django.db.models.loading import get_model
 from django.utils.translation import ugettext_lazy as _
+from django.utils.translation import get_language
 
 
 __all__ = ['I18nModel', 'I18nManager']
 
     use_for_related_fields = True
 
+    def current_language(self):
+        language_code = get_language()
+        return self.filter(i18n_language=language_code)
+
     def __new__(cls, *args, **kwargs):
         for language in settings.LANGUAGES:
             language_code = language[0]

File tests/test_translation_method.py

View file
  • Ignore whitespace
 from django.conf import settings
 from django.core.management import call_command
 from django.db.models import loading
+from django.utils import translation
 
 from .models import Post, PostI18N
 
         self.assertEqual(
             self.post.translations.it().get(),
             PostI18N.translate(self.post, 'it')
+        )
+
+    def test_get_current_language(self):
+        """
+        Test getting current language
+        """
+
+        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('de')
+
+        translation_de = PostI18N.translate(self.post, 'de')
+
+        self.assertEqual(
+            self.post.translations.current_language().get(),
+            translation_de
         )