ขั้นสูงการแปลผ่านโค้ด PHP
การแปลผ่านโค้ด PHP
คุณสามารถเรียกใช้การแปลผ่านโค้ด PHPภายในแอปพลิเคชัน ธีม หรือปลั๊กอินของคุณได้
คลาส GatoStandalone\GatoAITranslationsForPolylang\Gato มี static methods สำหรับเรียกใช้การแปล:
| เมธอด | คำอธิบาย |
|---|---|
translate | ชื่อแทนของ translateCustomPosts |
translateCustomPosts | แปล custom posts (หน้า, โพสต์, custom post types) |
translateTaxonomyTerms | แปล taxonomy terms (หมวดหมู่, แท็ก, custom taxonomies) |
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;
}บริบทการเรียกใช้งาน
เรียกใช้เมธอดใดก็ได้หลังจากที่ปลั๊กอินได้รับการเริ่มต้นแล้วเท่านั้น ซึ่งจะเกิดขึ้นบน hooks ที่แตกต่างกันขึ้นอยู่กับบริบท:
| บริบท | Hook |
|---|---|
| Frontend | 'wp' action hook |
| Admin | 'wp_loaded' action hook |
| REST API | 'rest_jsonp_enabled' filter hook |
ตัวอย่างการเรียกใช้งาน
การเรียกใช้การแปลจากภายใน WordPress hooks แต่ละตัว:
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 ทำงานอยู่
- คุณมีใบอนุญาตที่ถูกต้องสำหรับปลั๊กอิน
- ปลั๊กอินได้รับการเริ่มต้นแล้ว (กล่าวคือ hooks ข้างต้นได้รับการเรียกใช้แล้ว)
...คุณสามารถเรียกใช้การแปลโดยไม่ต้องตรวจสอบ exceptions:
Gato::translate(123); // Same as `translateCustomPosts`
Gato::translateCustomPosts(123);
Gato::translateTaxonomyTerms([456, 789]);
Gato::translateMedia([101, 102]);