forked from crewAIInc/crewAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for translations (crewAIInc#120)
Add translations support
- Loading branch information
1 parent
ea7759b
commit 8e7772c
Showing
12 changed files
with
148 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import json | ||
import os | ||
from typing import Dict, Optional | ||
|
||
from pydantic import BaseModel, Field, PrivateAttr, ValidationError, model_validator | ||
|
||
|
||
class I18N(BaseModel): | ||
_translations: Optional[Dict[str, str]] = PrivateAttr() | ||
language: Optional[str] = Field( | ||
default="en", | ||
description="Language used to load translations", | ||
) | ||
|
||
@model_validator(mode="after") | ||
def load_translation(self) -> "I18N": | ||
"""Load translations from a JSON file based on the specified language.""" | ||
try: | ||
dir_path = os.path.dirname(os.path.realpath(__file__)) | ||
prompts_path = os.path.join(dir_path, f"translations/{self.language}.json") | ||
|
||
with open(prompts_path, "r") as f: | ||
self._translations = json.load(f) | ||
except FileNotFoundError: | ||
raise ValidationError( | ||
f"Trasnlation file for language '{self.language}' not found." | ||
) | ||
except json.JSONDecodeError: | ||
raise ValidationError(f"Error decoding JSON from the prompts file.") | ||
return self | ||
|
||
def slice(self, slice: str) -> str: | ||
return self.retrieve("slices", slice) | ||
|
||
def errors(self, error: str) -> str: | ||
return self.retrieve("errors", error) | ||
|
||
def tools(self, error: str) -> str: | ||
return self.retrieve("tools", error) | ||
|
||
def retrieve(self, kind, key): | ||
try: | ||
return self._translations[kind].get(key) | ||
except: | ||
raise ValidationError(f"Translation for '{kind}':'{key}' not found.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.