Skip to content

Commit 5da01bc

Browse files
charIeszhaogao-sun
andauthored
fix(toolkit): make language tag check case-insensitive (#7164)
* fix(toolkit): language tag check should be case insensitive * chore: add changeset * fix(translate): fix target language variable in prompt Co-authored-by: Gao Sun <[email protected]>
1 parent 5180368 commit 5da01bc

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

.changeset/tame-snails-fold.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@logto/language-kit": patch
3+
"@logto/translate": patch
4+
---
5+
6+
make method `isLanguageTag` case-insensitive
7+
8+
The language tags should be case insensitive. In `phrases` and `phrases-experience` packages, the language tags are all in lowercase. However, in the language kit, the language tags are in mixed cases, such as `pt-BR` and `zh-CN`.
9+
10+
Therefore, some of the i18n phrases were not translated by the translate CLI tool. The fix is to update the language kit to ignore cases in `isLanguageTag` function, so that the previously mismatched language tags can be detected and translated.

packages/toolkit/language-kit/src/utility.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { languages } from './const.js';
44
import type { LanguageTag } from './type.js';
55

66
export const isLanguageTag = (value: unknown): value is LanguageTag =>
7-
typeof value === 'string' && value in languages;
7+
typeof value === 'string' &&
8+
Object.keys(languages)
9+
.map((key) => key.toLowerCase())
10+
.includes(value.toLowerCase());
811

912
export const languageTagGuard: z.ZodType<LanguageTag> = z
1013
.any()

packages/translate/src/prompts.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { languages, type LanguageTag } from '@logto/language-kit';
1+
import { type LanguageTag } from '@logto/language-kit';
22
import { conditionalString } from '@silverhand/essentials';
33

44
type GetTranslationPromptProperties = {
@@ -22,9 +22,7 @@ export const getTranslationPromptMessages = ({
2222
}: GetTranslationPromptProperties) => [
2323
{
2424
role: 'assistant',
25-
content: `You are a assistant translator and will receive a TypeScript object. Traverse and find object values with "${untranslatedMark}" annotation on the top, then translate these values to target locale "${
26-
languages[targetLanguage]
27-
}". Remove the "${untranslatedMark}" annotations from output. Escape the single quotes (if any) in translated results by prepending a backslash. Keep the interpolation double curly brackets and their inner values intact. Make sure there is a space between the CJK and non-CJK characters. Prefer using "你" instead of "您" in Chinese. Do not include sample code snippet below in the final output. ${conditionalString(
25+
content: `You are a assistant translator and will receive a TypeScript object. Traverse and find object values with "${untranslatedMark}" annotation on the top, then translate these values to target locale "${targetLanguage}". Remove the "${untranslatedMark}" annotations from output. Escape the single quotes (if any) in translated results by prepending a backslash. Keep the interpolation double curly brackets and their inner values intact. Make sure there is a space between the CJK and non-CJK characters. Prefer using "你" instead of "您" in Chinese. Do not include sample code snippet below in the final output. ${conditionalString(
2826
extraPrompt
2927
)}
3028

0 commit comments

Comments
 (0)