Nâng cao
Nâng caoDịch thuật qua code PHP

Dịch thuật qua code PHP

Bạn có thể kích hoạt dịch thuật qua code PHP trong ứng dụng, theme hoặc plugin của mình.

Lớp GatoStandalone\GatoAITranslationsForPolylang\Gato cung cấp các phương thức tĩnh để kích hoạt dịch thuật:

Phương thứcMô tả
translateBí danh của translateCustomPosts
translateCustomPostsDịch các custom post (trang, bài viết, loại bài viết tùy chỉnh)
translateTaxonomyTermsDịch các thuật ngữ phân loại (danh mục, thẻ, phân loại tùy chỉnh)
translateMediaDịch các mục phương tiện (hình ảnh, tài liệu, v.v.)

Chữ ký phương thức

Đây là chữ ký phương thức cho tất cả các phương thức dịch thuật:

Chỉ tham số ids là bắt buộc. Tất cả các tham số khác, nếu không được cung cấp, sẽ được đặt bằng giá trị từ Cài đặt plugin.

Bạn có thể tìm thấy lớp này trong tệp src/Gato.php bên trong plugin.

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

Ngữ cảnh thực thi

Chỉ thực thi bất kỳ phương thức nào sau khi plugin đã được khởi tạo. Điều đó xảy ra trên các hook khác nhau, tùy thuộc vào ngữ cảnh:

Ngữ cảnhHook
FrontendAction hook 'wp'
AdminAction hook 'wp_loaded'
REST APIFilter hook 'rest_jsonp_enabled'

Ví dụ thực thi

Thực thi dịch thuật từ bên trong từng WordPress hook:

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

Nếu bạn chắc chắn rằng:

  • Polylang đang hoạt động
  • Bạn có giấy phép hợp lệ cho plugin
  • Plugin đã được khởi tạo (tức là các hook ở trên đã được thực thi)

...thì bạn có thể thực thi dịch thuật mà không cần kiểm tra ngoại lệ:

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