NEXU AI for Loco Translate — AI-Ready Feature Specifications (Verified from code)

1) Core Purpose
- Adds AI-powered translation features directly into the Loco Translate editor workflow in WordPress admin.
- Translates PO string entries using external AI providers, then writes translated targets back into the PO file and (when possible) compiles the corresponding MO file.
- Provides both batch translation (many strings) and single-string translation inside the editor.

2) Hard Requirements / Dependencies
- Requires PHP 7.4+.
- Requires the “Loco Translate” plugin to be installed and active; otherwise the plugin stops initialization and shows an admin notice.

3) Admin UI (Settings Pages)
- Adds a management page under WordPress “Tools” for configuring the plugin.
- Uses a multi-tab settings UI:
  - AI Providers: enable providers, set API key, pick model, test connection.
  - Glossary: “Do Not Translate” list + “Fixed Translations” rules.
  - Prompt: system prompt + default custom prompt.
  - Turbo Mode: parallel processing controls for speed.
  - Settings: default provider, batch size, delay, retries, UI toggles.
- On first activation it can redirect to a dedicated welcome screen (one-time).
- On plugin settings pages it suppresses other admin notices for a cleaner UI.

4) AI Providers (What can be configured)
- Supports multiple provider types (as selectable, enabled/disabled providers):
  - OpenAI, Claude (Anthropic), Gemini (Google), DeepL, Grok (xAI), DeepSeek, Mistral.
- Each provider configuration stores:
  - Enabled toggle
  - API key
  - Model identifier
  - Base URL (stored and used as the request base; some providers hide this field in UI but it is still stored)
- Provider connection testing exists as an admin action (sends a small request and reports success/failure in UI).

5) Translation Features Inside Loco Translate Editor
- Injects an “AI Translate” entry point into the Loco editor toolbar.
- Provides a modal-driven translation flow:
  - Provider selector (only enabled providers with an API key appear).
  - Custom prompt field (optional, per-translation session override/append).
  - Mode selection:
    - Translate Empty Only (only untranslated strings)
    - Translate All (re-translate everything)
  - Progress display with counts and percent complete.
  - Stop button with confirmation (safe stop).
  - Results summary: translated count + failed count.
  - “Apply to Loco” button that saves translations into the PO file and refreshes the editor page to show changes.
- Single-string translation button:
  - Adds a small “AI” button in the editor header area (next to existing Loco controls) that translates the currently selected source string and fills the target field.
  - Updates Loco editor’s UI state to mark content as modified (unsaved) after inserting the translation.
- PO data capture strategies:
  - Captures Loco’s internal PO dataset early (before Loco modifies it) using an early admin-head script hook.
  - Falls back to reading from Loco runtime config or parsing inline scripts if needed.

6) Batch Translation Behavior (Normal Mode)
- Collects translatable strings from Loco’s PO dataset (skips the header row and empty sources).
- Sends translation batches to the server with indices + source texts.
- Applies a configurable delay between batches to reduce rate-limit risk.
- Tracks and displays per-item results (source → translation for success, source + error snippet for failures).

7) Turbo Mode (Parallel Processing)
- Optional “Turbo Mode” translates faster by running multiple batches concurrently.
- Turbo mode parameters are configurable:
  - Concurrent batches (validated range: 2–5)
  - Turbo batch size (validated range: 10–100)
  - Turbo delay between batch groups (validated range: 0–2000 ms)
- Turbo mode still respects stop behavior and finalizes once active requests complete.

8) Placeholder / Code / Non-Translatable Protection
- Before sending text to AI, it protects non-translatable tokens by replacing them with stable placeholders (for example [PH000], [PH001] …).
- The protection patterns include:
  - printf placeholders (%s, %d, %1$s, etc.)
  - HTML tags
  - WordPress shortcodes
  - Template variables ({{var}}, {var})
  - URLs and email addresses
- Do-not-translate glossary terms are also protected using dedicated placeholders (for example [DNT000] …).
- After AI returns translations, it restores placeholders back to original tokens.
- Restoration also attempts to repair common AI “corruptions” of placeholders (extra spaces, parentheses/braces variants, etc.).

9) Glossary System (Translation Rules)
- Do Not Translate:
  - List of terms (one per line) that must remain exactly as written.
- Fixed Translations:
  - Rule list mapping “source term” → “target translation” that will be applied after translation for consistent terminology.
- Glossary supports export to JSON and import from JSON (for portability).

10) Prompt System (How instructions are controlled)
- System prompt:
  - Editable in settings and used as the “system” instruction for supported providers.
  - Includes default guidance to preserve placeholders/HTML/shortcodes and to return only translated text.
  - Can be reset to default in the UI.
- Default custom prompt:
  - Stored in settings and used as additional context/instructions.
  - Can be overridden or appended per translation session in the Loco modal.
- For batch translation calls, providers request a JSON array response with one translated item per source string; responses are parsed to extract a JSON array even if wrapped in code blocks.
- For single translation calls, providers request “translated text only” output.

11) Saving to PO/MO (Apply to Loco)
- “Apply to Loco” sends translated items (by index) and the PO file path to the server.
- Server-side apply flow:
  - Resolves the PO path under the WordPress content directory.
  - Loads PO entries via Loco Translate classes.
  - Sets translated targets by index.
  - Saves the PO file back to disk (prefers WordPress filesystem APIs, with a direct-write fallback).
  - Attempts to compile the MO file; MO compilation failure does not block PO saving.

12) Security & Permissions
- All plugin admin actions require administrator capability (manage_options).
- Requests are protected with WordPress nonces:
  - One nonce family for saving settings/providers/glossary/prompts/turbo and testing providers.
  - A separate nonce family for translation actions inside the Loco editor (batch translate, single translate, apply translations).

13) Operational Notes / Not Found
- An “Auto Save” setting exists in the general settings UI and is stored in options; however, no automatic-save behavior was found implemented in the Loco integration script (manual “Apply to Loco” is present).

