Anonymous committed 9d5bc9a

Docstrings, added id_<language-code> to instance with translation table row ids of selected language, also added a conf.CURRENT_LANGUAGE attribute to instances with current selected language

  • Participants
  • Parent commits a48cbe4

Comments (0)

Files changed (3)

File model_i18n/

 # Current loaded languages attribute name
 CURRENT_LANGUAGES = 'current_languages'
+# Current selected language
+CURRENT_LANGUAGE  = 'current_language'
 # Do we have multidb support? (post r11952)

File model_i18n/

                         if alias not in used_aliases ]
     def __and__(self, right):
+        """ AND operator. Useful to setup several joins rules """
         if not isinstance(right, QOuterJoins):
             return super(QOuterJoins, self).__and__(right)
         # resolve joins
         super(TransJoin, self).add_to_query(query, used_aliases)
+        trans_pk = QN(
         fields = self.model._translation_model._transmeta.translatable_fields
         # add joined columns needed
         select = {}
         for alias, lang in
             alias = QN(alias)
+            select['id_%s' % lang] = '%s.%s' % (alias, trans_pk)
             select.update(('%s_%s' % (name, lang),
                            '%s.%s' % (alias, QN(name)))
                                 for name in fields)
         query.add_extra(select, None, None, None, None, None)
     def __and__(self, right):
+        """ AND operator, useful to request more than one language
+        in a single query:
+            TransJoin(...) & TransJoin(...)
+        """
         if isinstance(right, TransJoin) and self.model == right.model:
         return super(TransJoin, self).__and__(right)
                                          CURRENT_LANGUAGES, '').split('_'))
         implicit = self.lang
         if implicit and implicit in languages:
-            instance.set_language(implicit) # switch to implicit language
+            instance.switch_language(implicit) # switch to implicit language
         setattr(instance, CURRENT_LANGUAGES, languages)
         return instance
     def _clone(self, *args, **kwargs):
+        """ _clone override, setups languages requested and current 
+        selected language"""
         clone = super(TransQuerySet, self)._clone()
         clone.lang = self.lang
         clone.languages = self.languages

File model_i18n/

 from model_i18n import managers
 from model_i18n.options import ModelTranslation
 from model_i18n.exceptions import AlreadyRegistered
-from model_i18n.conf import CURRENT_LANGUAGES, ATTR_BACKUP_SUFFIX
+from model_i18n.conf import CURRENT_LANGUAGES, CURRENT_LANGUAGE, \
+                            ATTR_BACKUP_SUFFIX
 __all__ = ['register', 'ModelTranslation']
             will load attribute values for master default language
     current_languages = getattr(instance, CURRENT_LANGUAGES, None)
+    current = getattr(instance, CURRENT_LANGUAGE, None)
     if current_languages: # any translation?
         trans_meta = instance._translation_model._transmeta
         fields = trans_meta.translatable_fields
         if not lang or lang == trans_meta.master_language: # use defaults
             for name in fields:
-                value = getattr(instance, '_'.join((name, ATTR_BACKUP_SUFFIX)))
+                value = getattr(instance, '_'.join((name, ATTR_BACKUP_SUFFIX)),
+                                None)
                 setattr(instance, name, value)
-        elif lang in current_languages: # swtich language
+        elif lang in current_languages and lang != current: # swtich language
             for name in fields:
                 value = getattr(instance, '_'.join((name, lang)), None)
                 if value is not None: # Ignore None, means not translated
                     setattr(instance, name, value)
+        setattr(instance, CURRENT_LANGUAGE, lang)
 # Just one Translator instance is needed.
 _translator = Translator()