コード例 #1
0
ファイル: views.py プロジェクト: hyperweek/django-inlinetrans
def set_new_translation(request):
    """
    Post to include a new translation for a msgid
    """

    message = "SOME ERRORS"
    if not request.POST:
        message = "ERROR: need a msgid and msgstr on post"
    else:
        msgid = smart_str(request.POST["msgid"])
        msgstr = smart_str(request.POST["msgstr"])
        lang = get_language()
        pos = find_pos(lang, include_djangos=True)
        if pos:
            for file_po in pos:
                candidate = pofile(file_po)
                poentry = candidate.find(msgid)
                if poentry:
                    selected_pofile = candidate
                    poentry.msgstr = msgstr
                    if "fuzzy" in poentry.flags:
                        poentry.flags.remove("fuzzy")
                    po_filename = file_po
                    break
            format_errors = validate_format(selected_pofile)
            if poentry and not format_errors:
                try:
                    selected_pofile.metadata["Last-Translator"] = smart_str(
                        "%s %s <%s>" % (request.user.first_name, request.user.last_name, request.user.email)
                    )
                    selected_pofile.metadata["X-Translated-Using"] = smart_str(
                        "inlinetrans %s" % inlinetrans.get_version(False)
                    )
                    selected_pofile.metadata["PO-Revision-Date"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M%z")
                except UnicodeDecodeError:
                    pass
                selected_pofile.save()
                selected_pofile.save_as_mofile(po_filename.replace(".po", ".mo"))
                message = "OK"

    if message == "OK":
        return render_to_response(
            "inlinetrans/response.html", {"message": message}, context_instance=RequestContext(request)
        )
    else:
        return HttpResponseBadRequest(
            render_to_response(
                "inlinetrans/response.html", {"message": message}, context_instance=RequestContext(request)
            )
        )
コード例 #2
0
def set_new_translation(request):
    """
    Post to include a new translation for a msgid
    """

    if not request.user.is_staff:
        return HttpResponseForbidden(_('You have no permission to update translation catalogs'))
    if not request.POST:
        return HttpResponseBadRequest(render_to_response('inlinetrans/response.html',
                                      {'message': _('Invalid request method')},
                                      context_instance=RequestContext(request)))
    else:
        result = {'errors': True,
                  'question': False,
                  'message': _('Unknow error'),
                 }
        selected_pofile = None
        msgid = smart_str(request.POST['msgid'])
        msgstr = smart_str(request.POST['msgstr'])
        retry = smart_str(request.POST['retry'])
        lang = get_language()

        # We try to update the catalog
        if retry != 'false':
            root_path = os.path.dirname(os.path.normpath(os.sys.modules[settings.SETTINGS_MODULE].__file__))
            locale_path = os.path.dirname(os.path.normpath(os.sys.modules[settings.SETTINGS_MODULE].__file__))
            make_messages(lang, extensions=['.html'], root_path=root_path, locale_path=locale_path)

        pos = find_pos(lang, include_djangos=True)
        if pos:
            for file_po in pos:
                candidate = pofile(file_po)
                poentry = candidate.find(msgid)
                if poentry:
                    selected_pofile = candidate
                    poentry.msgstr = msgstr
                    if 'fuzzy' in poentry.flags:
                        poentry.flags.remove('fuzzy')
                    po_filename = file_po
                    break
            # We can not find the msgid in any of the catalogs
            if not selected_pofile:
                result['message'] = _('"%(msgid)s" not found in any catalog' % {'msgid': msgid})
                if retry == 'false':
                    result['question'] = _('Do you want to update the catalog (this could take longer) and try again?')
                return HttpResponse(simplejson.dumps(result), mimetype='text/plain')

            format_errors = validate_format(selected_pofile)
            if format_errors:
                result['message'] = format_errors
                return HttpResponse(simplejson.dumps(result), mimetype='text/plain')

            if poentry and not format_errors:
                try:
                    selected_pofile.metadata['Last-Translator'] = smart_str("%s %s <%s>" % (request.user.first_name, request.user.last_name, request.user.email))
                    selected_pofile.metadata['X-Translated-Using'] = smart_str("inlinetrans %s" % inlinetrans.get_version(False))
                    selected_pofile.metadata['PO-Revision-Date'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M%z')
                except UnicodeDecodeError:
                    pass
                selected_pofile.save()
                selected_pofile.save_as_mofile(po_filename.replace('.po', '.mo'))
                result['errors'] = False
                result['message'] = _('Catalog updated successfully')
            elif not poentry:
                result['message'] = _('PO entry not found')
    return HttpResponse(simplejson.dumps(result), mimetype='text/plain')
コード例 #3
0
ファイル: views.py プロジェクト: Seha16/django-inlinetrans
def set_new_translation(request):
    """
    Post to include a new translation for a msgid
    """
    if not get_user_can_translate(request.user):
        return HttpResponseForbidden(
            _('You have no permission to update translation catalogs'))
    if not request.POST:
        return HttpResponseBadRequest(
            render_to_response('inlinetrans/response.html',
                               {'message': _('Invalid request method')},
                               context_instance=RequestContext(request)))
    else:
        result = {
            'errors': True,
            'question': False,
            'message': _('Unknow error')
        }
        selected_pofile = None
        msgid = smart_str(request.POST['msgid'])
        msgstr = smart_str(request.POST['msgstr'])
        retry = smart_str(request.POST['retry'])
        lang = get_language()

        # We try to update the catalog
        if retry != 'false':
            root_path = os.path.dirname(
                os.path.normpath(
                    os.sys.modules[settings.SETTINGS_MODULE].__file__))
            locale_path = os.path.dirname(
                os.path.normpath(
                    os.sys.modules[settings.SETTINGS_MODULE].__file__))
            makemessages(lang,
                         extensions=['.html'],
                         root_path=root_path,
                         locale_path=locale_path)

        pos = find_pos(lang, include_djangos=True)
        if pos:
            for file_po in pos:
                candidate = pofile(file_po)
                poentry = candidate.find(msgid)
                if poentry:
                    selected_pofile = candidate
                    poentry.msgstr = msgstr
                    if 'fuzzy' in poentry.flags:
                        poentry.flags.remove('fuzzy')
                    po_filename = file_po
                    break
            # We can not find the msgid in any of the catalogs
            if not selected_pofile:
                result['message'] = _('"%(msgid)s" not found in any catalog' %
                                      {'msgid': msgid})
                if retry == 'false':
                    result['question'] = _(
                        'Do you want to update the catalog (this could take longer) and try again?'
                    )
                return HttpResponse(simplejson.dumps(result),
                                    mimetype='text/plain')

            format_errors = validate_format(selected_pofile)
            if format_errors:
                result['message'] = format_errors
                return HttpResponse(simplejson.dumps(result),
                                    mimetype='text/plain')

            if poentry and not format_errors:
                try:
                    selected_pofile.metadata['Last-Translator'] = smart_str(
                        "%s %s <%s>" %
                        (request.user.first_name, request.user.last_name,
                         request.user.email))
                    selected_pofile.metadata['X-Translated-Using'] = smart_str(
                        "inlinetrans %s" % inlinetrans.get_version(False))
                    selected_pofile.metadata[
                        'PO-Revision-Date'] = datetime.datetime.now().strftime(
                            '%Y-%m-%d %H:%M%z')
                except UnicodeDecodeError:
                    pass
                selected_pofile.save()
                selected_pofile.save_as_mofile(
                    po_filename.replace('.po', '.mo'))
                result['errors'] = False
                result['message'] = _('Catalog updated successfully')
            elif not poentry:
                result['message'] = _('PO entry not found')
    return HttpResponse(simplejson.dumps(result), mimetype='text/plain')