def handle(self, *args, **options): # Get translation object translation = self.get_translation(**options) # Get user try: user = User.objects.get(username=options['user']) Profile.objects.get_or_create(user=user) except User.DoesNotExist: raise CommandError('User does not exist!') if options['source']: parts = options['source'].split('/') if len(parts) != 2: raise CommandError('Invalid source component specified!') try: subproject = SubProject.objects.get( project__slug=parts[0], slug=parts[1], ) except SubProject.DoesNotExist: raise CommandError('No matching source component found!') source = subproject.id else: source = '' if options['inconsistent']: filter_type = 'check:inconsistent' elif options['overwrite']: filter_type = 'all' else: filter_type = 'todo' auto = AutoTranslate(user, translation, filter_type) auto.process_others(source, check_acl=False) self.stdout.write('Updated {0} units'.format(auto.updated))
def handle(self, *args, **options): # Get translation object translation = self.get_translation(**options) # Get user try: user = User.objects.get(username=options['user']) Profile.objects.get_or_create(user=user) except User.DoesNotExist: raise CommandError('User does not exist!') if options['source']: parts = options['source'].split('/') if len(parts) != 2: raise CommandError('Invalid source component specified!') try: component = Component.objects.get( project__slug=parts[0], slug=parts[1], ) except Component.DoesNotExist: raise CommandError('No matching source component found!') source = component.id else: source = '' if options['inconsistent']: filter_type = 'check:inconsistent' elif options['overwrite']: filter_type = 'all' else: filter_type = 'todo' auto = AutoTranslate(user, translation, filter_type) auto.process_others(source, check_acl=False) self.stdout.write('Updated {0} units'.format(auto.updated))
def handle(self, *args, **options): # Get translation object translation = self.get_translation(**options) # Get user try: user = User.objects.get(username=options['user']) except User.DoesNotExist: raise CommandError('User does not exist!') if options['source']: parts = options['source'].split('/') if len(parts) != 2: raise CommandError('Invalid source component specified!') try: component = Component.objects.get( project__slug=parts[0], slug=parts[1], ) except Component.DoesNotExist: raise CommandError('No matching source component found!') source = component.id else: source = '' if options['mt']: for translator in options['mt']: if translator not in MACHINE_TRANSLATION_SERVICES.keys(): raise CommandError( 'Machine translation {} is not available'.format( translator ) ) if options['inconsistent']: filter_type = 'check:inconsistent' elif options['overwrite']: filter_type = 'all' else: filter_type = 'todo' # Create fake request object request = HttpRequest() request.user = user auto = AutoTranslate(user, translation, filter_type, request) if options['mt']: auto.process_mt(options['mt'], options['threshold']) else: auto.process_others(source, check_acl=False) self.stdout.write('Updated {0} units'.format(auto.updated))
def auto_translation(request, project, component, lang): translation = get_translation(request, project, component, lang) project = translation.component.project if not request.user.has_perm('translation.auto', project): raise PermissionDenied() autoform = AutoForm(request.user, translation, request.POST) if translation.component.locked or not autoform.is_valid(): messages.error(request, _('Failed to process form!')) show_form_errors(request, autoform) return redirect(translation) auto = AutoTranslate( request.user, translation, autoform.cleaned_data['type'], request=request ) if autoform.cleaned_data['auto_source'] == 'mt': auto.process_mt( autoform.cleaned_data['engines'], autoform.cleaned_data['threshold'], ) else: auto.process_others( autoform.cleaned_data['component'], ) import_message( request, auto.updated, _('Automatic translation completed, no strings were updated.'), ungettext( 'Automatic translation completed, %d string was updated.', 'Automatic translation completed, %d strings were updated.', auto.updated ) ) return redirect(translation)
def auto_translation(request, project, component, lang): translation = get_translation(request, project, component, lang) project = translation.component.project if not request.user.has_perm('translation.auto', project): raise PermissionDenied() autoform = AutoForm(translation, request.user, request.POST) if translation.component.locked or not autoform.is_valid(): messages.error(request, _('Failed to process form!')) show_form_errors(request, autoform) return redirect(translation) auto = AutoTranslate(request.user, translation, autoform.cleaned_data['type'], request=request) if autoform.cleaned_data['auto_source'] == 'mt': auto.process_mt( autoform.cleaned_data['engines'], autoform.cleaned_data['threshold'], ) else: auto.process_others(autoform.cleaned_data['component'], ) import_message( request, auto.updated, _('Automatic translation completed, no strings were updated.'), ungettext('Automatic translation completed, %d string was updated.', 'Automatic translation completed, %d strings were updated.', auto.updated)) return redirect(translation)
def auto_translate( user_id, translation_id, mode, filter_type, auto_source, component, engines, threshold, ): if user_id: user = User.objects.get(pk=user_id) else: user = None with override(user.profile.language if user else "en"): auto = AutoTranslate(user, Translation.objects.get(pk=translation_id), filter_type, mode) if auto_source == "mt": auto.process_mt(engines, threshold) else: auto.process_others(component) if auto.updated == 0: return _( "Automatic translation completed, no strings were updated.") return (ungettext( "Automatic translation completed, %d string was updated.", "Automatic translation completed, %d strings were updated.", auto.updated, ) % auto.updated)
def auto_translate( user_id, translation_id, mode, filter_type, auto_source, component, engines, threshold, ): if user_id: user = User.objects.get(pk=user_id) else: user = None with override(user.profile.language if user else "en"): translation = Translation.objects.get(pk=translation_id) translation.log_info("starting automatic translation %s", current_task.request.id) auto = AutoTranslate(user, translation, filter_type, mode) if auto_source == "mt": auto.process_mt(engines, threshold) else: auto.process_others(component) translation.log_info("completed automatic translation") if auto.updated == 0: return _( "Automatic translation completed, no strings were updated.") return (ngettext( "Automatic translation completed, %d string was updated.", "Automatic translation completed, %d strings were updated.", auto.updated, ) % auto.updated)
def auto_translate( user_id: int, translation_id: int, mode: str, filter_type: str, auto_source: str, component: Optional[int], engines: List[str], threshold: int, translation: Optional[Translation] = None, component_wide: bool = False, ): if translation is None: translation = Translation.objects.get(pk=translation_id) if user_id: user = User.objects.get(pk=user_id) else: user = None translation.log_info( "starting automatic translation %s: %s: %s", current_task.request.id, auto_source, ", ".join(engines) if engines else component, ) with translation.component.lock, override( user.profile.language if user else "en"): auto = AutoTranslate(user, translation, filter_type, mode, component_wide=component_wide) if auto_source == "mt": auto.process_mt(engines, threshold) else: auto.process_others(component) translation.log_info("completed automatic translation") if auto.updated == 0: message = _( "Automatic translation completed, no strings were updated.") else: message = (ngettext( "Automatic translation completed, %d string was updated.", "Automatic translation completed, %d strings were updated.", auto.updated, ) % auto.updated) return {"translation": translation_id, "message": message}
def handle(self, *args, **options): # Get translation object translation = self.get_translation(**options) # Get user try: user = User.objects.get(username=options["user"]) except User.DoesNotExist: raise CommandError("User does not exist!") if options["source"]: parts = options["source"].split("/") if len(parts) != 2: raise CommandError("Invalid source component specified!") try: component = Component.objects.get(project__slug=parts[0], slug=parts[1]) except Component.DoesNotExist: raise CommandError("No matching source component found!") source = component.id else: source = "" if options["mt"]: for translator in options["mt"]: if translator not in MACHINE_TRANSLATION_SERVICES.keys(): raise CommandError( f"Machine translation {translator} is not available" ) if options["mode"] not in ("translate", "fuzzy", "suggest"): raise CommandError("Invalid translation mode specified!") mode = options["mode"] if options["inconsistent"]: filter_type = "check:inconsistent" elif options["overwrite"]: filter_type = "all" else: filter_type = "todo" auto = AutoTranslate(user, translation, filter_type, mode) if options["mt"]: auto.process_mt(options["mt"], options["threshold"]) else: auto.process_others(source) self.stdout.write(f"Updated {auto.updated} units")
def auto_translate( user_id, translation_id, mode, filter_type, auto_source, component, engines, threshold, ): auto = AutoTranslate( User.objects.get(pk=user_id) if user_id else None, Translation.objects.get(pk=translation_id), filter_type, mode, ) if auto_source == 'mt': auto.process_mt(engines, threshold) else: auto.process_others(component)