Skip to content

Commit

Permalink
feat: add cohere_translate_instructions configuration for translation…
Browse files Browse the repository at this point in the history
… customization
  • Loading branch information
knajjars committed Dec 17, 2024
1 parent e0c41b5 commit e4e1df6
Show file tree
Hide file tree
Showing 20 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "Pfad für benutzerdefinierten Namespace. Dokumentation für weitere details prüfen.",
"config.preferred_delimiter": "Bevorzugtes Trennzeichen für zusammengesetzte Schlüsse. Standardwert ist \"-\"",
"config.prompt_translating_source": "Aufforderung zur Auswahl der Quellsprache bei jeder Verwendung. Wenn false, die Quellsprache wird aus der Konfiguration gelesen.",
Expand Down
1 change: 1 addition & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"config.cohere_api_key": "Cohere API key",
"config.cohere_api_model": "Cohere Chat model",
"config.cohere_api_root": "Cohere API root URL",
"config.cohere_translate_instructions": "Cohere custom instructions for translating text",
"config.path_matcher": "Match path for custom locale/namespace. Check out the docs for more details.",
"config.preferred_delimiter": "Preferred delimiter of composed keypath, default to \"-\"(dash)",
"config.prompt_translating_source": "Prompt to select source locale on translating every time. If set false, the source language in the config will be used.",
Expand Down
1 change: 1 addition & 0 deletions locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "Igual el path para configuraciones regionales/espacios de nombres personalizados. Comprueba la documentación para más detalles.",
"config.preferred_delimiter": "Delimitador preferido para las rutas de acceso compuestas, por defecto \"-\"(guión)",
"config.prompt_translating_source": "Solicitud para seleccionar la configuración regional de origen al traducir. Si se establece en false, se utilizará el idioma de origen en la configuración.",
Expand Down
1 change: 1 addition & 0 deletions locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "Faire correspondre le chemin pour les locales/namespaces personnalisés. Consultez la documentation pour plus de détails.",
"config.preferred_delimiter": "Délimiteur recommandé pour les noms de chemins à trous, par défaut « - » (tiret)",
"config.prompt_translating_source": "Invite à sélectionner la langue source à chaque fois. Si défini sur False, le langage du système sera automatiquement utilisé.",
Expand Down
1 change: 1 addition & 0 deletions locales/hu.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "A locale / névterekhez tartozó egyezési út. További információért lásd a dokumentációt.",
"config.preferred_delimiter": "Az összetett kulcsú útvonalak preferált határolója, alapértelmezetten az - (kötőjel)",
"config.prompt_translating_source": "Mindig kérdezze meg forrás nyelvre fordításkor. Ha hamisra van állítva, a konfigurációban megadott forrásnyelv lesz használva.",
Expand Down
1 change: 1 addition & 0 deletions locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "",
"config.preferred_delimiter": "キーパスを構成するデリミタ。デフォルトは\"-\"(ダッシュ)",
"config.prompt_translating_source": "",
Expand Down
1 change: 1 addition & 0 deletions locales/ko.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "사용자 정의 로케일/네임스페이스에 대한 일치 경로. \n자세한 내용은 문서를 확인하세요.",
"config.preferred_delimiter": "구성된 키 경로의 기본 구분 기호, 기본값은 \"-\"(대시)",
"config.prompt_translating_source": "번역할 때마다 소스 로케일을 선택하라는 메시지가 표시됩니다. \nfalse로 설정하면 구성의 소스 언어가 사용됩니다.",
Expand Down
1 change: 1 addition & 0 deletions locales/nb-NO.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "",
"config.preferred_delimiter": "Foretrukket skilletegn for komponert nøkkelbane, \"-\"(bindestrek) som standard",
"config.prompt_translating_source": "",
Expand Down
1 change: 1 addition & 0 deletions locales/nl-NL.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "Pad voor onderscheiding van sleutels op basis van folderstructuur/bestandsnaam. Zie documentatie voor meer uitleg.",
"config.preferred_delimiter": "Gewenst scheidingsteken voor sleutel pad, standaard waarde \"-\"(dash)",
"config.prompt_translating_source": "Altijd vragen om de bron locale te selectering bij vertalingen. Indien false, dan zal de bron taal in de configuratie gebruikt worden.",
Expand Down
1 change: 1 addition & 0 deletions locales/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "",
"config.preferred_delimiter": "Delimitador para o caminho da chave, o padrão é \"-\"(hífen)",
"config.prompt_translating_source": "Sempre perguntar idioma de origem durante tradução. Se o valor for \"false\", será usado o idioma de origem das configurações.",
Expand Down
1 change: 1 addition & 0 deletions locales/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "Сопоставить путь для пользовательского перевода/пространства имен. Подробности смотрите в документации.",
"config.preferred_delimiter": "Желаемый разделитель пути составного ключа, значение по умолчанию \"-\"(тире)",
"config.prompt_translating_source": "Предлагать выбрать исходный перевод при переводе каждый раз. Если установлено false, будет использоваться исходный перевод из конфигурации.",
Expand Down
1 change: 1 addition & 0 deletions locales/sv-SE.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "Matcha sökväg för egna locale-filer/namespace. Se dokumentation för mer detaljer.",
"config.preferred_delimiter": "Föredragen avskiljare av sammansatta nyckelsökvägar, som standard satt till \"-\" (bindestreck)",
"config.prompt_translating_source": "Fråga om att välja locale-filskälla varje gång vid översättning. Om satt till `false` kommer locale-filskällan i konfigurationen användas.",
Expand Down
1 change: 1 addition & 0 deletions locales/th.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "จับคู่เส้นทางสำหรับภาษา/namespace ที่กำหนดเอง ตรวจสอบเอกสารสำหรับรายละเอียดเพิ่มเติม",
"config.preferred_delimiter": "ตัวคั่นที่ต้องการของเส้นทางที่ประกอบขึ้น ค่าเริ่มต้นคือ \"-\"(เส้นประ)",
"config.prompt_translating_source": "พร้อมท์ให้เลือกภาษาต้นทางในการแปลทุกครั้ง หากตั้งค่าเป็นเท็จ ภาษาต้นทางในการกำหนดค่าจะถูกใช้",
Expand Down
1 change: 1 addition & 0 deletions locales/tr.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "Dosya yolunu özelleştirilmiş bir dil/namespace'e göre eşleştirin. Daha fazla ayrıntı için dokümantasyona göz atın.",
"config.preferred_delimiter": "Bileşik anahtar yolları için ayıran karakter, varsayılan \"-\" (tire)",
"config.prompt_translating_source": "Her seferinde çeviride kaynak dili seçmesini isteyin. Eğer false olarak ayarlıysa, yapılandırmadaki kaynak dil kullanılacaktır.",
Expand Down
1 change: 1 addition & 0 deletions locales/uk-UA.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "Шлях для розрізнення ключів на основі структури папок/імені файлу. Подробиці дивіться в документації.",
"config.preferred_delimiter": "Бажаний роздільник шляху складеного ключа, значення за замовчуванням \"-\"(тире)",
"config.prompt_translating_source": "Пропонувати обирати мовний стандарт кожен раз при перекладі. Якщо встановлено false, буде використовуватися мова з конфігурації.",
Expand Down
1 change: 1 addition & 0 deletions locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "匹配自定义区域设置/命名空间的路径。查看文档了解更多详细信息。",
"config.preferred_delimiter": "组合键路径的默认分隔符,默认为 \"-\"(短划线)",
"config.prompt_translating_source": "每次翻译时都会提示选择源语言环境。\n如果设置为false,将使用配置中的源语言。",
Expand Down
1 change: 1 addition & 0 deletions locales/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"config.cohere_api_key": "",
"config.cohere_api_model": "",
"config.cohere_api_root": "",
"config.cohere_translate_instructions": "",
"config.path_matcher": "匹配自定義區域設置/命名空間的路徑。\n查看文件了解更多詳細信息。",
"config.preferred_delimiter": "組合鍵路徑的首選分隔符,默認為 \"-\"(短劃線)",
"config.prompt_translating_source": "每次翻譯時都會提示選擇源語言環境。\n如果設置為false,將使用配置中的源語言。",
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,12 @@
],
"description": "%config.cohere_api_model%"
},
"i18n-ally.translate.cohere.translateInstructions": {
"type": "string",
"default": "You are a professional translation engine. Please translate text without explanation.",
"description": "%config.cohere_translate_instructions%",
"editPresentation": "multilineText"
},
"i18n-ally.usage.scanningIgnore": {
"type": "array",
"items": {
Expand Down
4 changes: 4 additions & 0 deletions src/core/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,10 @@ export class Config {
return this.getConfig<string>('translate.cohere.apiModel') ?? 'command-r7b-12-2024'
}

static get cohereTranslateInstructions() {
return this.getConfig<string>('translate.cohere.translateInstructions') ?? 'You are a professional translation engine. Please translate text without explanation.'
}

static get telemetry(): boolean {
return workspace.getConfiguration().get('telemetry.enableTelemetry') as boolean
}
Expand Down
11 changes: 2 additions & 9 deletions src/translators/engines/cohere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,9 @@ import { Config } from '~/core'

export default class CohereTranslate extends TranslateEngine {
apiRoot = 'https://api.cohere.com'
systemPrompt = `You are a professional translation engine.
Please follow these rules strictly:
1. Only provide the translation of the text. Do not add any additional information.
2. Do not translate tags, e.g., <cta>TEXT_TO_TRANSLATE</cta>.
3. Do not translate variable names used for interpolation, e.g., {item}.
4. Do not translate JSON keys, only translate the values.
5. Maintain the original capitalization of the text.`

async translate(options: TranslateOptions) {
const systemPrompt = Config.cohereTranslateInstructions
const apiKey = Config.cohereApiKey
let apiRoot = this.apiRoot
if (Config.cohereApiRoot) apiRoot = Config.cohereApiRoot.replace(/\/$/, '')
Expand All @@ -26,7 +19,7 @@ Please follow these rules strictly:
messages: [
{
role: 'system',
content: this.systemPrompt,
content: systemPrompt,
},
{
role: 'user',
Expand Down

0 comments on commit e4e1df6

Please sign in to comment.