将无法多语化的部分多语化 [Bogo 插件]

An aerial view of a city at night

Bogo 是一个用于创建多语言网站的实用插件。不过,在一些领域,即使是 Bogo 也无法使用多种语言。

该部分已作修改,以显示多种语言。该部分的内容说明如下。

需要注意的是,此修复不能在 functions.php 中进行,而是要通过修改 WordPress 本身和 Yoast SEO 插件的源文件来实现。如果您想进行修复,我建议您在修复前备份整个 WordPress。

页面语言切换

在 Bogo 未使用多种语言的地区

大部分内容已通过 Bogo 插件实现多语言化。但是,日语原文的某些部分仍然存在。这次,我将把这些部分变成多语种。

具体来说

  • 网站口号(网站口号)区块
  • 首页 <head> 中的 <title> 标签
  • 首页 <head> 中的结构化数据(schema.org)

日文显示屏中以日文显示的部分将使用多种语言,包括

网站标语(Site Tagline)区块

我本以为它会被管理菜单中 “语言 (Languages)”和 “文本翻译(Terms Translation)”部分的一个项目所取代,但网站标语块中的文本即使在页面的语言发生变化时也不会被自动替换。但是,即使页面的语言发生变化,网站标语 (Site Tagline) 块中的文本也不会自动替换。

作为权宜之计,我们没有使用网站的口头禅区块,而是使用 Block Visibility 插件来显示正常文本中输入的文字,并根据语言进行调整。

在这种情况下,我能够调查问题的原因,并通过修改 WordPress 本身的源文件来解决。

