mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-11 00:58:30 +00:00
feat: add global i18n configuration via CREWAI_I18N_FILE environment variable
- Add support for CREWAI_I18N_FILE environment variable to globally configure translation files - Maintain backward compatibility with existing prompt_file parameter - Priority order: explicit prompt_file > CREWAI_I18N_FILE env var > default en.json - Add Chinese (zh.json) and Spanish (es.json) translation files for testing - Add comprehensive tests covering environment variable functionality, priority handling, and error cases - Resolves issue #3264 by providing global interface to change i18n files Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import json
|
||||
import os
|
||||
import pytest
|
||||
|
||||
from crewai.utilities.i18n import I18N
|
||||
@@ -42,3 +44,89 @@ 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_global_i18n_file_environment_variable(monkeypatch):
|
||||
"""Test that CREWAI_I18N_FILE environment variable is respected"""
|
||||
test_translations = {
|
||||
"slices": {"role_playing": "Test role playing message"},
|
||||
"tools": {"ask_question": "Test ask question message"}
|
||||
}
|
||||
|
||||
test_file_path = os.path.join(os.path.dirname(__file__), "test_env_prompts.json")
|
||||
with open(test_file_path, "w", encoding="utf-8") as f:
|
||||
json.dump(test_translations, f)
|
||||
|
||||
try:
|
||||
monkeypatch.setenv("CREWAI_I18N_FILE", test_file_path)
|
||||
|
||||
i18n = I18N()
|
||||
i18n.load_prompts()
|
||||
|
||||
assert i18n.slice("role_playing") == "Test role playing message"
|
||||
assert i18n.tools("ask_question") == "Test ask question message"
|
||||
|
||||
finally:
|
||||
if os.path.exists(test_file_path):
|
||||
os.remove(test_file_path)
|
||||
|
||||
|
||||
def test_prompt_file_priority_over_environment_variable(monkeypatch):
|
||||
"""Test that explicit prompt_file takes priority over environment variable"""
|
||||
monkeypatch.setenv("CREWAI_I18N_FILE", "/nonexistent/path.json")
|
||||
|
||||
path = os.path.join(os.path.dirname(__file__), "prompts.json")
|
||||
i18n = I18N(prompt_file=path)
|
||||
i18n.load_prompts()
|
||||
|
||||
assert i18n.retrieve("slices", "role_playing") == "Lorem ipsum dolor sit amet"
|
||||
|
||||
|
||||
def test_environment_variable_file_not_found(monkeypatch):
|
||||
"""Test proper error handling when environment variable points to non-existent file"""
|
||||
monkeypatch.setenv("CREWAI_I18N_FILE", "/nonexistent/file.json")
|
||||
|
||||
i18n = I18N()
|
||||
with pytest.raises(Exception, match="Prompt file '/nonexistent/file.json' not found"):
|
||||
i18n.load_prompts()
|
||||
|
||||
|
||||
def test_fallback_to_default_when_no_environment_variable(monkeypatch):
|
||||
"""Test that it falls back to default en.json when no environment variable is set"""
|
||||
monkeypatch.delenv("CREWAI_I18N_FILE", raising=False)
|
||||
|
||||
i18n = I18N()
|
||||
i18n.load_prompts()
|
||||
|
||||
assert isinstance(i18n.slice("role_playing"), str)
|
||||
assert len(i18n.slice("role_playing")) > 0
|
||||
|
||||
|
||||
def test_chinese_translation_file():
|
||||
"""Test loading Chinese translation file"""
|
||||
import os
|
||||
|
||||
zh_path = os.path.join(os.path.dirname(__file__), "../../src/crewai/translations/zh.json")
|
||||
zh_path = os.path.abspath(zh_path)
|
||||
|
||||
i18n = I18N(prompt_file=zh_path)
|
||||
i18n.load_prompts()
|
||||
|
||||
assert i18n.retrieve("hierarchical_manager_agent", "role") == "团队经理"
|
||||
assert i18n.slice("observation") == "\n观察:"
|
||||
assert i18n.errors("tool_usage_error") == "我遇到了错误: {error}"
|
||||
|
||||
|
||||
def test_spanish_translation_file():
|
||||
"""Test loading Spanish translation file"""
|
||||
import os
|
||||
|
||||
es_path = os.path.join(os.path.dirname(__file__), "../../src/crewai/translations/es.json")
|
||||
es_path = os.path.abspath(es_path)
|
||||
|
||||
i18n = I18N(prompt_file=es_path)
|
||||
i18n.load_prompts()
|
||||
|
||||
assert i18n.retrieve("hierarchical_manager_agent", "role") == "Gerente del Equipo"
|
||||
assert i18n.slice("observation") == "\nObservación:"
|
||||
assert i18n.errors("tool_usage_error") == "Encontré un error: {error}"
|
||||
|
||||
Reference in New Issue
Block a user