getTranslationForKey
Retrieve translation text for a specific key in a Weblate project component and language.
Instructions
Get translation value for a specific key in a project
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| projectSlug | Yes | The slug of the project | |
| componentSlug | Yes | The slug of the component | |
| languageCode | Yes | The language code (e.g., en, es, fr) | |
| key | Yes | The translation key to look up |
Implementation Reference
- src/tools/translations.tool.ts:98-148 (handler)The handler function that implements the core logic of the getTranslationForKey tool. It calls the WeblateApiService to retrieve the translation and handles formatting and error responses.async getTranslationForKey({ projectSlug, componentSlug, languageCode, key, }: { projectSlug: string; componentSlug: string; languageCode: string; key: string; }) { try { const translation = await this.weblateApiService.getTranslationByKey( projectSlug, componentSlug, languageCode, key, ); if (!translation) { return { content: [ { type: 'text', text: `Translation not found for key "${key}" in ${projectSlug}/${componentSlug}/${languageCode}`, }, ], }; } return { content: [ { type: 'text', text: this.formatTranslationResult(translation), }, ], }; } catch (error) { this.logger.error(`Failed to get translation for key ${key}`, error); return { content: [ { type: 'text', text: `Error getting translation for key "${key}": ${error.message}`, }, ], isError: true, }; } }
- src/tools/translations.tool.ts:91-96 (schema)Zod schema defining the input parameters for the getTranslationForKey tool.parameters: z.object({ projectSlug: z.string().describe('The slug of the project'), componentSlug: z.string().describe('The slug of the component'), languageCode: z.string().describe('The language code (e.g., en, es, fr)'), key: z.string().describe('The translation key to look up'), }),
- src/tools/translations.tool.ts:88-97 (registration)The @Tool decorator that registers the getTranslationForKey tool with MCP, including name, description, and parameter schema.@Tool({ name: 'getTranslationForKey', description: 'Get translation value for a specific key in a project', parameters: z.object({ projectSlug: z.string().describe('The slug of the project'), componentSlug: z.string().describe('The slug of the component'), languageCode: z.string().describe('The language code (e.g., en, es, fr)'), key: z.string().describe('The translation key to look up'), }), })
- Helper method used by the handler to format the translation result for display.private formatTranslationResult(translation: Unit): string { const status = translation.approved ? 'β Approved' : translation.translated ? 'π Translated' : 'β Untranslated'; const sourceText = translation.source && Array.isArray(translation.source) ? translation.source.join('') : (translation.source || '(empty)'); const targetText = translation.target && Array.isArray(translation.target) ? translation.target.join('') : (translation.target || '(empty)'); return `**Key:** ${translation.context} **Source:** ${sourceText} **Target:** ${targetText} **Status:** ${status} **Context:** ${translation.context || '(none)'} **Note:** ${translation.note || '(none)'} **ID:** ${translation.id}`; }