Beispiel #1
0
def review_translation(translation_id):
    '''
    Formulario de revisión de traducción
    '''

    page = request.args.get("page", 0, int)
    mode = request.args.get("show", "old", str)
    size = request.args.get("size", 15, int)

    # Optimización: si envío en un campo la lista de strids, no la pido a BBDD
    data = None
    if request.form.get("field_keys", None):
        data_fields = {i:"" for i in request.form.get("fields").replace("‚",",").replace("&","&").split(",")}
    else:
        data = pagesdb.get_translation(translation_id)
        data_fields = data["texts"]

    # Lenguaje base
    base_lang = pomanager.get_lang("en")

    # Campos de traducción
    fields = {"field_%s" % key:
        TextAreaField(key, default=unfix_lang_values(value, base_lang[key]) or value)
        if len(value) > 40 else
        TextField(key, default=unfix_lang_values(value, base_lang[key]) or value)
        for key, value in data_fields.iteritems() if key in base_lang}

    # Checkboxes
    fields.update(
        ("check_%s" % key,  BooleanField(default=False))
        for key in data_fields.iterkeys())

    form = expanded_instance(ValidateTranslationForm, fields, request.form, prefix="translation_")

    # Guardo en el Hiddenfield la lista de strids
    form.field_keys = "&".join(i.replace("&","&").replace(",","‚") for i in data_fields.iterkeys())

    if request.method=='POST' and form.validate():
        if form.submit.data:
            # Actualizamos el lenguaje de destino con los valores con checkbox marcado
            pomanager.update_lang(data["dest_lang"],
                {key: form["field_%s" % key].data
                    for key in data_fields.iterkeys() if form["check_%s" % key].data})
        pagesdb.update_translation({"_id":translation_id,"processed":True})
        flash("admin_saved")
        return redirect(url_for('admin.translations', page=page, mode=mode, size=size))
    elif data is None:
        # Si no he pedido a base de datos la traducción por optimizar, pero no queda otra
        data = pagesdb.get_translation(translation_id)

    pomanager.preload((g.lang, data["dest_lang"], data["user_lang"]))

    cur_lang = pomanager.get_lang(g.lang)
    user_lang = pomanager.get_lang(data["user_lang"])
    dest_lang = pomanager.get_lang(data["dest_lang"])

    translation = {
        key:(
             cur_lang[key] if key in cur_lang else None,
             user_lang[key] if key in user_lang else None,
             dest_lang[key] if key in dest_lang else None)
        for key, value in data.pop("texts").iteritems()
        if key in base_lang
        }

    return render_template('admin/translation_review.html',
        page_title=_('admin_translation_review'),
        title=admin_title('admin_translation_review'),
        form=form,
        langs=(g.lang, data["user_lang"], data["dest_lang"]),
        data=data,
        page=page,
        list_mode=mode,
        field_keys=",".join(translation.iterkeys()),
        fields=translation)
Beispiel #2
0
def review_translation(translation_id):
    """
    Formulario de revisión de traducción
    """
    page = request.args.get("page", 0, int)
    mode = request.args.get("show", "old", str)
    size = request.args.get("size", 15, int)
    select = request.args.get("select", "default", str)

    # Optimización: si envío en un campo la lista de strids, no la pido a BBDD
    data = None
    if request.form.get("field_keys", None):
        data_fields = {
            i: "" for i in request.form.get("fields").replace("‚", ",").replace("&", "&").split(",")
        }
    else:
        data = pagesdb.get_translation(translation_id)
        data_fields = data["texts"]

    def denuke(o):
        """Evita nukeos de msgstrs malformados"""
        try:
            return _(o)  # TODO: Investigar porqué esto hace falta
        except:
            return o

    # Campos de traducción
    fields = {
        "field_%s" % key: TextAreaField(key, default=value, description=denuke(key))
        if len(value) > 40
        else TextField(key, default=value, description=denuke(key))
        for key, value in data_fields.iteritems()
    }

    # Checkboxes
    fields.update(("check_%s" % key, BooleanField(default=(select == all))) for key in data_fields.iterkeys())

    form = expanded_instance(ValidateTranslationForm, fields, request.form, prefix="translation_")

    # Guardo en el Hiddenfield la lista de strids
    form.field_keys = "&".join(i.replace("&", "&").replace(",", "‚") for i in data_fields.iterkeys())

    if request.method == "POST" and form.validate():
        if form.submit.data:
            pomanager.update_lang(
                data["dest_lang"],
                {key: form["field_%s" % key].data for key in data_fields.iterkeys() if form["check_%s" % key].data},
            )
        pagesdb.update_translation({"_id": translation_id, "processed": True})
        return redirect(url_for("admin.translations", page=page, mode=mode, size=size))
    elif data is None:
        # Si no he pedido a base de datos la traducción por optimizar
        data = pagesdb.get_translation(translation_id)

    dest_lang = {key: value for key, value in pomanager.get_lang(data["dest_lang"]).iteritems() if key in data["texts"]}

    user_lang_path = lang_path(data["user_lang"])
    user_lang = (
        {i.msgid: i.msgstr for i in polib.pofile(user_lang_path) if i.msgid in data["texts"]} if user_lang_path else {}
    )

    def try_current(o):
        try:
            return _(o)
        except:
            return None

    translation = {
        key: (
            user_lang[key] if key in user_lang else None,
            dest_lang[key] if key in dest_lang else None,
            try_current(key),
        )
        for key, value in data.pop("texts").iteritems()
    }

    return render_template(
        "admin/translation_review.html",
        page_title=_("admin_translation_review"),
        title=admin_title("admin_translation_review"),
        form=form,
        select=select,
        data=data,
        page=page,
        list_mode=mode,
        page_size=size,
        field_keys=",".join(translation.iterkeys()),
        fields=translation,
    )