PhoenixKit.Modules.Publishing.Workers.TranslatePostWorker (phoenix_kit v1.7.38)
Copy Markdown View SourceOban worker for translating publishing posts to multiple languages using AI.
This worker translates the primary language version of a post to all enabled languages (or a specified subset). Each language translation is processed sequentially to avoid overwhelming the AI endpoint.
Usage
# Translate to all enabled languages
PhoenixKit.Modules.Publishing.translate_post_to_all_languages(
"docs",
"getting-started",
endpoint_id: 1
)
# Or enqueue directly
%{
"group_slug" => "docs",
"post_slug" => "getting-started",
"endpoint_id" => 1
}
|> TranslatePostWorker.new()
|> Oban.insert()Job Arguments
group_slug- The publishing group slugpost_slug- The post slug (for slug mode) or date/time path (for timestamp mode)endpoint_id- AI endpoint ID to use for translationsource_language- Source language to translate from (optional, defaults to primary language)target_languages- List of target languages (optional, defaults to all enabled except source)version- Version number to translate (optional, defaults to latest/published)user_id- User ID for audit trail (optional)
Configuration
Set the default AI endpoint for translations in Settings:
PhoenixKit.Settings.update_setting("publishing_translation_endpoint_id", "1")
Summary
Functions
Creates a new translation job for a post.
Enqueues a translation job for a post.
Translates content and returns the result without saving.
Translates a post to a single language synchronously (without queuing).
Functions
Creates a new translation job for a post.
Options
:endpoint_id- AI endpoint ID (required if not set in settings):source_language- Source language (defaults to primary language):target_languages- List of target languages (defaults to all enabled except source):version- Version to translate (defaults to latest):user_id- User ID for audit trail
Examples
TranslatePostWorker.create_job("docs", "getting-started", endpoint_id: 1)
TranslatePostWorker.create_job("docs", "getting-started",
endpoint_id: 1,
target_languages: ["es", "fr"]
)
Enqueues a translation job for a post.
See create_job/3 for options.
Examples
{:ok, job} = TranslatePostWorker.enqueue("docs", "getting-started", endpoint_id: 1)
Translates content and returns the result without saving.
Use this when you want to display the translation in the UI first, allowing the user to review/edit before saving.
Parameters
group_slug- The publishing group slugpost_slug- The post slugtarget_language- The target language code (e.g., "es")opts- Options::endpoint_id- AI endpoint ID to use (required):source_language- Source language code (defaults to post's primary language):version- Version to translate (defaults to latest)
Returns
{:ok, %{title: title, url_slug: slug, content: content}}on success{:error, reason}on failure
Example
{:ok, result} = TranslatePostWorker.translate_content("docs", "getting-started", "es", endpoint_id: 1)
# => {:ok, %{title: "Primeros Pasos", url_slug: "primeros-pasos", content: "..."}}
Translates a post to a single language synchronously (without queuing).
Use this for immediate translation of a single language, e.g., when the user clicks "Translate to This Language" while viewing a non-primary language.
Parameters
group_slug- The publishing group slugpost_slug- The post slugtarget_language- The target language code (e.g., "es")opts- Options::endpoint_id- AI endpoint ID to use (required):source_language- Source language code (defaults to post's primary language):version- Version to translate (defaults to latest):user_id- User ID for audit trail
Returns
:okon success{:error, reason}on failure
Example
:ok = TranslatePostWorker.translate_now("docs", "getting-started", "es", endpoint_id: 1)