PhoenixKit. Modules. Publishing. Web. Editor. Translation
(PhoenixKitPublishing v0.2.0)
Copy Markdown
View Source
AI translation functionality for the publishing editor.
Handles translation workflow, Oban job enqueuing, and translation progress tracking.
Summary
Functions
Checks if AI translation is available (AI module installed + enabled + endpoints configured).
Builds warnings for the translation confirmation modal.
The shipped translation prompt template.
Checks if the default translation prompt already exists.
Whether the persisted default prompt is stale — it exists but predates the
lowercase-placeholder standardization, so it still references {{Title}}/
{{Content}} and would not bind the adapter's title/content keys (the
model would hallucinate). Drives a "Regenerate default prompt" affordance so a
stale row isn't a dead end (the "Generate" button hides once a prompt exists).
Actually enqueues the translation job (after confirmation if needed).
Enqueues translation job with validation and warnings. Returns {:noreply, socket} for use in handle_event.
Generates the default translation prompt in the AI prompts system. Returns {:ok, prompt} or {:error, changeset}.
Gets all target languages for translation (all except primary).
Gets the default AI endpoint UUID from settings.
Gets the default AI prompt UUID for translation (setting, then slug fallback).
Gets target languages for translation (missing languages only).
Lists available AI endpoints for translation as [{uuid, name}].
Lists available AI prompts for translation as [{uuid, name}].
Clears completed translation status when switching languages.
Restores translation status from an active Oban job if one exists for this post. Call on mount to survive page refreshes.
Repairs a stale default prompt by rewriting its content to the current
template in place (preserving its uuid + any endpoint wiring), or creates
it if absent. Returns {:ok, prompt} or {:error, reason}.
Returns {title_blank?, content_blank?} for the translation source.
Checks if the source body content is blank. Kept for callers that only care
about the body; source_blank_state/1 is the richer title+content variant.
Returns the source language for translation based on the post's primary language or the system default.
Starts translation to the current (non-primary) language.
Functions
Checks if AI translation is available (AI module installed + enabled + endpoints configured).
Builds warnings for the translation confirmation modal.
@spec default_prompt_content() :: String.t()
The shipped translation prompt template.
The {{title}} / {{content}} placeholders are lowercase on purpose —
they must match, byte-for-byte, the keys AITranslatable.source_fields/2
binds (the same lowercase convention as the catalogue and projects prompts),
because core's prompt-variable substitution (PhoenixKitAI.Prompt.render) is
case-sensitive and silently leaves an unmatched {{Var}} literal in the
prompt. The defensive "skip a value that is still a literal placeholder"
rule makes a binding mismatch fail closed (the field is skipped, the job
fails cleanly on a missing marker) rather than producing a hallucinated
translation. editor_translation_test.exs pins the placeholder↔key invariant
so the two can't drift apart again. Exposed (not inlined) so that test can
read the template without writing a DB row.
Checks if the default translation prompt already exists.
Whether the persisted default prompt is stale — it exists but predates the
lowercase-placeholder standardization, so it still references {{Title}}/
{{Content}} and would not bind the adapter's title/content keys (the
model would hallucinate). Drives a "Regenerate default prompt" affordance so a
stale row isn't a dead end (the "Generate" button hides once a prompt exists).
Actually enqueues the translation job (after confirmation if needed).
Enqueues translation job with validation and warnings. Returns {:noreply, socket} for use in handle_event.
Generates the default translation prompt in the AI prompts system. Returns {:ok, prompt} or {:error, changeset}.
Gets all target languages for translation (all except primary).
Gets the default AI endpoint UUID from settings.
Gets the default AI prompt UUID for translation (setting, then slug fallback).
Gets target languages for translation (missing languages only).
Lists available AI endpoints for translation as [{uuid, name}].
Lists available AI prompts for translation as [{uuid, name}].
Clears completed translation status when switching languages.
Restores translation status from an active Oban job if one exists for this post. Call on mount to survive page refreshes.
Repairs a stale default prompt by rewriting its content to the current
template in place (preserving its uuid + any endpoint wiring), or creates
it if absent. Returns {:ok, prompt} or {:error, reason}.
Returns {title_blank?, content_blank?} for the translation source.
The source feeds two translatable fields — the title and the body content —
so the confirmation modal can warn precisely (nothing, only-title, or
only-content) instead of a blanket "content is empty". The effective title
mirrors the adapter's extract_title/1: the first # heading of the body,
else the stored/typed title (the default "Untitled" counts as no title).
Checks if the source body content is blank. Kept for callers that only care
about the body; source_blank_state/1 is the richer title+content variant.
Returns the source language for translation based on the post's primary language or the system default.
Starts translation to the current (non-primary) language.