示例#1
0
def save_translation(entity, locale, string, plural_form=None, fuzzy=False):
    """Admin interface: save new or update existing translation in DB."""

    approved = not fuzzy
    translations = Translation.objects.filter(
        entity=entity, locale=locale, plural_form=plural_form)
    translations_equal = translations.filter(string=string)
    translations_equal_count = translations_equal.count()

    # Save new translation if it doesn's exist yet
    if translations_equal_count == 0:
        unset_approved(translations)
        t = Translation(
            entity=entity, locale=locale, plural_form=plural_form,
            string=string, date=datetime.datetime.now(),
            approved=approved, fuzzy=fuzzy)
        t.save(stats=False)

    # Update existing translations if fuzzy status changes
    elif translations_equal_count > 0:
        t = translations_equal[0]
        if translations_equal_count > 1:
            try:
                t = translations_equal.get(approved=True)
            except Translation.DoesNotExist:
                t = translations_equal.latest("date")

        if t.fuzzy != fuzzy:
            unset_approved(translations)
            t.date = datetime.datetime.now()
            t.approved = approved
            t.fuzzy = fuzzy
            t.save(stats=False)
示例#2
0
def _save_translation(entity, locale, translation, author=""):
    """Admin interface: save new or update existing translation in DB."""

    translations = Translation.objects.filter(entity=entity, locale=locale).order_by("date")

    if len(translations) == 0:  # New translation
        t = Translation(entity=entity, locale=locale, string=translation, author=author, date=datetime.datetime.now())
    else:  # Update translation
        t = translations.reverse()[0]
        t.string = translation
        t.author = author
        t.date = datetime.datetime.now()
    t.save()