上級PHPコードによる翻訳
PHPコードによる翻訳
アプリケーション、テーマ、またはプラグイン内のPHPコードから翻訳をトリガーできます。
クラス GatoStandalone\GatoAITranslationsForPolylang\Gato は、翻訳をトリガーするための静的メソッドを提供します:
| メソッド | 説明 |
|---|---|
translate | translateCustomPosts のエイリアス |
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]);