훅을 통한 데이터 재정의

훅을 통한 데이터 재정의

이 섹션에서는 PHP 훅을 사용하여 콘텐츠 번역에 사용되는 데이터를 재정의하는 방법에 대해 설명합니다.

AI 번역 제공업체를 위한 프롬프트

PHP 코드의 훅을 사용하여 AI 번역 제공업체에 전송되는 프롬프트를 커스터마이징할 수 있습니다.

다음 항목을 커스터마이징할 수 있습니다:

  • 시스템 메시지
  • 프롬프트 템플릿
  • 프롬프트

이들 각각에 대해 두 가지 훅이 있습니다:

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

첫 번째 훅은 모든 제공업체에 대해 변수를 수정하는 데 사용됩니다.

두 번째 훅은 특정 제공업체에 대해 변수를 수정하는 데 사용됩니다.

지원되는 제공업체 이름은 다음과 같습니다:

  • 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
);