fix: title translate and description translate

This commit is contained in:
hanyixuanten
2026-07-26 00:39:49 +08:00
parent a4f43bce2f
commit cd49b8c563
4 changed files with 47 additions and 2 deletions
+2 -1
View File
@@ -30,8 +30,9 @@ Changing source text produces a new fingerprint and therefore schedules a new tr
- Assigned categories and tags, including their descriptions.
- Yoast SEO title and description fields.
- Rank Math title and description fields.
- All in One SEO and SEOPress title and description output filters.
Only the listed SEO meta fields are sent for translation. Arbitrary metadata, serialized data, credentials, and code are excluded.
Only the listed SEO meta fields and the generated title/description output from supported SEO plugins are sent for translation. Browser titles generated through the WordPress document-title API use the persisted translated post title. Arbitrary metadata, serialized data, credentials, and code are excluded.
When post content contains HTML links, the plugin preserves each link's `href` and attributes, translates its visible text as a separate value, and inserts that translated text back into the original link. Re-run a post translation after upgrading to regenerate previously translated content that contains links.
+20
View File
@@ -24,10 +24,15 @@ class WPT_Content_Controller {
add_filter( 'wp_get_attachment_image_attributes', array( $this, 'translate_image_alt' ), 20, 2 );
add_filter( 'single_term_title', array( $this, 'translate_term_title' ), 20, 2 );
add_filter( 'term_description', array( $this, 'translate_term_description' ), 20, 3 );
add_filter( 'document_title_parts', array( $this, 'translate_document_title_parts' ), 20 );
add_filter( 'wpseo_title', array( $this, 'translate_seo_title' ), 20 );
add_filter( 'wpseo_metadesc', array( $this, 'translate_seo_description' ), 20 );
add_filter( 'rank_math/frontend/title', array( $this, 'translate_seo_title' ), 20 );
add_filter( 'rank_math/frontend/description', array( $this, 'translate_seo_description' ), 20 );
add_filter( 'aioseo_title', array( $this, 'translate_seo_title' ), 20 );
add_filter( 'aioseo_description', array( $this, 'translate_seo_description' ), 20 );
add_filter( 'seopress_titles_the_title', array( $this, 'translate_seo_title' ), 20 );
add_filter( 'seopress_titles_the_description', array( $this, 'translate_seo_description' ), 20 );
add_filter( 'post_link', array( $this, 'localize_permalink' ), 20 );
add_filter( 'page_link', array( $this, 'localize_permalink' ), 20 );
add_filter( 'post_type_link', array( $this, 'localize_permalink' ), 20 );
@@ -202,6 +207,21 @@ class WPT_Content_Controller {
return $this->translated_value( $description, 'term:' . $term_id . ':description' );
}
public function translate_document_title_parts( $parts ) {
if ( ! is_singular() || empty( $parts['title'] ) ) {
return $parts;
}
$post_id = get_queried_object_id();
if ( ! $post_id ) {
return $parts;
}
$parts['title'] = $this->translated_value( $parts['title'], 'post:' . $post_id . ':post_title' );
return $parts;
}
public function translate_term_object( $term, $taxonomy, $context = '' ) {
if ( ! $term instanceof WP_Term || ! in_array( $term->taxonomy, array( 'category', 'post_tag' ), true ) ) {
return $term;
+18
View File
@@ -0,0 +1,18 @@
<?php
use PHPUnit\Framework\TestCase;
class WPT_Test_Language_Router extends WPT_Language_Router {
public function current_language() {
return 'en';
}
}
class DocumentTitleTest extends TestCase {
public function test_document_title_callback_leaves_non_singular_title_parts_unchanged() {
$controller = new WPT_Content_Controller( new WPT_Test_Translation_Store(), new WPT_Test_Language_Router() );
$parts = array( 'title' => '中文主页', 'site' => '示例站点' );
$this->assertSame( $parts, $controller->translate_document_title_parts( $parts ) );
}
}
+6
View File
@@ -2,6 +2,10 @@
define( 'ABSPATH', __DIR__ . '/' );
function is_singular() {
return false;
}
require_once dirname( __DIR__ ) . '/includes/interface-wpt-translation-provider.php';
require_once dirname( __DIR__ ) . '/includes/class-wpt-translation-result.php';
require_once dirname( __DIR__ ) . '/includes/class-wpt-translation-identity.php';
@@ -9,3 +13,5 @@ require_once dirname( __DIR__ ) . '/includes/class-wpt-translation-store.php';
require_once dirname( __DIR__ ) . '/includes/class-wpt-translation-service.php';
require_once dirname( __DIR__ ) . '/includes/class-wpt-content-translator.php';
require_once dirname( __DIR__ ) . '/includes/class-wpt-settings.php';
require_once dirname( __DIR__ ) . '/includes/class-wpt-language-router.php';
require_once dirname( __DIR__ ) . '/includes/class-wpt-content-controller.php';