SurveyJS Python Backend Integration with Django /

Translate with AI

Commercial AI Provider Required

Translate with AI

Translate form text with AI and let authors review the results. Your backend sends titles, choices, and other text to your chosen provider, keeping its API key on the server.

Documentation

Try this

  1. In the Translations tab, use Add Language to add another language. Click Auto-translate All beside a language, then again in the dialog to translate missing text.
  2. Review and edit the generated translations in the dialog, then click Apply to add them to the form. The sample already has a French title to demonstrate keeping existing translations.
  3. Changes are saved automatically after you click Apply. Reload the page to confirm the translations were saved.
  4. Open the Preview tab and use its language selector to view the translated form or compare it with the original.
  5. Open Requests to inspect the translation request and schema save.

SurveyJS commercial products require a developer license for production use. See SurveyJS Licensing for details.

Running main@fc1a5b4

    No requests

      What the server stored

      JSON definition · shared/definitions/ai-translation.json View on GitHub
      {
        "title": {
          "default": "Workshop registration",
          "fr": "Inscription à l'atelier"
        },
        "description": "Choose a session and leave your contact details",
        "elements": [
          {
            "type": "text",
            "name": "full_name",
            "title": "Full name",
            "isRequired": true
          },
          {
            "type": "text",
            "name": "email",
            "title": "Email address",
            "inputType": "email",
            "isRequired": true
          },
          {
            "type": "radiogroup",
            "name": "session",
            "title": "Preferred session",
            "isRequired": true,
            "choices": [
              {
                "value": "morning",
                "text": "Morning"
              },
              {
                "value": "afternoon",
                "text": "Afternoon"
              }
            ]
          }
        ]
      }
      Client · shared/client/ai-translation.js · sjs:ai-translation.client View on GitHub
      // Return translated strings in the original order, and always finish the callback
      creator.onMachineTranslate.add(async (_, options) => {
        showError("");
        try {
          const response = await fetch("/api/translate", {
            method: "POST", headers: { "Content-Type": "application/json" },
            body: JSON.stringify({ strings: options.strings, from: options.fromLocale, to: options.toLocale })
          });
          if (!response.ok) throw await responseError(response, "AI translation failed. Please try again.");
          const strings = await response.json();
          if (!Array.isArray(strings) || strings.length !== options.strings.length || strings.some(text => typeof text !== "string"))
            throw new Error("The translation provider returned an invalid result. Please try again.");
          options.callback(strings);
        } catch (error) {
          options.callback([]);
          showError(`${translationError(error)} Existing translations are kept.`);
          note(error.message, "error");
        }
      });
      Server · examples/views/ai_translation.py · sjs:ai-translation.server View on GitHub
      # POST /api/translate — strings in, translated strings out (same order); nothing goes to SurveyJS
      async def translate(request: HttpRequest) -> JsonResponse:
          if not settings.AI_API_KEY:
              return JsonResponse({"error": "Set AI_API_KEY to enable translation"}, status=501)
          body = read_json(request)
          strings = body.get("strings", []) if isinstance(body, dict) else None
          if not isinstance(strings, list) or not all(isinstance(text, str) for text in strings):
              return JsonResponse({"error": "Expected { strings, from, to }"}, status=400)
      
          translated = await AiTranslation().translate(strings, body.get("from", ""), body.get("to", ""))   # your provider
          if not isinstance(translated, list) or len(translated) != len(strings) \
                  or not all(isinstance(text, str) for text in translated):
              return JsonResponse({"error": "The AI provider returned an unexpected answer"}, status=502)
      
          return JsonResponse(translated, safe=False)   # what options.callback() takes