Ejemplo n.º 1
0
    def _add_translation_index(self, value, **kwargs):
        index = get_trans_index(value)
        if not index:
            return

        if index not in self._translation_indexes:
            self._translation_indexes[index] = []

        self._translation_indexes[index].append(kwargs)
Ejemplo n.º 2
0
    def _add_translation_index(self, value, **kwargs):
        index = get_trans_index(value)
        if not index:
            return

        if index not in self._info['translation_index_usage']:
            self._info['translation_index_usage'][index] = []

        self._info['translation_index_usage'][index].append(kwargs)
Ejemplo n.º 3
0
    def _add_translation_index(self, value, **kwargs):
        index = get_trans_index(value)
        if not index:
            return

        if index not in self._translation_indexes:
            self._translation_indexes[index] = []

        self._translation_indexes[index].append(kwargs)
Ejemplo n.º 4
0
    def _add_translation_index(self, value, **kwargs):
        index = get_trans_index(value)
        if not index:
            return

        if index not in self._info['translation_index_usage']:
            self._info['translation_index_usage'][index] = []

        self._info['translation_index_usage'][index].append(kwargs)
Ejemplo n.º 5
0
    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 is 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)