本文整理匯總了Python中weblate.trans.forms.TranslationForm類的典型用法代碼示例。如果您正苦於以下問題:Python TranslationForm類的具體用法?Python TranslationForm怎麽用?Python TranslationForm使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了TranslationForm類的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: handle_translate
def handle_translate(request, translation, this_unit_url, next_unit_url):
"""Save translation or suggestion to database and backend."""
# Antispam protection
antispam = AntispamForm(request.POST)
if not antispam.is_valid():
# Silently redirect to next entry
return HttpResponseRedirect(next_unit_url)
form = TranslationForm(
request.user, translation, None, request.POST
)
if not form.is_valid():
show_form_errors(request, form)
return None
unit = form.cleaned_data['unit']
go_next = True
if 'suggest' in request.POST:
go_next = perform_suggestion(unit, form, request)
elif not request.user.has_perm('unit.edit', unit):
messages.error(
request,
_('Insufficient privileges for saving translations.')
)
else:
go_next = perform_translation(unit, form, request)
# Redirect to next entry
if go_next:
return HttpResponseRedirect(next_unit_url)
return HttpResponseRedirect(this_unit_url)
示例2: save_zen
def save_zen(request, project, subproject, lang):
'''
Save handler for zen mode.
'''
translation = get_translation(request, project, subproject, lang)
user_locked = translation.is_user_locked(request.user)
form = TranslationForm(translation, None, request.POST)
if not can_translate(request.user, translation):
messages.error(
request,
_('You don\'t have privileges to save translations!')
)
elif not form.is_valid():
messages.error(request, _('Failed to save translation!'))
elif not user_locked:
unit = form.cleaned_data['unit']
perform_translation(unit, form, request)
return render(
request,
'zen-response.html',
{},
)
示例3: handle_translate
def handle_translate(translation, request, user_locked,
this_unit_url, next_unit_url):
'''
Saves translation or suggestion to database and backend.
'''
# Antispam protection
antispam = AntispamForm(request.POST)
if not antispam.is_valid():
# Silently redirect to next entry
return HttpResponseRedirect(next_unit_url)
# Check whether translation is not outdated
translation.check_sync()
form = TranslationForm(translation, None, request.POST)
if not form.is_valid():
return
unit = form.cleaned_data['unit']
go_next = True
if 'suggest' in request.POST:
go_next = perform_suggestion(unit, form, request)
elif (translation.is_template() and not
request.user.has_perm('trans.save_template')):
# Need privilege to save
messages.error(
request,
_('You don\'t have privileges to save templates!')
)
elif not request.user.has_perm('trans.save_translation'):
# Need privilege to save
messages.error(
request,
_('You don\'t have privileges to save translations!')
)
elif (unit.only_vote_suggestions() and not
request.user.has_perm('trans.override_suggestion')):
messages.error(
request,
_('Only suggestions are allowed in this translation!')
)
elif not user_locked:
# Custom commit message
message = request.POST.get('commit_message')
if message and message != unit.translation.commit_message:
# Commit pending changes so that they don't get new message
unit.translation.commit_pending(request, request.user)
# Store new commit message
unit.translation.commit_message = message
unit.translation.save()
go_next = perform_translation(unit, form, request)
# Redirect to next entry
if go_next:
return HttpResponseRedirect(next_unit_url)
else:
return HttpResponseRedirect(this_unit_url)
示例4: save_zen
def save_zen(request, project, subproject, lang):
"""
Save handler for zen mode.
"""
translation = get_translation(request, project, subproject, lang)
form = TranslationForm(translation, None, request.POST)
if not form.is_valid():
messages.error(_("Failed to save translation!"))
else:
unit = form.cleaned_data["unit"]
perform_translation(unit, form, request)
return render(request, "zen-response.html")
示例5: save_zen
def save_zen(request, project, subproject, lang):
'''
Save handler for zen mode.
'''
translation = get_translation(request, project, subproject, lang)
form = TranslationForm(translation, None, request.POST)
if not form.is_valid():
messages.error(request, _('Failed to save translation!'))
else:
unit = form.cleaned_data['unit']
perform_translation(unit, form, request)
return render(
request,
'zen-response.html',
)
示例6: handle_translate
def handle_translate(translation, request, user_locked,
this_unit_url, next_unit_url):
"""Save translation or suggestion to database and backend."""
# Antispam protection
antispam = AntispamForm(request.POST)
if not antispam.is_valid():
# Silently redirect to next entry
return HttpResponseRedirect(next_unit_url)
# Check whether translation is not outdated
translation.check_sync()
form = TranslationForm(
request.user.profile, translation, None, request.POST
)
if not form.is_valid():
show_form_errors(request, form)
return
unit = form.cleaned_data['unit']
go_next = True
if 'suggest' in request.POST:
go_next = perform_suggestion(unit, form, request)
elif not can_translate(request.user, unit.translation):
messages.error(
request,
_('You don\'t have privileges to save translations!')
)
elif not user_locked:
# Custom commit message
message = request.POST.get('commit_message')
if message is not None and message != unit.translation.commit_message:
# Commit pending changes so that they don't get new message
unit.translation.commit_pending(request, request.user)
# Store new commit message
unit.translation.commit_message = message
unit.translation.save()
go_next = perform_translation(unit, form, request)
# Redirect to next entry
if go_next:
return HttpResponseRedirect(next_unit_url)
else:
return HttpResponseRedirect(this_unit_url)
示例7: save_zen
def save_zen(request, project, component, lang):
"""Save handler for zen mode."""
def render_mesage(message):
return render_to_string(
'message.html',
{'tags': message.tags, 'message': message.message}
)
translation = get_translation(request, project, component, lang)
form = TranslationForm(
request.user, translation, None, request.POST
)
translationsum = ''
if not form.is_valid():
show_form_errors(request, form)
elif not request.user.has_perm('unit.edit', form.cleaned_data['unit']):
messages.error(
request, _('Insufficient privileges for saving translations.')
)
else:
unit = form.cleaned_data['unit']
perform_translation(unit, form, request)
translationsum = hash_to_checksum(unit.get_target_hash())
response = {
'messages': '',
'state': 'success',
'translationsum': translationsum,
}
storage = get_messages(request)
if storage:
response['messages'] = '\n'.join([render_mesage(m) for m in storage])
tags = {m.tags for m in storage}
if 'error' in tags:
response['state'] = 'danger'
elif 'warning' in tags:
response['state'] = 'warning'
elif 'info' in tags:
response['state'] = 'info'
return JsonResponse(data=response)
示例8: save_zen
def save_zen(request, project, subproject, lang):
"""
Save handler for zen mode.
"""
translation = get_translation(request, project, subproject, lang)
user_locked = translation.is_user_locked(request.user)
form = TranslationForm(translation, None, request.POST)
if not can_translate(request.user, translation):
messages.error(request, _("You don't have privileges to save translations!"))
elif not form.is_valid():
messages.error(request, _("Failed to save translation!"))
elif not user_locked:
unit = form.cleaned_data["unit"]
perform_translation(unit, form, request)
return render(request, "zen-response.html", {})
示例9: handle_translate
def handle_translate(translation, request, user_locked, this_unit_url, next_unit_url):
"""
Saves translation or suggestion to database and backend.
"""
# Antispam protection
antispam = AntispamForm(request.POST)
if not antispam.is_valid():
# Silently redirect to next entry
return HttpResponseRedirect(next_unit_url)
# Check whether translation is not outdated
translation.check_sync()
form = TranslationForm(translation, None, request.POST)
if not form.is_valid():
return
unit = form.cleaned_data["unit"]
go_next = True
if "suggest" in request.POST:
go_next = perform_suggestion(unit, form, request)
elif not request.user.has_perm("trans.save_translation"):
# Need privilege to save
messages.error(request, _("You don't have privileges to save translations!"))
elif unit.only_vote_suggestions() and not request.user.has_perm("trans.save_translation"):
messages.error(request, _("Only suggestions are allowed in this translation!"))
elif not user_locked:
# Custom commit message
if "commit_message" in request.POST and request.POST["commit_message"] != unit.translation.commit_message:
# Commit pending changes so that they don't get new message
unit.translation.commit_pending(request, request.user)
# Store new commit message
unit.translation.commit_message = request.POST["commit_message"]
unit.translation.save()
go_next = perform_translation(unit, form, request)
# Redirect to next entry
if go_next:
return HttpResponseRedirect(next_unit_url)
else:
return HttpResponseRedirect(this_unit_url)
示例10: handle_translate
def handle_translate(translation, request, this_unit_url, next_unit_url):
"""Save translation or suggestion to database and backend."""
# Antispam protection
antispam = AntispamForm(request.POST)
if not antispam.is_valid():
# Silently redirect to next entry
return HttpResponseRedirect(next_unit_url)
form = TranslationForm(
request.user, translation, None, request.POST
)
if not form.is_valid():
show_form_errors(request, form)
return None
unit = form.cleaned_data['unit']
go_next = True
if 'suggest' in request.POST:
go_next = perform_suggestion(unit, form, request)
elif not request.user.has_perm('unit.edit', unit):
messages.error(
request,
_('Insufficient privileges for saving translations.')
)
else:
# Custom commit message
message = request.POST.get('commit_message')
if message is not None and message != unit.translation.commit_message:
# Commit pending changes so that they don't get new message
unit.translation.commit_pending(request)
# Store new commit message
unit.translation.commit_message = message
unit.translation.save()
go_next = perform_translation(unit, form, request)
# Redirect to next entry
if go_next:
return HttpResponseRedirect(next_unit_url)
return HttpResponseRedirect(this_unit_url)
示例11: translate
def translate(request, project, subproject, lang):
obj = get_object_or_404(Translation, language__code = lang, subproject__slug = subproject, subproject__project__slug = project, enabled = True)
if obj.subproject.locked:
messages.error(request, _('This translation is currently locked for updates!'))
if request.user.is_authenticated():
profile = request.user.get_profile()
else:
profile = None
secondary = None
unit = None
rqtype, direction, pos, search_query, search_exact, search_source, search_target, search_context, search_url = parse_search_url(request)
# Any form submitted?
if request.method == 'POST':
form = TranslationForm(request.POST)
if form.is_valid() and not obj.subproject.locked:
# Check whether translation is not outdated
obj.check_sync()
try:
try:
unit = Unit.objects.get(checksum = form.cleaned_data['checksum'], translation = obj)
except Unit.MultipleObjectsReturned:
# Possible temporary inconsistency caused by ongoing update of repo,
# let's pretend everyting is okay
unit = Unit.objects.filter(checksum = form.cleaned_data['checksum'], translation = obj)[0]
if 'suggest' in request.POST:
# Handle suggesion saving
user = request.user
if isinstance(user, AnonymousUser):
user = None
if form.cleaned_data['target'] == len(form.cleaned_data['target']) * ['']:
messages.error(request, _('Your suggestion is empty!'))
# Stay on same entry
return HttpResponseRedirect('%s?type=%s&pos=%d&dir=stay%s' % (
obj.get_translate_url(),
rqtype,
pos,
search_url
))
Suggestion.objects.create(
target = join_plural(form.cleaned_data['target']),
checksum = unit.checksum,
language = unit.translation.language,
project = unit.translation.subproject.project,
user = user)
# Update suggestion stats
if profile is not None:
profile.suggested += 1
profile.save()
elif not request.user.is_authenticated():
# We accept translations only from authenticated
messages.error(request, _('You need to log in to be able to save translations!'))
elif not request.user.has_perm('trans.save_translation'):
# Need privilege to save
messages.error(request, _('You don\'t have privileges to save translations!'))
else:
# Remember old checks
oldchecks = set(unit.active_checks().values_list('check', flat = True))
# Update unit and save it
unit.target = join_plural(form.cleaned_data['target'])
unit.fuzzy = form.cleaned_data['fuzzy']
unit.save_backend(request)
# Update stats
profile.translated += 1
profile.save()
# Get new set of checks
newchecks = set(unit.active_checks().values_list('check', flat = True))
# Did we introduce any new failures?
if newchecks > oldchecks:
# Show message to user
messages.error(request, _('Some checks have failed on your translation!'))
# Stay on same entry
return HttpResponseRedirect('%s?type=%s&pos=%d&dir=stay%s' % (
obj.get_translate_url(),
rqtype,
pos,
search_url
))
# Redirect to next entry
return HttpResponseRedirect('%s?type=%s&pos=%d%s' % (
obj.get_translate_url(),
rqtype,
pos,
search_url
))
except Unit.DoesNotExist:
logger.error('message %s disappeared!', form.cleaned_data['checksum'])
messages.error(request, _('Message you wanted to translate is no longer available!'))
# Handle translation merging
if 'merge' in request.GET:
if not request.user.has_perm('trans.save_translation'):
# Need privilege to save
messages.error(request, _('You don\'t have privileges to save translations!'))
else:
#.........這裏部分代碼省略.........