フック
フックフックによるデータの上書き

フックによるデータの上書き

このセクションでは、PHPフックを使用してコンテンツ翻訳に使用するデータを上書きする方法について説明します。

AI翻訳プロバイダーへのプロンプト

PHPコードのフックを使用して、AI翻訳プロバイダーに送信されるプロンプトをカスタマイズできます。

以下の項目をカスタマイズできます:

  • システムメッセージ
  • プロンプトテンプレート
  • プロンプト

これらのそれぞれに対して、2つのフックがあります:

  • gatompl:<hook_name>
  • gatompl:<hook_name>:<provider_name>

最初のフックは、すべてのプロバイダーに対して変数を変更するために使用されます。

2番目のフックは、特定のプロバイダーに対して変数を変更するために使用されます。

以下のプロバイダー名がサポートされています:

  • chatgpt
  • claude
  • deepseek
  • gemini
  • mistral
  • openrouter
  • self_hosted_llm

以下のフックは、翻訳対象のエンティティデータ(例:投稿ID、カスタム投稿タイプなど)を受け取らず、言語コードと翻訳する文字列のみを受け取ります。

エンティティデータが必要な場合は、アクションフック gatompl:query_execution_start を使用して取得できます。こちらの例をご参照ください。

このフックはクエリが実行される前にトリガーされるため、データを変数に保存して以下のフィルターフックのいずれかで使用できます。

システムメッセージ

システムメッセージは、AIが翻訳のコンテキストを理解するためのものです。例:

You are a language translator.

gatompl:system_message

add_filter(
  'gatompl:system_message',
  function (string $systemMessage, string $providerName): string {
    return $systemMessage;
  },
  10,
  2
);

gatompl:system_message:<provider_name>

add_filter(
  'gatompl:system_message:chatgpt',
  function (string $systemMessage): string {
    return $systemMessage;
  }
);

プロンプトテンプレート

プロンプトテンプレートには、実行時に解決される変数プレースホルダーが含まれています。例:

I'm working on internationalizing my application.
 
I've created a JSON with sentences in {$sourceLanguage}. Please translate the sentences to {$targetLanguage} from {$targetCountry}.

gatompl:prompt_template

add_filter(
  'gatompl:prompt_template',
  function (string $promptTemplate, string $providerName): string {
    return $promptTemplate;
  },
  10,
  2
);

gatompl:prompt_template:<provider_name>

add_filter(
  'gatompl:prompt_template:chatgpt',
  function (string $promptTemplate): string {
    return $promptTemplate;
  }
);

プロンプト

プロンプトは、プロンプトテンプレートが解決された後にAIサービスに送信される実際のプロンプトです。レスポンスの形式が正しくなるよう追加情報を加えます。例:

I'm working on internationalizing my application.
 
I've created a JSON with sentences in English. Please translate the sentences to French from France.
 
