[WordPress] 外掛分享: Yard DeepL

首頁外掛目錄 › Yard DeepL
10+
安裝啟用
尚無評分
14 天前
最後更新
問題解決
WordPress 6.0+ PHP 8.0+ v2.0.0 上架:2025-01-06

內容簡介

總結:
此外掛註冊安全的API端點,允許您直接從DeepL請求翻譯,而不需公開DeepL API金鑰。只有在提供有效nonce時才能訪問這些端點。在向網站訪問者提供翻譯時,您可以配置支持翻譯的語言。

- 如何確保翻譯效率和節省成本?
每個經翻譯的物件將其快取翻譯存儲在數據庫中的wp_postmeta表中。這種快取機制確保翻譯被有效重複使用,減少不必要的API請求,節省成本。
- 如果快取翻譯新於物件的post_modified日期,則提供快取版本。
- 當物件的post_modified日期比快取翻譯更新時,將從DeepL抓取新的翻譯。一旦檢索到,此翻譯將立即被快取以供未來使用。
這種方法最大程度地減少對DeepL的API調用次數,確保必要時翻譯保持最新。

- 這個外掛連接到 DeepL API 提供翻譯服務:
- 服務:DeepL API (https://www.deepl.com)
- 目的:根據提供的目標語言代碼,將一種語言的文字翻譯為另一種語言。
- 發送的數據:翻譯的文字內容、目標語言代碼以及DeepL API金鑰(安全處理,不會公開給用戶)。
- 條件:當發起翻譯請求時會發送數據。
- 隱私政策:DeepL隱私政策
- 服務條款:DeepL服務條款

- 使用
- 確保安全性:
- 這個插件註冊的API端點使用WordPress nonce進行保護。nonce通過wp_localize_script函數傳遞到前端,並存儲在全局JavaScript對象ydpl中,包含以下屬性:
- ydpl_translate_post_id:待翻譯的文章ID。
- ydpl_rest_translate_url:翻譯請求的API端點URL。
- ydpl_supported_languages:支持翻譯的語言列表。
- ydpl_api_request_nonce:用於API驗證的nonce。
- 在發送API請求時,確保nonce包含在請求標頭中。標頭應命名為nonce,並包含ydpl_api_request_nonce的值。

外掛標籤

開發者團隊

⬇ 下載最新版 (v2.0.0) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「Yard DeepL」→ 直接安裝(推薦)
📦 歷史版本下載

原文外掛簡介

This plugin registers secure API endpoints that allow you to request translations directly from DeepL without exposing your Deepl API-key. These endpoints are only accessible when a valid nonce is provided. When providing translations to website visitors, you can configure which languages are supported for translation.
Caching Mechanism
Each object that is translated will store its cached translation in the wp_postmeta table within the database. This caching mechanism ensures that translations are efficiently reused, reducing unnecessary API requests to DeepL and saving costs.

Serving Cached Translations: If a cached translation is newer than the post_modified date of the object, the cached version is served.
Fetching New Translations: When the post_modified date of the object is more recent than the cached translation, a new translation is fetched from DeepL. Once retrieved, this translation is immediately cached for future use.
Cache Authorization: Only logged-in users with the edit_posts capability (or a custom capability configured via the yard::deepl/cache_capability filter) are permitted to create new cache entries. Requests from users without this capability will still return a translation from DeepL, but the result will not be stored in the cache.

This approach minimizes the number of API calls to DeepL, ensuring translations are kept up to date only when necessary.
Admin: Cache Metabox
A metabox labeled Yard DeepL is displayed on the edit screen of supported post types (default: page). It provides two options:

Disable translation cache: When checked, the cache is bypassed for this object. Useful for posts with dynamic content that should always be translated fresh.
Clear translation cache: When checked and the post is saved, all cached translations for this object are deleted.

Admin: Translation Cache Column
A Translation cache column is added to the post list screen of all supported post types. Its purpose is to notify editors which posts should be cached as soon as possible to avoid unnecessary DeepL API costs.

Green badge per language (e.g. NL, EN-US): a valid, up-to-date cached translation exists for that language. Visitor requests are served from cache at no cost.
Grey badge with a count per language (e.g. EN-US 42): no cache exists for that language and visitors have triggered that many live DeepL API calls. Hovering the badge shows the full count as a tooltip. These posts are the most urgent to cache.
—: the post has never been requested for translation, or caching is disabled for this post.

The uncached call count is only incremented for visitor requests (users without cache-write capability) and is automatically reset per language once a cache entry is stored. Posts with the Disable translation cache option enabled are excluded from the column entirely.
External Services
This plugin connects to the DeepL API to provide translations for content.

Service: DeepL API (https://www.deepl.com)
Purpose: To translate text from one language to another based on the provided target language.
Data Sent: Text content for translation, the target language code, and the DeepL API key (handled securely and never exposed to users).
Conditions: Data is sent when a request for translation is initiated.
Privacy Policy: DeepL Privacy Policy
Terms of Service: DeepL Terms of Service

Usage
Security
The API endpoints registered by this plugin are secured using a WordPress nonce. The nonce is passed to the front-end using the wp_localize_script function and is stored in a global JavaScript object ydpl which contains the following properties:

ydpl_translate_post_id: The ID of the post to be translated.
ydpl_rest_translate_url: The URL of the API endpoint for translation requests.
ydpl_supported_languages: The list of languages supported for translation.
ydpl_api_request_nonce: The nonce used for API validation.

When making requests to the API, ensure that the nonce is included in the request headers. The header should be named X-WP-Nonce, and it should contain a nonce created with the wp_rest action (available via ydpl.ydpl_api_request_nonce on the front-end).
Rate limiting: Unauthenticated requests (or requests from users without the cache capability) are limited to 3 requests per 60 seconds per IP address. Authenticated users with the required cache capability (default: edit_posts) are exempt from this rate limit.
Cache capability: Only users with the edit_posts capability can trigger cache creation. This can be customized using the yard::deepl/cache_capability filter.
Example
Request:
var xhr = new XMLHttpRequest();
xhr.open('POST', ydpl.ydpl_rest_translate_url, true);

// Set request headers
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('X-WP-Nonce', ydpl.ydpl_api_request_nonce);

// Handle response
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log('Translation:', JSON.parse(xhr.responseText));
} else if (xhr.readyState === 4) {
console.error('Error:', xhr.statusText);
}
};

// Prepare and send the request body
var data = JSON.stringify({
text: ["Look another test"],
target_lang: "DE"
});

xhr.send(data);

Response:
[
{
"text": "Look another test!",
"translation": "Sehen Sie sich einen weiteren Test an!"
}
]

Filters

* yard::deepl/cache_capability (default: edit_posts) — The WordPress capability required to create cache entries. Users without this capability receive translations but results are not cached.
* yard::deepl/cache_metabox_post_types (default: ['page']) — Post types on which the Yard DeepL cache metabox is displayed.
* yard::deepl/disable_cache_metabox_post_types — Deprecated. Use yard::deepl/cache_metabox_post_types instead.

延伸相關外掛

文章
Filter
Mastodon