- Translations completed for Front-End, Configs (e.g. Forms) and free text.

- Allowed_languages and default_language now part of Tenant Make iso Tenant
- Introduction of Translation into Traicie Selection Specialist
This commit is contained in:
Josako
2025-06-30 14:20:17 +02:00
parent 4338f09f5c
commit fbc9f44ac8
34 changed files with 1206 additions and 220 deletions

View File

@@ -1,3 +1,4 @@
import json
import uuid
from flask import Blueprint, render_template, request, session, current_app, jsonify, Response, stream_with_context
from sqlalchemy.exc import SQLAlchemyError
@@ -6,9 +7,12 @@ from common.extensions import db
from common.models.user import Tenant, SpecialistMagicLinkTenant, TenantMake
from common.models.interaction import SpecialistMagicLink, Specialist, ChatSession, Interaction
from common.services.interaction.specialist_services import SpecialistServices
from common.utils.business_event import BusinessEvent
from common.utils.business_event_context import current_event
from common.utils.database import Database
from common.utils.chat_utils import get_default_chat_customisation
from common.utils.execution_progress import ExecutionProgressTracker
from common.extensions import cache_manager
chat_bp = Blueprint('chat_bp', __name__, url_prefix='/chat')
@@ -103,13 +107,20 @@ def chat(magic_link_code):
"auto_scroll": True
}
specialist_config = specialist.configuration
if isinstance(specialist_config, str):
specialist_config = json.loads(specialist_config)
welcome_message = specialist_config.get('welcome_message', 'Hello! How can I help you today?')
return render_template('chat.html',
tenant=tenant,
tenant_make=tenant_make,
specialist=specialist,
customisation=customisation,
messages=[customisation['welcome_message']],
settings=settings
messages=[welcome_message],
settings=settings,
config=current_app.config
)
except Exception as e:
@@ -149,6 +160,11 @@ def send_message():
if form_values:
specialist_args['form_values'] = form_values
# Add language to specialist arguments if present
user_language = data.get('language')
if user_language:
specialist_args['language'] = user_language
current_app.logger.debug(f"Sending message to specialist: {specialist_id} for tenant {tenant_id}\n"
f" with args: {specialist_args}\n"
f"with session ID: {chat_session_id}")
@@ -255,3 +271,78 @@ def task_progress_stream(task_id):
current_app.logger.error(f"Failed to start progress stream: {str(e)}")
return jsonify({'error': str(e)}), 500
@chat_bp.route('/api/translate', methods=['POST'])
def translate():
"""
API endpoint om tekst te vertalen naar een doeltaal
Parameters (JSON):
- text: de tekst die moet worden vertaald
- target_lang: de ISO 639-1 taalcode waarnaar moet worden vertaald
- source_lang: (optioneel) de ISO 639-1 taalcode van de brontaal
- context: (optioneel) context voor de vertaling
Returns:
JSON met vertaalde tekst
"""
try:
tenant_id = session.get('tenant', {}).get('id')
with BusinessEvent('Client Translation Service', tenant_id):
with current_event.create_span('Front-End Translation'):
data = request.json
# Valideer vereiste parameters
if not data or 'text' not in data or 'target_lang' not in data:
return jsonify({
'success': False,
'error': 'Required parameters missing: text and/or target_lang'
}), 400
text = data.get('text')
target_lang = data.get('target_lang')
source_lang = data.get('source_lang')
context = data.get('context')
# Controleer of tekst niet leeg is
if not text.strip():
return jsonify({
'success': False,
'error': 'Text to translate cannot be empty'
}), 400
# Haal tenant_id uit sessie
tenant_id = session.get('tenant', {}).get('id')
if not tenant_id:
current_app.logger.error("No tenant ID found in session")
# Fallback naar huidige app tenant_id
tenant_id = getattr(current_app, 'tenant_id', None)
# Haal vertaling op (uit cache of genereer nieuw)
translation = cache_manager.translation_cache.get_translation(
text=text,
target_lang=target_lang,
source_lang=source_lang,
context=context
)
if not translation:
return jsonify({
'success': False,
'error': 'No translation found in cache. Please try again later.'
}), 500
# Retourneer het resultaat
return jsonify({
'success': True,
'translated_text': translation.translated_text,
'source_language': translation.source_language,
'target_language': translation.target_language
})
except Exception as e:
current_app.logger.error(f"Error translating: {str(e)}", exc_info=True)
return jsonify({
'success': False,
'error': f"Error translating: {str(e)}"
}), 500