Hooks
Hooksการแทนที่ข้อมูลผ่าน hooks

การแทนที่ข้อมูลผ่าน hooks

ส่วนนี้อธิบายวิธีการแทนที่ข้อมูลที่ใช้ในการแปลเนื้อหาผ่าน PHP hooks

Prompts สำหรับผู้ให้บริการแปล AI

คุณสามารถปรับแต่ง prompts ที่ส่งไปยังผู้ให้บริการแปล AI ผ่าน hooks ในโค้ด PHP ได้

สิ่งที่สามารถปรับแต่งได้มีดังนี้:

  • System message
  • Prompt template
  • Prompt

สำหรับแต่ละรายการ มี hooks สองตัว:

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

Hook ตัวแรกใช้สำหรับแก้ไขตัวแปรสำหรับผู้ให้บริการทั้งหมด

Hook ตัวที่สองใช้สำหรับแก้ไขตัวแปรสำหรับผู้ให้บริการที่ระบุเจาะจง

ชื่อผู้ให้บริการที่รองรับมีดังนี้:

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

Hooks ด้านล่างนี้ไม่ได้รับข้อมูล entity ที่ต้องการแปล (เช่น post ID, custom post type และอื่นๆ) แต่รับเฉพาะรหัสภาษาและสตริงที่ต้องการแปลเท่านั้น

หากคุณต้องการข้อมูล entity สามารถดึงข้อมูลได้ผ่าน action hook gatompl:query_execution_start ตามตัวอย่างนี้

เนื่องจาก hook นี้จะถูกเรียกใช้ก่อนที่ query จะถูกดำเนินการ คุณสามารถเก็บข้อมูลไว้ในตัวแปรและใช้งานใน filter hooks ใดๆ ด้านล่างได้

System message

System message มีไว้เพื่อให้ 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;
  }
);

Prompt template

Prompt template มี variable placeholders ที่จะถูก resolve ในขณะ runtime เช่น:

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

Prompt

Prompt คือ prompt จริงที่ส่งไปยังบริการ AI หลังจาก prompt template ถูก resolve แล้ว โดยจะเพิ่มข้อมูลเพิ่มเติมเพื่อให้แน่ใจว่ารูปแบบของการตอบสนองถูกต้อง เช่น:

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.

Hooks จะรับพารามิเตอร์เพิ่มเติมดังต่อไปนี้:

พารามิเตอร์คำอธิบายตัวอย่าง
$contentsสตริงที่ต้องการแปล['hello world']
$sourceLanguageCodeรหัส ISO-639 ของภาษาต้นทางที่ต้องการแปลen
$sourceLanguageNameชื่อภาษาต้นทาง (เป็นภาษาอังกฤษ)English
$targetLanguageCodeรหัส ISO-639 ของภาษาปลายทางที่ต้องการแปลไปfr
$targetLanguageNameชื่อภาษาปลายทาง (เป็นภาษาอังกฤษ)French
$targetCountryCodeรหัส ISO-3166 ของประเทศสำหรับ localize การแปลFR
$targetCountryNameชื่อประเทศ (เป็นภาษาอังกฤษ) สำหรับ localize การแปล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
);

Query Variables

Gato AI Translations for Polylang ดำเนินการ GraphQL query เพื่อทำการแปล โดยส่งการกำหนดค่า (ที่กำหนดไว้ในการตั้งค่าปลั๊กอิน) ไปยัง query ผ่าน GraphQL variables

คุณสามารถปรับแต่ง query variables ผ่าน hook ต่อไปนี้:

  • gatompl:query_variables

Hook จะรับพารามิเตอร์เพิ่มเติมดังต่อไปนี้:

พารามิเตอร์คำอธิบายตัวอย่าง
$querySlugSlug ของ query ที่ต้องการดำเนินการtranslate-customposts

รายการ query slugs ที่รองรับมีอยู่ในส่วน Query execution hooks

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

Meta keys

คุณสามารถปรับแต่ง meta keys ที่จะถูกซิงค์/แปล ผ่าน hooks ต่อไปนี้:

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

Hooks จะรับพารามิเตอร์ดังต่อไปนี้:

พารามิเตอร์คำอธิบาย
$objectEntity ที่กำลังถูกแปล ซึ่งมีประเภทเป็น WP_Post (สำหรับ custom posts และ media) หรือ WP_Term (สำหรับ tags และ categories)
$startingMetaKeysอาร์เรย์ของ meta keys ที่มีอยู่ใน entity

gatompl:syncable_meta_keys

Meta keys สำหรับคัดลอกจาก entity ต้นทางไปยัง entity ที่แปลแล้ว (สำหรับ posts, media, tags และ categories)

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

Meta keys ที่มีสตริง สำหรับคัดลอกและแปลจาก entity ต้นทางไปยัง entity ที่แปลแล้ว

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

Meta keys ที่มีการอ้างอิงไปยัง post IDs (เช่น custom posts และ media) สำหรับคัดลอกและแปลไปยัง 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

Meta keys ที่มีการอ้างอิงไปยัง taxonomy term IDs (เช่น tags และ categories) สำหรับคัดลอกและแปลไปยัง 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
);