Fix issue #2528: Restore language option in crew configuration

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-04-07 10:29:21 +00:00
parent 918c0589eb
commit ebd0803c0c
4 changed files with 70 additions and 4 deletions

View File

@@ -191,6 +191,14 @@ class Crew(BaseModel):
default=None,
description="Path to the prompt json file to be used for the crew.",
)
language: Optional[str] = Field(
default="en",
description="Language used for the crew, defaults to English.",
)
language_file: Optional[str] = Field(
default=None,
description="Path to the language file to be used for the crew.",
)
output_log_file: Optional[Union[bool, str]] = Field(
default=None,
description="Path to the log file to be saved",
@@ -609,7 +617,7 @@ class Crew(BaseModel):
self._interpolate_inputs(inputs)
self._set_tasks_callbacks()
i18n = I18N(prompt_file=self.prompt_file)
i18n = I18N(prompt_file=self.prompt_file, language=self.language)
for agent in self.agents:
agent.i18n = i18n
@@ -751,7 +759,7 @@ class Crew(BaseModel):
return self._execute_tasks(self.tasks)
def _create_manager_agent(self):
i18n = I18N(prompt_file=self.prompt_file)
i18n = I18N(prompt_file=self.prompt_file, language=self.language)
if self.manager_agent is not None:
self.manager_agent.allow_delegation = True
manager = self.manager_agent

View File

@@ -13,6 +13,10 @@ class I18N(BaseModel):
default=None,
description="Path to the prompt_file file to load",
)
language: Optional[str] = Field(
default="en",
description="Language to use for translations",
)
@model_validator(mode="after")
def load_prompts(self) -> "I18N":
@@ -23,8 +27,12 @@ class I18N(BaseModel):
self._prompts = json.load(f)
else:
dir_path = os.path.dirname(os.path.realpath(__file__))
prompts_path = os.path.join(dir_path, "../translations/en.json")
lang = self.language or "en"
prompts_path = os.path.join(dir_path, f"../translations/{lang}.json")
if not os.path.exists(prompts_path):
prompts_path = os.path.join(dir_path, "../translations/en.json")
with open(prompts_path, "r", encoding="utf-8") as f:
self._prompts = json.load(f)
except FileNotFoundError:

View File

@@ -0,0 +1,40 @@
import pytest
from unittest.mock import patch
from crewai import Crew, Agent, Process, Task
from crewai.utilities.i18n import I18N
def test_crew_with_language():
i18n = I18N(language="en")
agent = Agent(
role="Test Agent",
goal="Test Goal",
backstory="Test Backstory",
verbose=True
)
task = Task(
description="Test Task",
expected_output="Test Output",
agent=agent
)
with patch('crewai.crew.I18N') as mock_i18n:
mock_i18n.return_value = i18n
crew = Crew(
agents=[agent],
tasks=[task],
process=Process.sequential,
verbose=True,
language="fr" # Use French as an example
)
with patch.object(crew, '_run_sequential_process'):
with patch.object(crew, '_set_tasks_callbacks'):
with patch('crewai.agent.Agent.create_agent_executor'):
crew.kickoff()
mock_i18n.assert_called_with(prompt_file=None, language="fr")

View File

@@ -42,3 +42,13 @@ def test_prompt_file():
i18n.load_prompts()
assert isinstance(i18n.retrieve("slices", "role_playing"), str)
assert i18n.retrieve("slices", "role_playing") == "Lorem ipsum dolor sit amet"
def test_language_parameter():
i18n = I18N(language="en")
i18n.load_prompts()
assert isinstance(i18n.slice("role_playing"), str)
i18n = I18N(language="nonexistent")
i18n.load_prompts()
assert isinstance(i18n.slice("role_playing"), str)