feat: Add multi-language support for CrewAI prompts

- Add language parameter to I18N class to support multiple languages
- Implement fallback to English for unsupported languages
- Add Spanish (es.json) translation file as example
- Update Agent and Crew classes to accept language parameter
- Add comprehensive tests for multi-language support

Fixes #3780

Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
Devin AI
2025-10-23 03:13:06 +00:00
parent 9728388ea7
commit 3bd929b9f3
6 changed files with 316 additions and 6 deletions

View File

@@ -0,0 +1,116 @@
import pytest
from crewai.agent import Agent
from crewai.crew import Crew
from crewai.task import Task
def test_agent_default_language():
agent = Agent(
role="Test Agent",
goal="Test goal",
backstory="Test backstory"
)
assert agent.i18n.language == "en"
def test_agent_with_spanish_language():
agent = Agent(
role="Agente de Prueba",
goal="Objetivo de prueba",
backstory="Historia de fondo de prueba",
language="es"
)
assert agent.i18n.language == "es"
assert "Eres {role}" in agent.i18n.slice("role_playing")
def test_agent_with_english_language_explicit():
agent = Agent(
role="Test Agent",
goal="Test goal",
backstory="Test backstory",
language="en"
)
assert agent.i18n.language == "en"
assert "You are {role}" in agent.i18n.slice("role_playing")
def test_crew_default_language():
agent = Agent(
role="Test Agent",
goal="Test goal",
backstory="Test backstory"
)
task = Task(
description="Test task",
expected_output="Test output",
agent=agent
)
crew = Crew(agents=[agent], tasks=[task])
assert crew.language is None
def test_crew_with_spanish_language():
agent = Agent(
role="Agente de Prueba",
goal="Objetivo de prueba",
backstory="Historia de fondo de prueba"
)
task = Task(
description="Tarea de prueba",
expected_output="Salida de prueba",
agent=agent
)
crew = Crew(agents=[agent], tasks=[task], language="es")
assert crew.language == "es"
def test_crew_language_propagates_to_agents():
agent1 = Agent(
role="Agent 1",
goal="Goal 1",
backstory="Backstory 1"
)
agent2 = Agent(
role="Agent 2",
goal="Goal 2",
backstory="Backstory 2"
)
task = Task(
description="Test task",
expected_output="Test output",
agent=agent1
)
crew = Crew(agents=[agent1, agent2], tasks=[task], language="es")
assert crew.language == "es"
def test_agent_language_overrides_default():
agent_en = Agent(
role="English Agent",
goal="English goal",
backstory="English backstory",
language="en"
)
agent_es = Agent(
role="Spanish Agent",
goal="Spanish goal",
backstory="Spanish backstory",
language="es"
)
assert agent_en.i18n.language == "en"
assert agent_es.i18n.language == "es"
assert "You are {role}" in agent_en.i18n.slice("role_playing")
assert "Eres {role}" in agent_es.i18n.slice("role_playing")
def test_agent_without_language_uses_default():
agent = Agent(
role="Test Agent",
goal="Test goal",
backstory="Test backstory"
)
assert agent.language is None
assert agent.i18n.language == "en"

View File

@@ -0,0 +1,98 @@
import pytest
from crewai.utilities.i18n import I18N
def test_default_language_is_english():
i18n = I18N()
assert i18n.language == "en"
assert isinstance(i18n.slice("role_playing"), str)
assert "You are {role}" in i18n.slice("role_playing")
def test_explicit_english_language():
i18n = I18N(language="en")
assert i18n.language == "en"
assert isinstance(i18n.slice("role_playing"), str)
assert "You are {role}" in i18n.slice("role_playing")
def test_spanish_language():
i18n = I18N(language="es")
assert i18n.language == "es"
assert isinstance(i18n.slice("role_playing"), str)
assert "Eres {role}" in i18n.slice("role_playing")
def test_spanish_hierarchical_manager():
i18n = I18N(language="es")
role = i18n.retrieve("hierarchical_manager_agent", "role")
goal = i18n.retrieve("hierarchical_manager_agent", "goal")
backstory = i18n.retrieve("hierarchical_manager_agent", "backstory")
assert role == "Gerente del Equipo"
assert "Gestionar el equipo" in goal
assert "gerente experimentado" in backstory
def test_spanish_errors():
i18n = I18N(language="es")
error = i18n.errors("tool_usage_error")
assert "Encontré un error" in error
def test_spanish_tools():
i18n = I18N(language="es")
delegate_work = i18n.tools("delegate_work")
assert "Delega una tarea específica" in delegate_work
def test_fallback_to_english_for_unsupported_language():
i18n = I18N(language="fr")
assert isinstance(i18n.slice("role_playing"), str)
assert "You are {role}" in i18n.slice("role_playing")
def test_custom_prompt_file_overrides_language():
import os
import tempfile
import json
custom_prompts = {
"slices": {
"role_playing": "Custom role playing prompt"
}
}
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
json.dump(custom_prompts, f)
temp_file = f.name
try:
i18n = I18N(prompt_file=temp_file, language="es")
assert i18n.slice("role_playing") == "Custom role playing prompt"
finally:
os.unlink(temp_file)
def test_retrieve_with_spanish():
i18n = I18N(language="es")
observation = i18n.retrieve("slices", "observation")
assert "Observación" in observation
def test_spanish_reasoning_prompts():
i18n = I18N(language="es")
initial_plan = i18n.retrieve("reasoning", "initial_plan")
assert "Eres {role}" in initial_plan
assert "profesional" in initial_plan
def test_language_parameter_validation():
i18n = I18N(language="en")
assert i18n.language == "en"
i18n_es = I18N(language="es")
assert i18n_es.language == "es"
i18n_pt = I18N(language="pt")
assert i18n_pt.language == "pt"