Personalized Translation Prompts
Relevant source files
The following files were used as context for generating this wiki page:
- src/entrypoints/options/pages/translation/personalized-prompt/index.tsx
- src/utils/prompts/translate.ts
- src/entrypoints/options/pages/translation/personalized-prompt/utils/prompt-file.ts
- src/utils/constants/prompt.ts
- src/utils/config/tests/example/v022.ts
- src/locales/en.yml
- src/entrypoints/options/pages/translation/index.tsx
The Personalized Translation Prompts feature allows users to customize the instructions sent to Large Language Models (LLMs) for translation. This provides fine-grained control over the tone, style, and format of the translated text, enabling a more tailored and effective language learning experience. Users can create, manage, and switch between different prompt templates directly from the extension's options page.
This functionality is a key part of the AI-powered translation services, allowing users to move beyond generic translations and instruct the AI to act as a specific type of translator (e.g., a professional, a casual speaker, a technical expert). The system supports a default prompt and an unlimited number of user-defined custom prompts.
Sources: src/locales/en.yml:130-131, src/utils/prompts/translate.ts
Configuration Structure
The configuration for personalized prompts is stored within the translate section of the main application configuration object. The core of this is the promptsConfig object.
Sources: src/utils/config/tests/example/v022.ts:71-95
Data Schema
The relationship between the translation configuration and the prompt patterns is illustrated below.
This diagram shows that the translate configuration contains a promptsConfig object, which in turn holds the ID of the currently selected prompt and an array of promptPattern objects.
Sources: src/utils/config/tests/example/v022.ts:71-95
promptsConfig Object
| Field | Type | Description - prompt (or promptId) | string or null | The id of the currently active prompt pattern. If null or not set, the default prompt is used. - patterns | Array<object> | An array of prompt pattern objects. -
| promptId (new) / prompt (old) | string | null | The id of the currently active prompt pattern. If null or set to 'default', the default prompt is used. |
| patterns | Array<object> | An array of prompt pattern objects. |
Sources: src/utils/config/tests/example/v022.ts:72-73, src/utils/prompts/translate.ts:16-17
Prompt Pattern Object
Each object in the patterns array defines a single personalized prompt.
| Field | Type | Description - id | string | A unique identifier for the prompt pattern. - name | string | A user-friendly name for the prompt (e.g., "Technical Translation"). - prompt | string | The main user prompt text. This typically contains the {{input}} token. - systemPrompt | string | (Optional) The system prompt text, used to give high-level instructions to the LLM. -
Sources: src/utils/config/tests/example/v022.ts:74-94, src/locales/en.yml:140-145
Example Configuration
{
"translate": {
"providerName": "OpenAI",
"promptsConfig": {
"prompt": "123e4567-e89b-12d3-a456-426614174000",
"patterns": [
{
"id": "default",
"name": "default",
"prompt": "You are a professional {{targetLang}} native translator...\n\nTranslate to {{targetLang}}:\n{{input}}"
},
{
"id": "123e4567-e89b-12d3-a456-426614174000",
"name": "Technical Translation",
"prompt": "Technical translation from Spanish to {{targetLang}}. Preserve technical terms and accuracy:\n{{input}}"
}
]
}
}
}
Sources: src/utils/config/tests/example/v022.ts:107-133
Prompt Generation Logic
The getTranslatePrompt function is responsible for constructing the final system and user prompts to be sent to the LLM.
This diagram shows the flow for selecting and processing a translation prompt.
Sources: src/utils/prompts/translate.ts:13-53
Key Steps:
- Retrieve Configuration: The function first loads the user's configuration from storage.
- Select Prompt: It checks if
promptIdis set incustomPromptsConfig.- If
promptIdexists, it searches thepatternsarray for a matching prompt. - If no
promptIdis set, or if the specified pattern is not found, it falls back to the default prompts (DEFAULT_TRANSLATE_SYSTEM_PROMPTandDEFAULT_TRANSLATE_PROMPT).
- If
- Handle Batch Translation: If the translation is a batch operation (
options.isBatchis true), it appends theDEFAULT_BATCH_TRANSLATE_PROMPTto the system prompt. This provides the LLM with rules for handling multiple, separated text segments. - Token Replacement: Finally, it replaces all placeholder tokens in both the system and user prompts with their actual values (e.g.,
{{targetLang}}becomesJapanese).
Sources: src/utils/prompts/translate.ts:15-52
Prompt Tokens
Tokens are placeholders in the prompt templates that are dynamically replaced with contextual information before being sent to the AI.
| Token | Description - {{targetLang}} | The target language for the translation (e.g., "Japanese"). - {{input}} | The source text content to be translated. - {{title}} | The title of the webpage, providing context to the AI. This requires "AI Smart Context" to be enabled. - {{summary}} | A summary of the webpage, providing broader context. This also requires "AI Smart-Context" to be enabled. -
Sources: src/utils/constants/prompt.ts:1-4, src/locales/en.yml:143-145, src/utils/prompts/translate.ts:46-51
Default Prompts
If no custom prompt is selected, the system uses a set of default prompts.
Default System Prompt:
You are a professional {{targetLang}} native translator who needs to fluently translate text into {{targetLang}}.
## Translation Rules
1. Output only the translated content, without explanations or additional content (such as "Here's the translation:" or "Translation as follows:")
2. The returned translation must maintain exactly the same number of paragraphs and format as the original text.
3. If the text contains HTML tags, consider where the tags should be placed in the translation while maintaining fluency.
4. For content that should not be translated (such as proper nouns, code, etc.), keep the original text.
## Document Metadata for Context Awareness
Title: {{title}}
Summary: {{summary}}
Sources: src/utils/constants/prompt.ts:11-23
Default User Prompt:
Translate to {{targetLang}}:
{{input}}
Sources: src/utils/constants/prompt.ts:25-28
User Interface
The "Personalized Prompts" feature is managed through the extension's options page, under the "Translation" section.
The UI is built using React and provides the following capabilities:
- List Prompts: Displays a list of all available prompts, including the default and any user-created ones. The currently active prompt is highlighted.
- Add Prompt: A button to open a dialog for creating a new prompt pattern.
- Edit Prompt: Each custom prompt in the list has an edit button to modify its name, system prompt, and user prompt.
- Delete Prompt: Custom prompts can be deleted.
- Import/Export: Users can import and export their prompt configurations as JSON files, making it easy to back up or share prompts.
The UI component for this feature is PersonalizedPrompts, which is rendered within the main TranslationPage.
Sources: src/entrypoints/options/pages/translation/personalized-prompt/index.tsx, src/entrypoints/options/pages/translation/index.tsx:18, src/locales/en.yml:130-149
The import/export logic is handled by utility functions that read and create JSON files from the prompt patterns.
Sources: src/entrypoints/options/pages/translation/personalized-prompt/utils/prompt-file.ts