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.
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
Hello @david
While doing a recent audit I noticed that this bit of code for our Chinese, Spanish, and Hebrew categories is only intermittently working and the only pages that it is working on now have an error in our console. The pages that do not have the error are now showing up as English pages in the search council. Any Thoughts?
Here is the error.
Chinese Category code being used.
<script>
document.addEventListener('DOMContentLoaded', function(){
if(document.querySelector("#breadcrumb-0 a[href='/c/chinese']")){
document.documentElement.lang = "zh";
}
});
</script>
A quick note. When I do a live test on the pages with errors the errors go away, however they then switch from showing as a Chinese to Spanish Page to Showing as an English page.
-Robert
The data-vocabulary.org warning should be resolved in the latest version of discourse per
https://github.com/discourse/discourse/commit/96b64df4d46627fbcaf3c40612750405ca058333
Your site has this update, so we just need to wait for googlebot to catch up. As you found, doing a live test will resolve the warning.
Unfortunately this restructuring of breadcrumbs broke the little language script we came up with. The breadcrumbs no longer have the #breadcrumb-0
id, so we need something else. I think this should do it:
<script>
document.addEventListener('DOMContentLoaded', function(){
if(document.querySelector("#breadcrumbs a[href*='/c/chinese']")){
document.documentElement.lang = "zh";
}
});
</script>
Let me know if that resolves the issue with a live test
This did seem to solve the issue for items in the Chinese category, however the sub categories are not working. Would I need to have a separate one for my /c/chinese/FAQ for example?
-Robert
Hmm, it should work for subcategories as well. Fetching this topic as googlebot gives me the lang=zh
metadata
Is it possible that Google has cached the topic you tried? Also, remember this script only works on topics, not on the category pages themselves.
I am not sure what happened in the first test. I have done a couple more sub-category pages and they are working.
Thank you again for you time.
-Robert
Hello @david
I was going through our site with my SEO team and it appears that these may no longer be working.
Example Post in our German Category.
When I look at the crawled page on the Google Search Console it is showing English.
Here is the code I am currently using.
<script>
document.addEventListener('DOMContentLoaded', function(){
if(document.querySelector("#breadcrumbs a[href*='/c/german']")){
document.documentElement.lang = "de";
}
});
</script>
Any help or direction would be appreciated.
-Robert