translations.pyโข12 kB
"""Translation management tools for POEditor MCP server."""
from typing import Any, Dict, List
from mcp.types import Tool, TextContent, INVALID_PARAMS
from mcp import McpError
from ..poeditor_client import POEditorClient, POEditorError
def get_translation_tools() -> List[Tool]:
"""Get all translation-related tools."""
return [
Tool(
name="list_translations",
description="List translations for a language",
inputSchema={
"type": "object",
"properties": {
"project_id": {
"type": "string",
"description": "Project ID"
},
"language_code": {
"type": "string",
"description": "Language code"
}
},
"required": ["project_id", "language_code"]
}
),
Tool(
name="add_translation",
description="Add or update a translation",
inputSchema={
"type": "object",
"properties": {
"project_id": {
"type": "string",
"description": "Project ID"
},
"language_code": {
"type": "string",
"description": "Language code"
},
"term_id": {
"type": "string",
"description": "Term ID"
},
"content": {
"type": "string",
"description": "Translation content"
},
"fuzzy": {
"type": "boolean",
"description": "Whether the translation is fuzzy"
}
},
"required": ["project_id", "language_code", "term_id", "content"]
}
),
Tool(
name="update_translation",
description="Update a translation",
inputSchema={
"type": "object",
"properties": {
"project_id": {
"type": "string",
"description": "Project ID"
},
"language_code": {
"type": "string",
"description": "Language code"
},
"term_id": {
"type": "string",
"description": "Term ID"
},
"content": {
"type": "string",
"description": "New translation content"
},
"fuzzy": {
"type": "boolean",
"description": "Whether the translation is fuzzy"
}
},
"required": ["project_id", "language_code", "term_id", "content"]
}
),
Tool(
name="delete_translation",
description="Delete a translation",
inputSchema={
"type": "object",
"properties": {
"project_id": {
"type": "string",
"description": "Project ID"
},
"language_code": {
"type": "string",
"description": "Language code"
},
"term_id": {
"type": "string",
"description": "Term ID"
}
},
"required": ["project_id", "language_code", "term_id"]
}
),
Tool(
name="export_translations",
description="Export translations",
inputSchema={
"type": "object",
"properties": {
"project_id": {
"type": "string",
"description": "Project ID"
},
"language_code": {
"type": "string",
"description": "Language code"
},
"file_format": {
"type": "string",
"description": "Export format (json, csv, xml, etc.)"
}
},
"required": ["project_id", "language_code"]
}
)
]
async def handle_list_translations(
client: POEditorClient,
arguments: Dict[str, Any]
) -> List[TextContent]:
"""Handle list_translations tool call."""
project_id = arguments.get("project_id")
language_code = arguments.get("language_code")
if not project_id:
raise McpError(INVALID_PARAMS, "project_id is required")
if not language_code:
raise McpError(INVALID_PARAMS, "language_code is required")
try:
translations = await client.list_translations(project_id, language_code)
if not translations:
return [TextContent(
type="text",
text=f"No translations found for language {language_code} in project {project_id}."
)]
output = f"**Translations for {language_code} in Project {project_id}:**\n\n"
for translation in translations[:20]: # Limit to first 20 translations
term = translation.get('term', 'Unknown term')
content = translation.get('content', 'No translation')
fuzzy = translation.get('fuzzy', 0)
output += f"- **{term}:** {content}"
if fuzzy:
output += " (fuzzy)"
output += "\n"
if len(translations) > 20:
output += f"\n... and {len(translations) - 20} more translations"
return [TextContent(type="text", text=output)]
except POEditorError as e:
raise McpError(INVALID_PARAMS, f"POEditor API error: {e}")
except Exception as e:
raise McpError(INVALID_PARAMS, f"Unexpected error: {e}")
async def handle_add_translation(
client: POEditorClient,
arguments: Dict[str, Any]
) -> List[TextContent]:
"""Handle add_translation tool call."""
project_id = arguments.get("project_id")
language_code = arguments.get("language_code")
term_id = arguments.get("term_id")
content = arguments.get("content")
if not project_id:
raise McpError(INVALID_PARAMS, "project_id is required")
if not language_code:
raise McpError(INVALID_PARAMS, "language_code is required")
if not term_id:
raise McpError(INVALID_PARAMS, "term_id is required")
if not content:
raise McpError(INVALID_PARAMS, "content is required")
fuzzy = arguments.get("fuzzy", False)
try:
result = await client.add_translation(project_id, language_code, term_id, content, fuzzy)
output = f"**Translation Added Successfully!**\n\n"
output += f"- **Project ID:** {project_id}\n"
output += f"- **Language:** {language_code}\n"
output += f"- **Term ID:** {term_id}\n"
output += f"- **Content:** {content}\n"
output += f"- **Fuzzy:** {fuzzy}\n"
return [TextContent(type="text", text=output)]
except POEditorError as e:
raise McpError(INVALID_PARAMS, f"POEditor API error: {e}")
except Exception as e:
raise McpError(INVALID_PARAMS, f"Unexpected error: {e}")
async def handle_update_translation(
client: POEditorClient,
arguments: Dict[str, Any]
) -> List[TextContent]:
"""Handle update_translation tool call."""
project_id = arguments.get("project_id")
language_code = arguments.get("language_code")
term_id = arguments.get("term_id")
content = arguments.get("content")
if not project_id:
raise McpError(INVALID_PARAMS, "project_id is required")
if not language_code:
raise McpError(INVALID_PARAMS, "language_code is required")
if not term_id:
raise McpError(INVALID_PARAMS, "term_id is required")
if not content:
raise McpError(INVALID_PARAMS, "content is required")
fuzzy = arguments.get("fuzzy", False)
try:
result = await client.update_translation(project_id, language_code, term_id, content, fuzzy)
output = f"**Translation Updated Successfully!**\n\n"
output += f"- **Project ID:** {project_id}\n"
output += f"- **Language:** {language_code}\n"
output += f"- **Term ID:** {term_id}\n"
output += f"- **Content:** {content}\n"
output += f"- **Fuzzy:** {fuzzy}\n"
return [TextContent(type="text", text=output)]
except POEditorError as e:
raise McpError(INVALID_PARAMS, f"POEditor API error: {e}")
except Exception as e:
raise McpError(INVALID_PARAMS, f"Unexpected error: {e}")
async def handle_delete_translation(
client: POEditorClient,
arguments: Dict[str, Any]
) -> List[TextContent]:
"""Handle delete_translation tool call."""
project_id = arguments.get("project_id")
language_code = arguments.get("language_code")
term_id = arguments.get("term_id")
if not project_id:
raise McpError(INVALID_PARAMS, "project_id is required")
if not language_code:
raise McpError(INVALID_PARAMS, "language_code is required")
if not term_id:
raise McpError(INVALID_PARAMS, "term_id is required")
try:
success = await client.delete_translation(project_id, language_code, term_id)
if success:
output = f"**Translation deleted successfully!**\n"
output += f"- **Project ID:** {project_id}\n"
output += f"- **Language:** {language_code}\n"
output += f"- **Term ID:** {term_id}\n"
else:
output = f"**Failed to delete translation**"
return [TextContent(type="text", text=output)]
except POEditorError as e:
raise McpError(INVALID_PARAMS, f"POEditor API error: {e}")
except Exception as e:
raise McpError(INVALID_PARAMS, f"Unexpected error: {e}")
async def handle_export_translations(
client: POEditorClient,
arguments: Dict[str, Any]
) -> List[TextContent]:
"""Handle export_translations tool call."""
project_id = arguments.get("project_id")
language_code = arguments.get("language_code")
if not project_id:
raise McpError(INVALID_PARAMS, "project_id is required")
if not language_code:
raise McpError(INVALID_PARAMS, "language_code is required")
file_format = arguments.get("file_format", "json")
try:
export_url = await client.export_translations(project_id, language_code, file_format)
output = f"**Translations Export Ready!**\n\n"
output += f"- **Project ID:** {project_id}\n"
output += f"- **Language:** {language_code}\n"
output += f"- **Format:** {file_format}\n"
output += f"- **Download URL:** {export_url}\n"
return [TextContent(type="text", text=output)]
except POEditorError as e:
raise McpError(INVALID_PARAMS, f"POEditor API error: {e}")
except Exception as e:
raise McpError(INVALID_PARAMS, f"Unexpected error: {e}")
# Tool handler mapping
TRANSLATION_HANDLERS = {
"list_translations": handle_list_translations,
"add_translation": handle_add_translation,
"update_translation": handle_update_translation,
"delete_translation": handle_delete_translation,
"export_translations": handle_export_translations,
}