Skip to content

Commit 65740b3

Browse files
authored
Put the full language name in the system prompt (#1813)
1 parent 3b8e695 commit 65740b3

File tree

5 files changed

+44
-21
lines changed

5 files changed

+44
-21
lines changed

src/core/prompts/__tests__/sections.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe("addCustomInstructions", () => {
1313
)
1414

1515
expect(result).toContain("Language Preference:")
16-
expect(result).toContain('You should always speak and think in the "fr" language')
16+
expect(result).toContain('You should always speak and think in the "Français" (fr) language')
1717
})
1818

1919
test("works without vscode language", async () => {

src/core/prompts/sections/__tests__/custom-instructions.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ describe("addCustomInstructions", () => {
117117
)
118118

119119
expect(result).toContain("Language Preference:")
120-
expect(result).toContain("es")
120+
expect(result).toContain("Español") // Check for language name
121+
expect(result).toContain("(es)") // Check for language code in parentheses
121122
expect(result).toContain("Global Instructions:\nglobal instructions")
122123
expect(result).toContain("Mode-specific Instructions:\nmode instructions")
123124
expect(result).toContain("Rules from .clinerules-test-mode:\nmode specific rules")
@@ -145,6 +146,22 @@ describe("addCustomInstructions", () => {
145146
expect(result).not.toContain("Rules from .clinerules-test-mode")
146147
})
147148

149+
it("should handle unknown language codes properly", async () => {
150+
mockedFs.readFile.mockRejectedValue({ code: "ENOENT" })
151+
152+
const result = await addCustomInstructions(
153+
"mode instructions",
154+
"global instructions",
155+
"/fake/path",
156+
"test-mode",
157+
{ language: "xyz" }, // Unknown language code
158+
)
159+
160+
expect(result).toContain("Language Preference:")
161+
expect(result).toContain('"xyz" (xyz) language') // For unknown codes, the code is used as the name too
162+
expect(result).toContain("Global Instructions:\nglobal instructions")
163+
})
164+
148165
it("should throw on unexpected errors", async () => {
149166
const error = new Error("Permission denied") as NodeJS.ErrnoException
150167
error.code = "EPERM"

src/core/prompts/sections/custom-instructions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from "fs/promises"
22
import path from "path"
33
import * as vscode from "vscode"
4+
import { LANGUAGES } from "../../../shared/language"
45

56
async function safeReadFile(filePath: string): Promise<string> {
67
try {
@@ -47,8 +48,9 @@ export async function addCustomInstructions(
4748

4849
// Add language preference if provided
4950
if (options.language) {
51+
const languageName = LANGUAGES[options.language] || options.language
5052
sections.push(
51-
`Language Preference:\nYou should always speak and think in the "${options.language}" language unless the user gives you instructions below to do otherwise.`,
53+
`Language Preference:\nYou should always speak and think in the "${languageName}" (${options.language}) language unless the user gives you instructions below to do otherwise.`,
5254
)
5355
}
5456

src/shared/language.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
/**
2+
* Language name mapping from ISO codes to full language names
3+
*/
4+
export const LANGUAGES: Record<string, string> = {
5+
ca: "Català",
6+
de: "Deutsch",
7+
en: "English",
8+
es: "Español",
9+
fr: "Français",
10+
hi: "हिन्दी",
11+
it: "Italiano",
12+
ja: "日本語",
13+
ko: "한국어",
14+
pl: "Polski",
15+
"pt-BR": "Português",
16+
tr: "Türkçe",
17+
vi: "Tiếng Việt",
18+
"zh-CN": "简体中文",
19+
"zh-TW": "繁體中文",
20+
}
21+
122
/**
223
* Formats a VSCode locale string to ensure the region code is uppercase.
324
* For example, transforms "en-us" to "en-US" or "fr-ca" to "fr-CA".

webview-ui/src/components/settings/LanguageSettings.tsx

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,12 @@ import { Globe } from "lucide-react"
44

55
import { cn } from "@/lib/utils"
66
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from "@/components/ui"
7+
import { LANGUAGES } from "../../../../src/shared/language"
78

89
import { SetCachedStateField } from "./types"
910
import { SectionHeader } from "./SectionHeader"
1011
import { Section } from "./Section"
1112

12-
const LANGUAGES: Record<string, string> = {
13-
ca: "Català",
14-
de: "Deutsch",
15-
en: "English",
16-
es: "Español",
17-
fr: "Français",
18-
hi: "हिन्दी",
19-
it: "Italiano",
20-
ja: "日本語",
21-
ko: "한국어",
22-
pl: "Polski",
23-
"pt-BR": "Português",
24-
tr: "Türkçe",
25-
vi: "Tiếng Việt",
26-
"zh-CN": "简体中文",
27-
"zh-TW": "繁體中文",
28-
}
29-
3013
type LanguageSettingsProps = HTMLAttributes<HTMLDivElement> & {
3114
language: string
3215
setCachedStateField: SetCachedStateField<"language">

0 commit comments

Comments
 (0)