Diff
--- site-tagline.php.orig       2025-02-15 20:55:32.476074037 +0900
+++ site-tagline.php    2025-02-15 15:43:41.950994484 +0900
@@ -15,7 +15,12 @@
  * @return string The render.
  */
 function render_block_core_site_tagline( $attributes ) {
-       $site_tagline = get_bloginfo( 'description' );
+       /* 20250215 get_bloginfo -> bloginfo From */
+       ob_start();
+       bloginfo('description');
+       $site_tagline = ob_get_clean();
+       /* 20250215 get_bloginfo -> bloginfo End */
+       //$site_tagline = get_bloginfo( 'description' );
        if ( ! $site_tagline ) {
                return;
        }
wp-includes/blocks/site-tagline.php

不更换网站标语的原因见以下几页。(Japanese site)

它利用了这一点,注意到 bloginfo 函数处理的是翻译后的值,而 get_bloginfo 函数处理的是翻译前的值。

如果使用 get_bloginfo 函数加载网站标语,返回的将是原始文本(此处为日文),而不是多语言文本。因此,网站标语块的显示并没有被多语言版本取代。

上述补丁用 bloginfo(‘description’)替换了 get_bloginfo (‘description’)读取的 site-tagline.php 部分。由于无法按原样替换,因此使用 ob_start() 和 ob_get_clean() 来接收字符串。

打上这个 “补丁 “后,网站的口头禅区块就变成了多语言区块。

请注意,本博客的网站名称在两种语言中都是英文的,因此在这种情况下没有解决这个问题。应该可以用类似的方法解决这个问题。

<title> 标记和结构化数据故障

一旦口头禅区块得到解决,它在视觉上就是多语言的。

不过,在 Chrome 浏览器开发工具中查看 HTML 时,仍有一些地方出现了日语。

一个是 <title> 标签。所有语言都显示日语。

HTML
<title>Clairvoyance Power - ~千里の道も一歩から~</title>
<title> tag

另一种是结构化数据。JSON 文件中的部分数据是日文。

JSON
"@graph": [
    {
        "@type": "CollectionPage",
        "@id": "https://clairvoyance-power.com/",
        "url": "https://clairvoyance-power.com/",
        "name": "Clairvoyance Power - ~千里の道も一歩から~",
        "isPartOf": {
            "@id": "https://clairvoyance-power.com/#website"
        },
        "about": {
            "@id": "https://clairvoyance-power.com/#/schema/person/fb4fd70540149e2fdea9e6ae8eabab1e"
        },
        "breadcrumb": {
            "@id": "https://clairvoyance-power.com/#breadcrumb"
        },
        "inLanguage": "zh-Hans"
    },
<script type=”application/ld+json” class=”yoast-schema-graph”>
JSON
{
    "@type": "WebSite",
    "@id": "https://clairvoyance-power.com/#website",
    "url": "https://clairvoyance-power.com/",
    "name": "Clairvoyance Power",
    "description": "~千里の道も一歩から~",
    ...snip...
    "inLanguage": "zh-Hans"
},
<script type=”application/ld+json” class=”yoast-schema-graph”>

第 38 行显示语言为简体中文。本来,我希望显示中文网站的口号,但却显示了日文。

处理 <title> 标记和结构化数据

这些部分由 Yoast SEO 插件输出。上述页面介绍了如何在 Yoast SEO 插件中处理这个问题,但在该内容中,结构化数据部分没有被多语言部分取代。

这一次,Yoast SEO 插件的源文件做了相应修改:有三处修改为使用 bloginfo() 而不是 get_bloginfo()。

Diff
--- class-wpseo-replace-vars.php.orig   2025-02-15 20:41:57.707080317 +0900
+++ class-wpseo-replace-vars.php        2025-02-15 20:27:59.292149230 +0900
@@ -495,8 +495,13 @@
                static $replacement;

                if ( ! isset( $replacement ) ) {
+                       /* 20250215 get_bloginfo -> bloginfo */
+                       ob_start();
+                       bloginfo('description');
+                       $desc = ob_get_clean();
                        $description = wp_strip_all_tags( $desc );
-                       $description = wp_strip_all_tags( get_bloginfo( 'description' ) );
+                       //$description = wp_strip_all_tags( get_bloginfo( 'description' ) );
+                       /* 20250215 get_bloginfo -> bloginfo */
                        if ( $description !== '' ) {
                                $replacement = $description;
                        }
wp-content/plugins/wordpress-seo/inc/class-wpseo-replace-vars.php
Diff
--- website.php.orig    2025-02-15 20:47:52.567204942 +0900
+++ website.php 2025-02-15 20:26:47.784325248 +0900
@@ -24,13 +24,19 @@
         * @return array Website data blob.
         */
        public function generate() {
+               /* 20250215 get_bloginfo -> bloginfo */
+               ob_start();
+               bloginfo('description');
+               $desc = ob_get_clean();
                $data = [
                        '@type'       => 'WebSite',
                        '@id'         => $this->context->site_url . Schema_IDs::WEBSITE_HASH,
                        'url'         => $this->context->site_url,
                        'name'        => $this->helpers->schema->html->smart_strip_tags( $this->context->site_name ),
-                       'description' => \get_bloginfo( 'description' ),
+                       'description' => $desc,
+               //      'description' => \get_bloginfo( 'description' ),
                ];
+               /* 20250215 get_bloginfo -> bloginfo */

                if ( $this->context->site_represents_reference ) {
                        $data['publisher'] = $this->context->site_represents_reference;
wp-content/plugins/wordpress-seo/src/generators/schema/website.php
Diff
--- rss-footer-embed.php.orig   2025-02-15 20:52:54.467462231 +0900
+++ rss-footer-embed.php        2025-02-15 20:16:14.979891023 +0900
@@ -165,11 +165,17 @@
                        $author_link = \sprintf( $link_template, \esc_url( \get_author_posts_url( $post->post_author ) ), \esc_html( \get_the_author() ) );
                }

+               /* 20250215 get_bloginfo -> bloginfo */
+               ob_start();
+               bloginfo('description');
+               $desc = ob_get_clean();
+
                return [
                        '%%AUTHORLINK%%'   => $author_link,
                        '%%POSTLINK%%'     => \sprintf( $link_template, \esc_url( \get_permalink() ), \esc_html( \get_the_title() ) ),
                        '%%BLOGLINK%%'     => \sprintf( $link_template, \esc_url( \get_bloginfo( 'url' ) ), \esc_html( \get_bloginfo( 'name' ) ) ),
-                       '%%BLOGDESCLINK%%' => \sprintf( $link_template, \esc_url( \get_bloginfo( 'url' ) ), \esc_html( \get_bloginfo( 'name' ) ) . ' - ' . \esc_html( \get_bloginfo( 'description' ) ) ),
+               //      '%%BLOGDESCLINK%%' => \sprintf( $link_template, \esc_url( \get_bloginfo( 'url' ) ), \esc_html( \get_bloginfo( 'name' ) ) . ' - ' . \esc_html( \get_bloginfo( 'description' ) ) ),
+                       '%%BLOGDESCLINK%%' => \sprintf( $link_template, \esc_url( \get_bloginfo( 'url' ) ), \esc_html( \get_bloginfo( 'name' ) ) . ' - ' . \esc_html( $desc ) ),
                ];
        }
wp-content/plugins/wordpress-seo/src/integrations/front-end/rss-footer-embed.php

<title> 标记和结构化数据应对结果

通过修改 Yoast SEO 源文件,<title> 标签和结构化数据部分已实现多语言化。

HTML
<title>Clairvoyance Power - 千里之行,始于足下 -</title>
u003ctitleu003e
JSON
"@graph": [
    {
        "@type": "CollectionPage",
        "@id": "https://clairvoyance-power.com/",
        "url": "https://clairvoyance-power.com/",
        "name": "Clairvoyance Power - 千里之行,始于足下 -",
        "isPartOf": {
            "@id": "https://clairvoyance-power.com/#website"
        },
        "about": {
            "@id": "https://clairvoyance-power.com/#/schema/person/fb4fd70540149e2fdea9e6ae8eabab1e"
        },
        "breadcrumb": {
            "@id": "https://clairvoyance-power.com/#breadcrumb"
        },
        "inLanguage": "zh-Hans"
    },
    {
        "@type": "BreadcrumbList",
        "@id": "https://clairvoyance-power.com/#breadcrumb",
        "itemListElement": [
            {
                "@type": "ListItem",
                "position": 1,
                "name": "Home"
            }
        ]
    },
u003cscript type=u0022application/ld+jsonu0022 class=u0022yoast-schema-graphu0022u003e
JSON
{
    "@type": "WebSite",
    "@id": "https://clairvoyance-power.com/#website",
    "url": "https://clairvoyance-power.com/",
    "name": "Clairvoyance Power",
    "description": "- 千里之行,始于足下 -",
    ...snip...
    "inLanguage": "zh-Hans"
},
u003cscript type=u0022application/ld+jsonu0022 class=u0022yoast-schema-graphu0022u003e

在简体中文中,会显示该语言的网站口号。

Musubi

在本文中,我对 Bogo 插件中没有多语种化的部分进行了多语种化。

  • 网站口号(网站口号)区块
  • 首页 <head> 中的 <title> 标签
  • 首页 <head> 中的结构化数据(schema.org)

单个帖子页面已多语种化,因此无需采取任何行动。

如果网站使用多种语言,网站名称也不会反映出来。处理方法与本例相同。

它解决了我在建立这个网站并努力使其成为多语言网站时遇到的一个问题。不过,对源文件的修改将在更新时还原。

今后,我将寻找其他方法来解决这个问题。

Comments

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注