在单个页面上更改语言的 ISO 代码

On my forum we have created a section for Chinese Posts. Everything is working as desired, however the ISO code for the pages still shows English as that is the site setting. Does anyone know if there is a way we can set the category or individual pages to show that they are a different language?

Interesting, what are your thoughts on this @sam

I have not been able to see a way to change this in the existing software. Anyone have an idea if this could be built? I think it could help with traffic on our site as well as many others that have additional languages on one instance. Any direction would be helpful!
-Robert

Not that I’m an expert or anything, but shouldn’t that be possible with JavaScript or something similar?

I agree that it should be possible. I am trying to find a solution or suggestion on how I might implement this as we have multiple languages and I am not sure how I change the language code when someone posts in the Chinese category, however leave it as english when in the other categories. I also want to make sure this is not going to cause any issues elsewhere if we try to implement it. I know there are other forums that run multiple languages, however it looks like all the ones that run more than one language all show an “en” source code when you look at non english pages. Only the forums that are based in another language change. From my limited understanding this will mean that information is harder to find on google and if an english speaking person finds my chinese content google will not offer to translate as it shows english.
-Robert

Unfortunately this isn’t a built-in feature of Discourse. Theme components (javascript) wouldn’t work either, because the page language is set way before any javascript is loaded.

Having separate languages per-category would technically be possible using a plugin. This would work for crawlers like google, and would also trigger browser auto-translation features.

One issue with this is that Discourse is a ‘single page application’, and browsers only seem to check the lang attribute on the initial page load. That means that a user browsing the forum will be ‘stuck’ on the language of the first page they viewed. That may or may not be an issue depending on your use case.

I think the SEO and initial translation benefits of seeing it on a search would outweigh any issues I am seeing once on the forum. I am certainly interested in speaking with someone on what it might take to get this made and what it would look like currently and what might be needed if in the future we add more languages. Would the plugin be set per category or per post etc.
-Robert

Having done some further experimentation, this might not be completely true. Googlebot does render simple javascript when crawling the web, so we should be able to use javascript to modify the language.

To change the language for topics in a specific category (and its sub-categories), you can add something like this to the </head> section of a theme:

<script>
	if(document.querySelector("#breadcrumb-0 a[href='/c/chinese']")){
		document.documentElement.lang = "zh"
    }
</script>

I can’t find any specific documentation from google about whether this is acceptable, but I can see that it works when I “fetch as googlebot”:

@Robert_Fay let us know if you would like a hand adding this to your site.

I’m a bit confused, in looking at the code, there is no #breadcrumb-0 on our page.
image

image

I had tried using

<script>
if(document.querySelector("a.bullet[href='/c/chinese']")){
	document.documentElement.lang = "zh"
}
</script>

but that didn’t seem to do the trick. I have a feeling those elements are loaded later, because the code works in in the browser console, but not in a component /head. Any pointers?

This code is specifically for the crawler view of the site which googlebot sees. It has a different html structure to the regular view.

If you change your browser’s user agent to googlebot then you will see it take effect.

David,

I am still not seeing the change even as google bot. Could the issue be that my category name is actually 中文 技术论坛 and we use chinese as a category slug? Here is the page

 https://forum.digikey.com/c/chinese

中文 技术论坛 - Engineering and Component Solution Forum - TechForum │ Digi-Key
Thank you
-Robert

Ah, I should have specified that the code needs to go in the </body> section of the theme. Alternatively, you can leave it in the </head>, and wrap it like this:

<script>
    document.addEventListener('DOMContentLoaded', function(){
    	if(document.querySelector("#breadcrumb-0 a[href='/c/chinese']")){
    		document.documentElement.lang = "zh";
    	}
    });
</script>

If you would also like the <html lang= to be updated for non-googlebot views, you can add this. Google chrome doesn’t seem to detect the ‘live’ change in language, but other browsers might:

<script type="text/discourse-plugin" version="0.8">
    api.onPageChange(()=>{
        const body = document.documentElement.querySelector("body");
        if(body.classList.contains("category-chinese")){
            document.documentElement.lang = "zh";
        }else{
            document.documentElement.lang = I18n.locale;
        }
    });
</script>

This is in and looks like it is working in the googlebot views.
Thank You for all the help!
-Robert

你好 @david

在进行最近的审计时,我注意到用于中文、西班牙语和希伯来语类别的这段代码仅间歇性生效,而且目前仅在页面上生效时,我们的控制台会出现错误。未出现错误的页面现在在搜索控制台中显示为英文页面。有什么想法吗?

以下是错误信息。

正在使用的中文类别代码。

<script>
    document.addEventListener('DOMContentLoaded', function(){
    	if(document.querySelector("#breadcrumb-0 a[href='/c/chinese']")){
    		document.documentElement.lang = "zh";
    	}
    });
</script>

简要说明:当我对存在错误的页面进行实时测试时,错误会消失,但页面显示语言会从中文或西班牙语切换为英语。

  • Robert

data-vocabulary.org 警告应在 Discourse 的最新版本中通过以下提交解决:

您的站点已包含此更新,因此我们只需等待 Googlebot 跟进。正如您发现的,进行一次实时测试即可解决该警告。

不幸的是,面包屑导航的重构破坏了我们之前编写的小型语言脚本。面包屑不再包含 #breadcrumb-0 的 ID,因此我们需要采用其他方式。我认为以下代码应该可以解决问题:

<script>
    document.addEventListener('DOMContentLoaded', function(){
    	if(document.querySelector("#breadcrumbs a[href*='/c/chinese']")){
    		document.documentElement.lang = "zh";
    	}
    });
</script>

请通过实时测试告诉我这是否解决了问题。

这似乎确实解决了中文分类下项目的问题,但子分类仍然无法正常工作。例如,我是否需要为 /c/chinese/FAQ 单独设置一个?

-Robert

嗯,它也应该适用于子分类。以 Googlebot 身份获取 此主题 时,我得到了 lang=zh 的元数据

有没有可能 Google 缓存了你尝试访问的主题?另外请注意,此脚本仅适用于主题页面,而不适用于分类页面本身。

我不确定第一次测试中发生了什么。我又做了几个子分类页面,它们现在都能正常运行了。

再次感谢您的时间。
-罗伯特

你好 @david

我和我的 SEO 团队在浏览我们的网站时,发现这些链接似乎已无法正常工作。

以下是我们德语分类中的一个示例帖子:

当我在 Google Search Console 中查看该页面的抓取结果时,显示的是英文。

这是我目前使用的代码:

<script>
    document.addEventListener('DOMContentLoaded', function(){
        if(document.querySelector("#breadcrumbs a[href*='/c/german']")){
            document.documentElement.lang = "de";
        }
    });
</script>

如有任何帮助或建议,我们将不胜感激。

  • Robert