Skip to content

Commit 74da580

Browse files
authored
Translate menus and UI messages to supported languages (#41)
* Translate the menu and messages of supported languages
1 parent 98cb993 commit 74da580

File tree

7 files changed

+139
-99
lines changed

7 files changed

+139
-99
lines changed

src/architect.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// TODO
2-
// Превод на меню и попъп съобщения на поддържаните езици + улесни превеждането - с ИИ.
32
// Да не премигва при избор само на ред или дума (върни частично проверката за съвпадение с последния рекуест?)
43
// Идеи
54
// - Използване на агенти (?)
@@ -19,7 +18,7 @@ export class Architect {
1918
let configurationChangeDisp = vscode.workspace.onDidChangeConfiguration((event) => {
2019
const config = vscode.workspace.getConfiguration("llama-vscode");
2120
this.app.extConfig.updateOnEvent(event, config);
22-
vscode.window.showInformationMessage(`llama-vscode extension is updated.`);
21+
vscode.window.showInformationMessage(this.app.extConfig.getUiText(`llama-vscode extension is updated.`)??"");
2322
});
2423
context.subscriptions.push(configurationChangeDisp);
2524
}

src/chat-with-ai.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class ChatWithAi {
1515
showChatWithAi = (withContext: boolean, context: vscode.ExtensionContext) => {
1616
const editor = vscode.window.activeTextEditor;
1717
let webviewIdentifier = 'htmlChatWithAiViewer'
18-
let panelTitle = 'Chat With AI'
18+
let panelTitle = this.app.extConfig.getUiText("Chat with AI")??""
1919
let aiPanel = this.askAiPanel
2020
let extraCont = "";
2121
if (withContext){
@@ -26,7 +26,7 @@ export class ChatWithAi {
2626
let chunksToSendHash = this.app.extraContext.chunksHash.filter((item) => !this.sentContextChunks.includes(item));
2727
if (chunksToSend.length > 0) extraCont = "Here are pieces of code from different files of the project: \n" + chunksToSend.reduce((accumulator, currentValue) => accumulator + "\nFile Name: " + currentValue.filename + "\nText:\n" + currentValue.text + "\n\n" , "");
2828
this.sentContextChunks.push(...chunksToSendHash)
29-
panelTitle = "Chat With AI With Project Context"
29+
panelTitle = this.app.extConfig.getUiText("Chat with AI with project context")??""
3030
}
3131
let selectedText = ""
3232
if (editor) {

src/completion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class Completion {
115115
return [this.getCompletion(completion, position)];
116116
} catch (err) {
117117
console.error("Error fetching llama completion:", err);
118-
vscode.window.showInformationMessage(`Error getting response. Please check if llama.cpp server is running. `);
118+
vscode.window.showInformationMessage(this.app.extConfig.getUiText(`Error getting response. Please check if llama.cpp server is running.`)??"");
119119
let errorMessage = "Error fetching completion"
120120
if (err instanceof Error) {
121121
vscode.window.showInformationMessage(err.message);

src/configuration.ts

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as vscode from "vscode";
22
import OpenAI from "openai";
33
import https from "https";
44
import fs from "fs";
5+
import {translations} from "./translations"
56

67
export class Configuration {
78
// extension configs
@@ -49,52 +50,34 @@ export class Configuration {
4950

5051
config: vscode.WorkspaceConfiguration;
5152

52-
private languageBg = new Map<string, string>([
53-
["no suggestion", "нямам предложение"],
54-
["thinking...", "мисля..."],
55-
]);
56-
private languageEn = new Map<string, string>([
57-
["no suggestion", "no suggestion"],
58-
["thinking...", "thinking..."],
59-
]);
60-
private languageDe = new Map<string, string>([
61-
["no suggestion", "kein Vorschlag"],
62-
["thinking...", "Ich denke..."],
63-
]);
64-
private languageRu = new Map<string, string>([
65-
["no suggestion", "нет предложения"],
66-
["thinking...", "думаю..."],
67-
]);
68-
private languageEs = new Map<string, string>([
69-
["no suggestion", "ninguna propuesta"],
70-
["thinking...", "pensando..."],
71-
]);
72-
private languageCn = new Map<string, string>([
73-
["no suggestion", "无建议"],
74-
["thinking...", "思考..."],
75-
]);
76-
private languageFr = new Map<string, string>([
77-
["no suggestion", "pas de suggestion"],
78-
["thinking...", "pense..."],
79-
]);
80-
81-
languages = new Map<string, Map<string, string>>([
82-
["bg", this.languageBg],
83-
["en", this.languageEn],
84-
["de", this.languageDe],
85-
["ru", this.languageRu],
86-
["es", this.languageEs],
87-
["cn", this.languageCn],
88-
["fr", this.languageFr],
53+
private uiLanguages = new Map<string, Map<string, string>>([])
54+
private langIndexes = new Map<number, string>([
55+
[0, "en"],
56+
[1, "bg"],
57+
[2, "de"],
58+
[3, "ru"],
59+
[4, "es"],
60+
[5, "cn"],
61+
[6, "fr"],
8962
]);
9063

9164
constructor() {
9265
this.config = vscode.workspace.getConfiguration("llama-vscode");
66+
this.initUiLanguages()
9367
this.updateConfigs(this.config);
9468
this.setLlamaRequestConfig();
9569
this.setOpenAiClient();
9670
}
9771

72+
private initUiLanguages(){
73+
let totalLanguages = 0;
74+
if (translations.length > 0) totalLanguages = translations[0].length
75+
for (let langInd = 0; langInd < totalLanguages; langInd++){
76+
let lang = new Map(translations.map(transl => [transl[0], transl[langInd]]));
77+
this.uiLanguages.set(this.langIndexes.get(langInd) ?? "", lang);
78+
}
79+
}
80+
9881
private updateConfigs = (config: vscode.WorkspaceConfiguration) => {
9982
// TODO Handle the case of wrong types for the configuration values
10083
this.endpoint = this.trimTrailingSlash(String(config.get<string>("endpoint")));
@@ -126,8 +109,8 @@ export class Configuration {
126109
};
127110

128111
getUiText = (uiText: string): string | undefined => {
129-
let langTexts = this.languages.get(this.language);
130-
if (langTexts == undefined) langTexts = this.languages.get("en");
112+
let langTexts = this.uiLanguages.get(this.language);
113+
if (langTexts == undefined) langTexts = this.uiLanguages.get("en");
131114
return langTexts?.get(uiText);
132115
};
133116

src/llama-server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export class LlamaServer {
152152

153153
shellFimCmd = (launchCmd: string): void => {
154154
if (!launchCmd) {
155-
vscode.window.showInformationMessage("There is no command to execute.");
155+
vscode.window.showInformationMessage(this.app.extConfig.getUiText("There is no command to execute.")??"");
156156
return;
157157
}
158158
try {
@@ -163,14 +163,14 @@ export class LlamaServer {
163163
this.vsCodeFimTerminal.sendText(launchCmd);
164164
} catch(err){
165165
if (err instanceof Error) {
166-
vscode.window.showInformationMessage("Error executind command " + launchCmd +" : " + err.message);
166+
vscode.window.showInformationMessage(this.app.extConfig.getUiText("Error executind command") + " " + launchCmd +" : " + err.message);
167167
}
168168
}
169169
}
170170

171171
shellChatCmd = (launchCmd: string): void => {
172172
if (!launchCmd) {
173-
vscode.window.showInformationMessage("There is no command to execute.");
173+
vscode.window.showInformationMessage(this.app.extConfig.getUiText("There is no command to execute.")??"");
174174
return;
175175
}
176176
try {
@@ -181,7 +181,7 @@ export class LlamaServer {
181181
this.vsCodeChatTerminal.sendText(launchCmd);
182182
} catch(err){
183183
if (err instanceof Error) {
184-
vscode.window.showInformationMessage("Error executind command " + launchCmd +" : " + err.message);
184+
vscode.window.showInformationMessage(this.app.extConfig.getUiText("Error executind command") + " " + launchCmd +" : " + err.message);
185185
}
186186
}
187187
}

0 commit comments

Comments
 (0)