Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(toolkit): language tag check should be case insensitive #7164

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/tame-snails-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@logto/language-kit": patch
"@logto/translate": patch
---

make method `isLanguageTag` case-insensitive

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`.

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.
5 changes: 4 additions & 1 deletion packages/toolkit/language-kit/src/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { languages } from './const.js';
import type { LanguageTag } from './type.js';

export const isLanguageTag = (value: unknown): value is LanguageTag =>
typeof value === 'string' && value in languages;
typeof value === 'string' &&
Object.keys(languages)
.map((key) => key.toLowerCase())
.includes(value.toLowerCase());

export const languageTagGuard: z.ZodType<LanguageTag> = z
.any()
Expand Down
6 changes: 2 additions & 4 deletions packages/translate/src/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { languages, type LanguageTag } from '@logto/language-kit';
import { type LanguageTag } from '@logto/language-kit';
import { conditionalString } from '@silverhand/essentials';

type GetTranslationPromptProperties = {
Expand All @@ -22,9 +22,7 @@ export const getTranslationPromptMessages = ({
}: GetTranslationPromptProperties) => [
{
role: 'assistant',
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 "${
languages[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(
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(
extraPrompt
)}

Expand Down
Loading