def test_split_translated_fieldname(self):

        self.assertEquals(split_translated_fieldname('title_nl'),
                          ('title', 'nl'))

        self.assertEquals(split_translated_fieldname('full_name_nl'),
                          ('full_name', 'nl'))
Exemple #2
0
def copy_translations(Model, fields):
    '''
    Copy translations for all items in the database for a Model with
    translations managed by django-modeltranslation into a json field `i18n`
    managed by django-modeltrans.
    Values for the default language will be copied to the original field.

    Arguments:
        Model: A (historical) Model from the migraton's app registry
        fields(iterable): list of fields to copy into their new places.
    '''
    for m in Model.objects.all():
        m.i18n = {}
        for field in fields:
            value = getattr(m, field)
            if value is None:
                continue

            original_field, lang = split_translated_fieldname(field)

            if lang == DEFAULT_LANGUAGE:
                setattr(m, original_field, value)
            else:
                m.i18n[field] = value

        m.save()
    def test_split_translated_fieldname(self):
        self.assertEqual(split_translated_fieldname("title_nl"),
                         ("title", "nl"))

        self.assertEqual(split_translated_fieldname("full_name_nl"),
                         ("full_name", "nl"))