If a sentence contains HTML:
- Translate the text inside the HTML tags. (eg: `<p>Hello world</p>` => `<p>Hola mundo</p>`)
- Translate the following properties inside the HTML tags: alt, title, placeholder, aria-label, aria-describedby, aria-labelledby, aria-placeholder. Do not translate any other property.
- Ensure that any double quotes (") within a translated string inside an HTML tag attribute are properly escaped by adding a backslash before them (\"), but only if they haven't been escaped already.
- Ensure that the quotes in HTML tag attributes are not escaped (eg: keep `<mark class="has-inline-color">` as is, do not convert to `<mark class=\"has-inline-color\">`).
- Ensure that slashes within HTML tags are not escaped (eg: keep `<p>Hello world</p>` as is, do not convert to `<p>Hello world<\/p>`).
Keep emojis exactly as they are, do not translate them.
Ensure that the response is encoded using UTF-8 for all characters.

フックは以下の追加パラメーターを受け取ります:

パラメーター説明
$contents翻訳する文字列['hello world']
$sourceLanguageCode翻訳元言語のISO-639コードen
$sourceLanguageName翻訳元言語の名称(英語)English
$targetLanguageCode翻訳先言語のISO-639コードfr
$targetLanguageName翻訳先言語の名称(英語)French
$targetCountryCode翻訳をローカライズする国のISO-3166コードFR
$targetCountryName翻訳をローカライズする国の名称(英語)France

gatompl:prompt

add_filter(
  'gatompl:prompt',
  /**
   * @param string[] $contents The strings to be translated (eg: `['hello world', 'how are you?']`).
   */
  function (
    string $prompt,
    string $providerName,
    array $contents,
    string $sourceLanguageCode,
    string $sourceLanguageName,
    string $targetLanguageCode,
    string $targetLanguageName,
    string $targetCountryCode,
    string $targetCountryName
): string {
    return $prompt;
  },
  10,
  9
);

gatompl:prompt:<provider_name>

add_filter(
  'gatompl:prompt:chatgpt',
  /**
   * @param string[] $contents The strings to be translated (eg: `['hello world', 'how are you?']`).
   */
  function (
    string $prompt,
    array $contents,
    string $sourceLanguageCode,
    string $sourceLanguageName,
    string $targetLanguageCode,
    string $targetLanguageName,
    string $targetCountryCode,
    string $targetCountryName
): string {
    return $prompt;
  },
  10,
  8
);

クエリ変数

Gato AI Translations for PolylangGraphQLクエリを実行して翻訳を行います。プラグインの設定で定義された設定をGraphQL変数を通じてクエリに渡します。

以下のフックを使用してクエリ変数をカスタマイズできます:

  • gatompl:query_variables

フックは以下の追加パラメーターを受け取ります:

パラメーター説明
$querySlug実行するクエリのスラッグtranslate-customposts

サポートされているクエリスラッグの一覧は、クエリ実行フックセクションで確認できます。

add_filter(
  'gatompl:query_variables',
  /**
   * @param array<string, mixed> $variables The variables to pass to the query.
   * @return array<string, mixed> The variables to pass to the query.
   */
  function (
    array $variables,
    string $querySlug
): array {
    return $variables;
  },
  10,
  2
);

メタキー

以下のフックを使用して、同期・翻訳するメタキーをカスタマイズできます:

  • gatompl:syncable_meta_keys
  • gatompl:translatable_meta_keys
  • gatompl:custompost_and_media_entity_reference_translatable_meta_keys
  • gatompl:taxonomy_entity_reference_translatable_meta_keys

フックは以下のパラメーターを受け取ります:

パラメーター説明
$object翻訳対象のエンティティ。カスタム投稿やメディアの場合は WP_Post 型、タグやカテゴリーの場合は WP_Term
$startingMetaKeysエンティティに存在するメタキーの配列

gatompl:syncable_meta_keys

元のエンティティから翻訳されたエンティティにコピーするメタキー(投稿、メディア、タグ、カテゴリー用)。

add_filter(
  'gatompl:syncable_meta_keys',
  /**
   * @param string[] $metaKeys
   * @param string[] $startingMetaKeys
   * @return string[]
   */
  function (array $metaKeys, WP_Post | WP_Term $object, array $startingMetaKeys): array
  {
    $metaKeysToCopy = $object instanceof WP_Post ? [
      '_myproject_meta_key_for_posts',
    ] : [
      '_myproject_meta_key_for_terms',
    ];
    return array_merge($metaKeys, array_intersect($startingMetaKeys, $metaKeysToCopy));
  },
  10,
  3
);

gatompl:translatable_meta_keys

文字列を含むメタキー。元のエンティティから翻訳されたエンティティにコピーして翻訳します。

add_filter(
  'gatompl:translatable_meta_keys',
  /**
   * @param string[] $metaKeys
   * @param string[] $startingMetaKeys
   * @return string[]
   */
  function (array $metaKeys, WP_Post | WP_Term $object, array $startingMetaKeys): array
  {
    $metaKeysToCopy = $object instanceof WP_Post ? [
      '_myproject_meta_key_for_posts',
    ] : [
      '_myproject_meta_key_for_terms',
    ];
    return array_merge($metaKeys, array_intersect($startingMetaKeys, $metaKeysToCopy));
  },
  10,
  3
);

gatompl:custompost_and_media_entity_reference_translatable_meta_keys

投稿IDへの参照を含むメタキー(カスタム投稿やメディア)。対象言語の対応するIDにコピーして翻訳します。

add_filter(
  'gatompl:custompost_and_media_entity_reference_translatable_meta_keys',
  /**
   * @param string[] $metaKeys
   * @param string[] $startingMetaKeys
   * @return string[]
   */
  function (array $metaKeys, WP_Post | WP_Term $object, array $startingMetaKeys): array
  {
    $metaKeysToCopy = $object instanceof WP_Post ? [
      '_myproject_meta_key_for_posts',
    ] : [
      '_myproject_meta_key_for_terms',
    ];
    return array_merge($metaKeys, array_intersect($startingMetaKeys, $metaKeysToCopy));
  },
  10,
  3
);

gatompl:taxonomy_entity_reference_translatable_meta_keys

タクソノミータームIDへの参照を含むメタキー(タグやカテゴリー)。対象言語の対応するIDにコピーして翻訳します。

add_filter(
  'gatompl:taxonomy_entity_reference_translatable_meta_keys',
  /**
   * @param string[] $metaKeys
   * @param string[] $startingMetaKeys
   * @return string[]
   */
  function (array $metaKeys, WP_Post | WP_Term $object, array $startingMetaKeys): array
  {
    $metaKeysToCopy = $object instanceof WP_Post ? [
      '_myproject_meta_key_for_posts',
    ] : [
      '_myproject_meta_key_for_terms',
    ];
    return array_merge($metaKeys, array_intersect($startingMetaKeys, $metaKeysToCopy));
  },
  10,
  3
);