duplicate_translation
Copy translations from one key or namespace to another for creating similar keys or reorganizing translation structures.
Instructions
Duplicate translations from one key/namespace to another key/namespace.
This function copies all translations for a specific key (and optionally namespace) to another key (and optionally namespace). Useful for duplicating translations when creating similar keys or reorganizing translations.
Args: from_dict: Source dictionary with fields: - key (required): Source translation key - namespace (optional): Source namespace to_dict: Destination dictionary with fields: - key (required): Destination translation key - namespace (optional): Destination namespace
Returns: String message indicating success or failure
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| from_dict | Yes | ||
| to_dict | Yes |
Implementation Reference
- main.py:207-313 (handler)The main handler function for the 'duplicate_translation' tool. It is decorated with @mcp.tool(), which registers it as an MCP tool. The function copies translations from a source key/namespace to a destination key/namespace by fetching existing translations via API, creating the destination key if necessary, and bulk-updating the translations using the SimpleLocalize API.@mcp.tool() async def duplicate_translation(from_dict: dict, to_dict: dict) -> str: """Duplicate translations from one key/namespace to another key/namespace. This function copies all translations for a specific key (and optionally namespace) to another key (and optionally namespace). Useful for duplicating translations when creating similar keys or reorganizing translations. Args: from_dict: Source dictionary with fields: - key (required): Source translation key - namespace (optional): Source namespace to_dict: Destination dictionary with fields: - key (required): Destination translation key - namespace (optional): Destination namespace Returns: String message indicating success or failure """ # Validate input if not from_dict.get("key"): raise ValueError("Source dictionary must have a 'key' field") if not to_dict.get("key"): raise ValueError("Destination dictionary must have a 'key' field") source_key = from_dict["key"] source_namespace = from_dict.get("namespace", "") dest_key = to_dict["key"] dest_namespace = to_dict.get("namespace", "") try: # Step 1: Get all translations from the source result = await make_simplelocalize_request( "GET", "/api/v2/translations" ) data = result.get("data", []) # Filter for translations matching the source key/namespace source_translations = [] for item in data: key = item.get("key", "") namespace = item.get("namespace", "") language = item.get("language", "") text = item.get("text", "") # Check if this matches our source key/namespace if key == source_key and namespace == source_namespace and text: source_translations.append({ "language": language, "text": text }) if not source_translations: return f"No translations found for key '{source_key}'" + (f" in namespace '{source_namespace}'" if source_namespace else "") # Step 2: Create the destination key if it doesn't exist create_key_payload = { "key": dest_key } if dest_namespace: create_key_payload["namespace"] = dest_namespace # Try to create the key (will fail silently if it already exists) try: await make_simplelocalize_request( "POST", "/api/v1/translation-keys/bulk", {"translationKeys": [create_key_payload]} ) except SimpleLocalizeError: # Key might already exist, continue pass # Step 3: Copy all translations to the destination translations_to_update = [] for trans in source_translations: update_payload = { "key": dest_key, "language": trans["language"], "text": trans["text"] } if dest_namespace: update_payload["namespace"] = dest_namespace translations_to_update.append(update_payload) # Update translations in bulk update_result = await make_simplelocalize_request( "PATCH", "/api/v2/translations/bulk", {"translations": translations_to_update} ) if "failures" in update_result.get("data", {}): failures = update_result["data"]["failures"] if failures: return f"Some translations failed to duplicate: {failures}" source_desc = f"'{source_key}'" + (f" (namespace: '{source_namespace}')" if source_namespace else "") dest_desc = f"'{dest_key}'" + (f" (namespace: '{dest_namespace}')" if dest_namespace else "") return f"Successfully duplicated {len(translations_to_update)} translation(s) from {source_desc} to {dest_desc}" except SimpleLocalizeError as e: return str(e)