0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-03-31 22:51:25 -05:00

refactor(translate): improve prompts to better support i18n suffixes (#7167)

* refactor(translate): improve prompts to better support i18n suffixes

* chore: add changeset
This commit is contained in:
Charles Zhao 2025-03-25 11:32:54 +08:00 committed by GitHub
parent fd9b03ea08
commit 552a368482
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 40 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
"@logto/translate": patch
---
improve openai prompt to better support i18n plural form suffixes

View file

@ -22,9 +22,41 @@ 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 "${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
)}
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.
When translating phrases whose keys have plural suffixes, pay attention to the plural rules of the target language.
For example, the plural suffixes in English are "_one" and "_other". But other languages may have additional suffixes like "_two", "_few" and "_many".
These suffixed phrases always have a "{{count}}" variable. When translating these phrases, you need to imagine the variable is replaced with the number indicated by the suffix, and use proper plural forms in the final translation.
For example, you may meet these phrases in English:
\`\`\`ts
const password_requirement = {
length_one: 'requires a minimum of {{count}} character',
length_two: 'requires a minimum of {{count}} characters',
length_few: 'requires a minimum of {{count}} characters',
length_many: 'requires a minimum of {{count}} characters',
length_other: 'requires a minimum of {{count}} characters',
};
\`\`\`
And the proper translation in Russian would be:
\`\`\`ts
const password_requirement = {
length_one: 'Требуется минимум {{count}} символ',
length_two: 'Требуется минимум {{count}} символа',
length_few: 'Требуется минимум {{count}} символа',
length_many: 'Требуется минимум {{count}} символов',
length_other: 'Требуется минимум {{count}} символов',
};
\`\`\`
${conditionalString(extraPrompt)}
Take translating to zh-CN as an example, if the input is:
\`\`\`ts