# Change ISO code for language on single page

**URL:** https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736
**Category:** Support
**Created:** [January 28, 2019, 4:34pm UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736 "2019-01-28T16:34:16Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Robert\_Fay](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/robert_fay/32/86825_2.png) [@Robert\_Fay](https://meta.discourse.org/u/Robert_Fay)
#### Post date: [January 28, 2019, 4:34pm UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/1 "2019-01-28T16:34:16Z")

</div>

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?

 ![image](https://global.discourse-cdn.com/meta/original/3X/8/b/8ba412733d0d3fbb6f6c8325414ee4e23494bfcc.png)

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [January 29, 2019, 6:11am UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/2 "2019-01-29T06:11:34Z")

</div>

Interesting, what are your thoughts on this @sam

---

<div class="post-metadata">

### Author: ![Robert\_Fay](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/robert_fay/32/86825_2.png) [@Robert\_Fay](https://meta.discourse.org/u/Robert_Fay)
#### Post date: [February 6, 2019, 5:02pm UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/3 "2019-02-06T17:02:08Z")

</div>

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

---

<div class="post-metadata">

### Author: ![nicrats](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nicrats/32/130674_2.png) [@nicrats](https://meta.discourse.org/u/nicrats)
#### Post date: [February 6, 2019, 5:22pm UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/4 "2019-02-06T17:22:41Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Robert\_Fay](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/robert_fay/32/86825_2.png) [@Robert\_Fay](https://meta.discourse.org/u/Robert_Fay)
#### Post date: [February 7, 2019, 2:13pm UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/5 "2019-02-07T14:13:52Z")

</div>

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

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [February 12, 2019, 11:43am UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/6 "2019-02-12T11:43:08Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Robert\_Fay](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/robert_fay/32/86825_2.png) [@Robert\_Fay](https://meta.discourse.org/u/Robert_Fay)
#### Post date: [February 12, 2019, 7:49pm UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/7 "2019-02-12T19:49:00Z")

</div>

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

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [February 12, 2019, 9:53pm UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/12 "2019-02-12T21:53:48Z")

</div>

> [@david](#):
>
> Theme components (javascript) wouldn’t work either, because the page language is set way before any javascript is loaded.

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:

```plaintext
<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”:

 ![11](https://global.discourse-cdn.com/meta/original/3X/e/5/e5d6af2131946c281ac89d245e214c9b5e33042c.png)

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

---

<div class="post-metadata">

### Author: ![bombledmonk](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bombledmonk/32/80322_2.png) [@bombledmonk](https://meta.discourse.org/u/bombledmonk)
#### Post date: [February 12, 2019, 11:00pm UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/13 "2019-02-12T23:00:51Z")

</div>

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

 ![image](https://global.discourse-cdn.com/meta/original/3X/6/7/67c37363e5078e4f95725a9e99a7465f61761079.png)

![image](https://global.discourse-cdn.com/meta/original/3X/9/5/953c72bc47d3eccaea4860619015e7dcdb8f1187.png)

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?

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [February 12, 2019, 11:23pm UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/14 "2019-02-12T23:23:12Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Robert\_Fay](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/robert_fay/32/86825_2.png) [@Robert\_Fay](https://meta.discourse.org/u/Robert_Fay)
#### Post date: [February 13, 2019, 6:19pm UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/15 "2019-02-13T18:19:48Z")

</div>

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

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

[中文論壇 - DigiKey TechForum - An Electronic Component and Engineering Solution Forum](https://forum.digikey.com/c/chinese)  
Thank you  
-Robert

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [February 13, 2019, 7:33pm UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/16 "2019-02-13T19:33:40Z")

</div>

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:

```plaintext
<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:

```plaintext
<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>

```

---

<div class="post-metadata">

### Author: ![Robert\_Fay](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/robert_fay/32/86825_2.png) [@Robert\_Fay](https://meta.discourse.org/u/Robert_Fay)
#### Post date: [February 13, 2019, 8:37pm UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/17 "2019-02-13T20:37:09Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Robert\_Fay](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/robert_fay/32/86825_2.png) [@Robert\_Fay](https://meta.discourse.org/u/Robert_Fay)
#### Post date: [March 3, 2020, 4:34pm UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/18 "2020-03-03T16:34:07Z")

</div>

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.

 ![image](https://global.discourse-cdn.com/meta/original/3X/9/1/91290d9ee9aac51e91906b1a6d1897f9c3ca3daf.png)

Chinese Category code being used.

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

---

<div class="post-metadata">

### Author: ![Robert\_Fay](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/robert_fay/32/86825_2.png) [@Robert\_Fay](https://meta.discourse.org/u/Robert_Fay)
#### Post date: [March 3, 2020, 4:46pm UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/19 "2020-03-03T16:46:45Z")

</div>

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

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [March 3, 2020, 4:53pm UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/20 "2020-03-03T16:53:46Z")

</div>

The [data-vocabulary.org](http://data-vocabulary.org) warning should be resolved in the latest version of discourse per

[https://github.com/discourse/discourse/commit/96b64df4d46627fbcaf3c40612750405ca058333](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:

```plaintext
<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

---

<div class="post-metadata">

### Author: ![Robert\_Fay](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/robert_fay/32/86825_2.png) [@Robert\_Fay](https://meta.discourse.org/u/Robert_Fay)
#### Post date: [March 3, 2020, 5:01pm UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/21 "2020-03-03T17:01:20Z")

</div>

> [@david](#):
>

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

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [March 3, 2020, 5:08pm UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/22 "2020-03-03T17:08:31Z")

</div>

Hmm, it should work for subcategories as well. Fetching [this topic](https://forum.digikey.com/t/topic/5732) as googlebot gives me the `lang=zh` metadata

 ![Screenshot 2020-03-03 at 17.07.28](https://global.discourse-cdn.com/meta/original/3X/1/3/1312ecc448d4fa556363155a6c73146fa31f9adb.png)

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.

---

<div class="post-metadata">

### Author: ![Robert\_Fay](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/robert_fay/32/86825_2.png) [@Robert\_Fay](https://meta.discourse.org/u/Robert_Fay)
#### Post date: [March 3, 2020, 5:16pm UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/23 "2020-03-03T17:16:47Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Robert\_Fay](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/robert_fay/32/86825_2.png) [@Robert\_Fay](https://meta.discourse.org/u/Robert_Fay)
#### Post date: [April 5, 2021, 3:36pm UTC](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736/24 "2021-04-05T15:36:32Z")

</div>

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.

> **[Alternative für das Bauteil FR05-S1-R-0-105](https://forum.digikey.com/t/alternative-fur-das-bauteil-fr05-s1-r-0-105/12177)**
>
> Alternative für das Bauteil FR05-S1-R-0-105 Eine direkte Alternative für das Bauteil FR05-S1-R-0-105 / 2115-FR05-S1-R-0-105CT-ND von Fractus Antennas ( Discontinued at Digi-Key ) kann aus derselben EZConnect™ Serie diese RF-Antenne sein: ...

When I look at the crawled page on the Google Search Console it is showing English.

 ![image](https://global.discourse-cdn.com/meta/original/3X/f/9/f979ba95daad058d1b5d808d3e317d9ba931b7ff.png)

Here is the code I am currently using.

```plaintext
<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

[Next page](https://meta.discourse.org/t/change-iso-code-for-language-on-single-page/107736.md?page=2)
