PhoenixKit.Modules.Publishing.Workers.TranslatePostWorker (phoenix_kit v1.7.71)

Copy Markdown View Source

Oban 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",
  "019cce93-ed2e-7e1b-...",
  endpoint_uuid: "endpoint-uuid"
)

# Or enqueue directly
%{
  "group_slug" => "docs",
  "post_uuid" => "019cce93-ed2e-7e1b-...",
  "endpoint_uuid" => "endpoint-uuid"
}
|> TranslatePostWorker.new()
|> Oban.insert()

Job Arguments

  • group_slug - The publishing group slug
  • post_uuid - The post UUID
  • endpoint_uuid - AI endpoint UUID to use for translation
  • source_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_uuid - User UUID for audit trail (optional)

Configuration

Set the default AI endpoint for translations in Settings:

PhoenixKit.Settings.update_setting("publishing_translation_endpoint_uuid", "1")

Summary

Functions

Checks if there's an active translation job for the given post. Returns the job if found, nil otherwise.

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

active_job(post_uuid)

Checks if there's an active translation job for the given post. Returns the job if found, nil otherwise.

create_job(group_slug, post_uuid, opts \\ [])

Creates a new translation job for a post.

Options

  • :endpoint_uuid - AI endpoint UUID (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_uuid - User UUID for audit trail

Examples

TranslatePostWorker.create_job("docs", "019cce93-...", endpoint_uuid: "endpoint-uuid")
TranslatePostWorker.create_job("docs", "019cce93-...",
  endpoint_uuid: "endpoint-uuid",
  target_languages: ["es", "fr"]
)

enqueue(group_slug, post_uuid, opts \\ [])

Enqueues a translation job for a post.

See create_job/3 for options.

Examples

{:ok, job} = TranslatePostWorker.enqueue("docs", "019cce93-...", endpoint_uuid: "endpoint-uuid")

translate_content(group_slug, post_uuid, target_language, opts \\ [])

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 slug
  • post_uuid - The post UUID
  • target_language - The target language code (e.g., "es")
  • opts - Options:
    • :endpoint_uuid - AI endpoint UUID 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", "019cce93-...", "es", endpoint_uuid: "endpoint-uuid")
# => {:ok, %{title: "Primeros Pasos", url_slug: "primeros-pasos", content: "..."}}

translate_now(group_slug, post_uuid, target_language, opts \\ [])

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 slug
  • post_uuid - The post UUID
  • target_language - The target language code (e.g., "es")
  • opts - Options:
    • :endpoint_uuid - AI endpoint UUID to use (required)
    • :source_language - Source language code (defaults to post's primary language)
    • :version - Version to translate (defaults to latest)
    • :user_uuid - User UUID for audit trail

Returns

  • :ok on success
  • {:error, reason} on failure

Example

:ok = TranslatePostWorker.translate_now("docs", "019cce93-...", "es", endpoint_uuid: "endpoint-uuid")