def translation_memory(request): """Get translations from internal translations memory.""" try: text = request.GET["text"] locale = request.GET["locale"] pk = int(request.GET["pk"]) except (MultiValueDictKeyError, ValueError) as e: return JsonResponse( { "status": False, "message": "Bad Request: {error}".format(error=e) }, status=400, ) try: locale = Locale.objects.get(code=locale) except Locale.DoesNotExist as e: return JsonResponse( { "status": False, "message": "Not Found: {error}".format(error=e) }, status=404, ) data = get_translation_memory_data(text, locale, pk) return JsonResponse(data, safe=False)
def translation_memory(request): """Get translations from internal translations memory.""" try: text = request.GET['text'] locale = request.GET['locale'] pk = request.GET['pk'] except MultiValueDictKeyError as e: return JsonResponse( { 'status': False, 'message': 'Bad Request: {error}'.format(error=e), }, status=400) try: locale = Locale.objects.get(code=locale) except Locale.DoesNotExist as e: return JsonResponse( { 'status': False, 'message': 'Not Found: {error}'.format(error=e), }, status=404) data = get_translation_memory_data(text, locale, pk) return JsonResponse(data, safe=False)
def get_translations(entity, locale): """ Get pretranslations for the entity-locale pair :arg Entity entity: the Entity object :arg Locale locale: the Locale object :returns: a list of tuple with: - a pretranslation of the entity - plural form - user - tm_user/gt_user """ tm_user = User.objects.get(email="*****@*****.**") gt_user = User.objects.get(email="*****@*****.**") strings = [] plural_forms = range(0, locale.nplurals or 1) # Try to get matches from translation_memory tm_response = get_translation_memory_data( text=entity.string, locale=locale, ) tm_response = [t for t in tm_response if int(t["quality"]) == 100] if tm_response: if entity.string_plural == "": strings = [(tm_response[0]["target"], None, tm_user)] else: for plural_form in plural_forms: strings.append( (tm_response[0]["target"], plural_form, tm_user)) # Else fetch from google translate elif locale.google_translate_code: gt_response = get_google_translate_data( text=entity.string, locale_code=locale.google_translate_code, ) if gt_response["status"]: if entity.string_plural == "": strings = [(gt_response["translation"], None, gt_user)] else: for plural_form in plural_forms: strings.append( (gt_response["translation"], plural_form, gt_user)) return strings
def translation_memory(request): """Get translations from internal translations memory.""" try: text = request.GET["text"] locale = Locale.objects.get(code=request.GET["locale"]) pk = request.GET.get("pk", None) if pk is not None: pk = int(pk) except (Locale.DoesNotExist, MultiValueDictKeyError, ValueError) as e: return JsonResponse( { "status": False, "message": f"Bad Request: {e}" }, status=400, ) data = get_translation_memory_data(text, locale, pk) return JsonResponse(data, safe=False)