def __getattribute__(self, attr): language_ = get_language()[:2] value = object.__getattribute__(self, attr) index = get_trans_index(value) if index: # retrieve the translation try: id_ = object.__getattribute__(self, "id") table_ = object.__getattribute__( self, "__class__").__module__ + "." + object.__getattribute__( self, "__class__").__name__ attr_trans = Translation.objects.filter(text_id=index, element_id=id_, table=table_, language=language_) if (attr_trans.count() > 0): return attr_trans[0].value else: #get the default value attr_trans = Translation.objects.filter(text_id=index, element_id=id_, table=table_, default=True) if (attr_trans.count() > 0): return attr_trans[0].value # there isn't a default value -> it musn't be possible because it is ensured during the template parser return value except ObjectDoesNotExist, e: # this attribute doesn't exist raise AttributeError
def __getattribute__(self, attr): language_ = get_language()[:2] value = object.__getattribute__(self, attr) index = get_trans_index(value) if index: # retrieve the translation try: id_ = object.__getattribute__(self, "id") table_ = object.__getattribute__(self, "__class__").__module__ + "." + object.__getattribute__(self, "__class__").__name__ attr_trans = Translation.objects.filter(text_id=index, element_id=id_, table=table_, language=language_) if (attr_trans.count() > 0): return attr_trans[0].value else: #get the default value attr_trans = Translation.objects.filter(text_id=index, element_id=id_, table=table_, default=True) if (attr_trans.count() > 0): return attr_trans[0].value # there isn't a default value -> it musn't be possible because it is ensured during the template parser return value except ObjectDoesNotExist: # this attribute doesn't exist raise AttributeError else: # the element don't need to be translated return value
def addIndex(self, index): #add index to the translation list if len(index): value = get_trans_index(index[0]) if value and not value in self.translatable_list: self.translatable_list.append(value) return value
def addIndex(self, index): #add index to the translation list if (len(index)): value = get_trans_index(index[0]) if value and not value in self.translatable_list: self.translatable_list.append(value) return value return None
def get_translated_model(self): language = get_language()[:2] pk = self.pk index_mapping = {} for field in iter(self._meta.fields): index = get_trans_index(getattr(self, field.attname)) if index: if not index in index_mapping: index_mapping[index] = [] index_mapping[index].append(field.attname) data = {} key = self._get_cache_key() translations = cache.get(key) if translations == None: translations = { 'default': {}, } for attr_trans in Translation.objects.filter(table=self._get_table_id()): if attr_trans.default: try: translations['default'][attr_trans.element_id].append(attr_trans) except KeyError: translations['default'][attr_trans.element_id] = [attr_trans] if attr_trans.language not in translations: translations[attr_trans.language] = {} try: translations[attr_trans.language][attr_trans.element_id].append(attr_trans) except KeyError: translations[attr_trans.language][attr_trans.element_id] = [attr_trans] cache.set(key, translations) elif pk not in translations['default']: for attr_trans in Translation.objects.filter(table=self._get_table_id(), element_id=pk): if attr_trans.default: try: translations['default'][attr_trans.element_id].append(attr_trans) except KeyError: translations['default'][attr_trans.element_id] = [attr_trans] if attr_trans.language not in translations: translations[attr_trans.language] = {} try: translations[attr_trans.language][attr_trans.element_id].append(attr_trans) except KeyError: translations[attr_trans.language][attr_trans.element_id] = [attr_trans] cache.set(key, translations) # add default values try: attr_trans = translations['default'][pk] for element in attr_trans: if element.text_id in index_mapping: attnames = index_mapping[element.text_id] for attname in attnames: data[attname] = element.value except KeyError: pass # no data in default language # add specific values try: attr_trans = translations[language][pk] for element in attr_trans: if element.text_id in index_mapping: attnames = index_mapping[element.text_id] for attname in attnames: data[attname] = element.value except KeyError: pass # no data in language return TranslatedModel(self, data)