고급
고급PHP 코드를 통한 번역

PHP 코드를 통한 번역

애플리케이션, 테마 또는 플러그인 내에서 PHP 코드를 통해 번역을 트리거할 수 있습니다.

클래스 GatoStandalone\GatoAITranslationsForPolylang\Gato는 번역을 트리거하는 정적 메서드를 제공합니다:

메서드설명
translatetranslateCustomPosts의 별칭
translateCustomPosts커스텀 게시물(페이지, 포스트, 커스텀 포스트 타입)을 번역합니다
translateTaxonomyTerms택소노미 텀(카테고리, 태그, 커스텀 택소노미)을 번역합니다
translateMedia미디어 항목(이미지, 문서 등)을 번역합니다

메서드 시그니처

모든 번역 메서드의 시그니처는 다음과 같습니다:

필수 파라미터는 ids뿐입니다. 다른 파라미터는 지정하지 않으면 플러그인 설정값이 사용됩니다.

이 클래스는 플러그인 내 src/Gato.php 파일에서 찾을 수 있습니다.

namespace GatoStandalone\GatoAITranslationsForPolylang;
 
class Gato
{
  /**
   * Alias of `translateCustomPosts`
   *
   * @param int|int[] $ids Array of custom post IDs to translate
   * @param string|null $statusToUpdate The status the custom posts must have to be updated
   * @param string|null $statusWhenTranslated The status the custom posts will have after translation. Possible values: "draft", "pending", "publish", "private", "current" (i.e. don't modify the status), "same-as-origin" (i.e. copy the status from the origin post)
   * @param array<string,string>|null $languageProviders Array of: Key => language code, Value: Provider name, "none" to disable for that language, or "default" to use the default provider
   * @return array<string|int>|false Array of custom post IDs that were processed for translation, or `false` if none of the provided IDs was valid
   * @throws PolylangNotActiveException
   * @throws LicenseNotActiveException
   * @throws PluginNotInitializedException
   */
  public static function translate(
    int|array $ids,
    ?string $statusToUpdate = null,
    ?string $statusWhenTranslated = null,
    ?bool $copyDate = null,
    ?bool $translateSlugs = null,
    ?array $languageProviders = null,
    ?string $defaultTranslationProvider = null,
  ): array|false;
 
  /**
   * Translate custom posts
   *
   * @param int|int[] $ids Array of custom post IDs to translate
   * @param string|null $statusToUpdate The status the custom posts must have to be updated
   * @param string|null $statusWhenTranslated The status the custom posts will have after translation. Possible values: "draft", "pending", "publish", "private", "current" (i.e. don't modify the status), "same-as-origin" (i.e. copy the status from the origin post)
   * @param array<string,string>|null $languageProviders Array of: Key => language code, Value: Provider name, "none" to disable for that language, or "default" to use the default provider
   * @return array<string|int>|false Array of custom post IDs that were processed for translation, or `false` if none of the provided IDs was valid
   * @throws PolylangNotActiveException
   * @throws LicenseNotActiveException
   * @throws PluginNotInitializedException
   */
  public static function translateCustomPosts(
    int|array $ids,
    ?string $statusToUpdate = null,
    ?string $statusWhenTranslated = null,
    ?bool $copyDate = null,
    ?bool $translateSlugs = null,
    ?array $languageProviders = null,
    ?string $defaultTranslationProvider = null,
  ): array|false;
 
  /**
   * Translate taxonomy terms (categories and tags)
   *
   * @param int|int[] $ids Array of taxonomy term IDs to translate
   * @param array<string,string>|null $languageProviders Array of: Key => language code, Value: Provider name, "none" to disable for that language, or "default" to use the default provider
   * @return array<string|int>|false Array of taxonomy term IDs that were processed for translation, or `false` if none of the provided IDs was valid
   * @throws PolylangNotActiveException
   * @throws LicenseNotActiveException
   * @throws PluginNotInitializedException
   */
  public static function translateTaxonomyTerms(
    int|array $ids,
    ?bool $translateSlugs = null,
    ?array $languageProviders = null,
    ?string $defaultTranslationProvider = null,
  ): array|false;
 
  /**
   * Translate media items
   *
   * @param int|int[] $ids Array of media item IDs to translate
   * @param array<string,string>|null $languageProviders Array of: Key => language code, Value: Provider name, "none" to disable for that language, or "default" to use the default provider
   * @return array<string|int>|false Array of media item IDs that were processed for translation, or `false` if none of the provided IDs was valid
   * @throws PolylangNotActiveException
   * @throws LicenseNotActiveException
   * @throws PluginNotInitializedException
   */
  public static function translateMedia(
    int|array $ids,
    ?bool $translateSlugs = null,
    ?array $languageProviders = null,
    ?string $defaultTranslationProvider = null,
  ): array|false;
}

실행 컨텍스트

플러그인이 초기화된 후에만 메서드를 실행하세요. 초기화는 컨텍스트에 따라 다른 훅에서 이루어집니다:

컨텍스트
프런트엔드'wp' 액션 훅
관리자'wp_loaded' 액션 훅
REST API'rest_jsonp_enabled' 필터 훅

실행 예시

각 WordPress 훅 내에서 번역을 실행하는 예시:

use GatoStandalone\GatoAITranslationsForPolylang\Exception\AbstractGatoAITranslationsForPolylangException;
 
// Frontend
add_action('wp', function() {
  try {
    Gato::translateCustomPosts(123);
  } catch (AbstractGatoAITranslationsForPolylangException $e) {
    error_log($e->getMessage());
  }
});
 
// Admin
add_action('wp_loaded', function() {
  try {
    Gato::translateTaxonomyTerms([456, 789]);
  } catch (AbstractGatoAITranslationsForPolylangException $e) {
    error_log($e->getMessage());
  }
});
 
// REST API
add_filter('rest_jsonp_enabled', function(mixed $value): mixed {
  try {
    Gato::translateMedia([101, 102]);
  } catch (AbstractGatoAITranslationsForPolylangException $e) {
    error_log($e->getMessage());
  }
  return $value;
});

다음 조건이 모두 충족되는 경우:

  • Polylang이 활성화되어 있음
  • 플러그인의 유효한 라이선스를 보유하고 있음
  • 플러그인이 초기화되어 있음(즉, 위의 훅이 실행된 상태임)

…예외 확인 없이 번역을 실행할 수 있습니다:

Gato::translate(123); // Same as `translateCustomPosts`
Gato::translateCustomPosts(123);
Gato::translateTaxonomyTerms([456, 789]);
Gato::translateMedia([101, 102]);