# Switch language via URL?

**URL:** https://meta.discourse.org/t/switch-language-via-url/28937
**Category:** Support
**Created:** [May 18, 2015, 6:45am UTC](https://meta.discourse.org/t/switch-language-via-url/28937 "2015-05-18T06:45:51Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![hongquan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hongquan/32/115859_2.png) [@hongquan](https://meta.discourse.org/u/hongquan)
#### Post date: [May 18, 2015, 6:45am UTC](https://meta.discourse.org/t/switch-language-via-url/28937/1 "2015-05-18T06:45:51Z")

</div>

Hi

Does Discourse support live switching language via URL, like discourse.vn/?lang=vi ?

I’m asking this because our main website, www.easyuni.my allows user to select language in URL, like www.easyuni.my/id/ (for Indonesian). We will add Discourse as our forum, linking to it from the main site, and try to maintain the language preference via URL.

How can we achieve that?

---

<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: [May 18, 2015, 8:29am UTC](https://meta.discourse.org/t/switch-language-via-url/28937/2 "2015-05-18T08:29:01Z")

</div>

If you enable it in site settings, users can set the UI to any supported language in their user preferences.

---

<div class="post-metadata">

### Author: ![hongquan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hongquan/32/115859_2.png) [@hongquan](https://meta.discourse.org/u/hongquan)
#### Post date: [May 18, 2015, 8:33am UTC](https://meta.discourse.org/t/switch-language-via-url/28937/3 "2015-05-18T08:33:39Z")

</div>

Yes, but what I means is to switch on-the-fly via URL. The scenario is:

- If user visit my website www.easyuni.my/en/ (in English), our menu will point to discourse.easyuni.my/?lang=en.

- If user visit my website www.easyuni.my/id/ (in Indonesian), our menu will point to discourse.easyuni.my/?lang=id so that user still get his preferred language when he transfer from main website to forum.

---

<div class="post-metadata">

### Author: ![nahtnam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nahtnam/32/122073_2.png) [@nahtnam](https://meta.discourse.org/u/nahtnam)
#### Post date: [May 18, 2015, 2:58pm UTC](https://meta.discourse.org/t/switch-language-via-url/28937/4 "2015-05-18T14:58:12Z")

</div>

+1 for this feature. Discourse should also check the location of the user and switch to the native language automatically.

---

<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: [May 18, 2015, 9:38pm UTC](https://meta.discourse.org/t/switch-language-via-url/28937/5 "2015-05-18T21:38:51Z")

</div>

No, that breaks down when multiple languages are spoken in a country.

Auto switch via querystring might be possible, you would be forcing a pref on the user.

---

<div class="post-metadata">

### Author: ![meglio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/meglio/32/71444_2.png) [@meglio](https://meta.discourse.org/u/meglio)
#### Post date: [February 8, 2016, 1:56am UTC](https://meta.discourse.org/t/switch-language-via-url/28937/6 "2016-02-08T01:56:06Z")

</div>

I’m about to organize a Q/A Session between Ukrainian and English speaking goat farmers. It would be nice to give the link to the target topic with the correct language in URL, i.e. `?lang=en` for our English colleagues. So, +1 from me for this feature.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [February 8, 2016, 2:14am UTC](https://meta.discourse.org/t/switch-language-via-url/28937/7 "2016-02-08T02:14:13Z")

</div>

Will there be different content for each language? Ideally, how would you like this feature to work?

---

<div class="post-metadata">

### Author: ![meglio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/meglio/32/71444_2.png) [@meglio](https://meta.discourse.org/u/meglio)
#### Post date: [February 8, 2016, 2:54am UTC](https://meta.discourse.org/t/switch-language-via-url/28937/8 "2016-02-08T02:54:35Z")

</div>

We will translate questions and answers in the session topic, so that there are both Ukrainian and English versions of the same message. Will just separate them visually somehow.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [February 8, 2016, 3:04am UTC](https://meta.discourse.org/t/switch-language-via-url/28937/9 "2016-02-08T03:04:37Z")

</div>

If you put both the original and the translation in the same topic, maybe it would be possible to show/hide content based on the user’s locale. Something like this: [GitHub - wooorm/franc: Natural language detection · GitHub](https://github.com/wooorm/franc) could be used for language detection within the topic. Sections that are not in the user’s target language could be initially collapsed and then expanded for users who wanted to see the content in both languages.

edit:

Since you know the languages you are dealing with ahead of time you could just wrap the text for each language in a div so that Ukrainian sections would have `<div class="uk">` and English sections `<div class="en">` and then show/hide those sections based on the user’s locale.

---

<div class="post-metadata">

### Author: ![meglio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/meglio/32/71444_2.png) [@meglio](https://meta.discourse.org/u/meglio)
#### Post date: [February 8, 2016, 3:42am UTC](https://meta.discourse.org/t/switch-language-via-url/28937/10 "2016-02-08T03:42:43Z")

</div>

How would I make one section collapsed by default, but not the other? Also, which Discourse feature would I use to make sections?

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [February 8, 2016, 4:41am UTC](https://meta.discourse.org/t/switch-language-via-url/28937/11 "2016-02-08T04:41:27Z")

</div>

I’m just thinking about how to do it, It would have to be done in a plugin. Ultimately, a translation could be a type of reply. Creating it would wrap the content in a div with a class to indicate its language.

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

I’ll try out a few things and get back to you.

---

<div class="post-metadata">

### Author: ![meglio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/meglio/32/71444_2.png) [@meglio](https://meta.discourse.org/u/meglio)
#### Post date: [February 8, 2016, 7:14am UTC](https://meta.discourse.org/t/switch-language-via-url/28937/12 "2016-02-08T07:14:53Z")

</div>

There should be a plugin for Discourse for collapsable sections, if I remember correctly. It would be nice to make them auto-collapse or auto-expand based on the current locale. More generally, depending on any user field.

---

<div class="post-metadata">

### Author: ![jeanmonod](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jeanmonod/32/119952_2.png) [@jeanmonod](https://meta.discourse.org/u/jeanmonod)
#### Post date: [April 4, 2018, 1:53pm UTC](https://meta.discourse.org/t/switch-language-via-url/28937/13 "2018-04-04T13:53:57Z")

</div>

> Auto switch via querystring might be possible

I can’t find info/solution on the topic. I have the exact same requirement, need to redirect users on the forum but with a selected language. A query string approach can be ok, any other approach is welcome too.

@hongquan did you find a solution at the end?

---

<div class="post-metadata">

### Author: ![hongquan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hongquan/32/115859_2.png) [@hongquan](https://meta.discourse.org/u/hongquan)
#### Post date: [April 5, 2018, 4:59am UTC](https://meta.discourse.org/t/switch-language-via-url/28937/14 "2018-04-05T04:59:04Z")

</div>

> [@jeanmonod](#):
>
> @hongquan did you find a solution at the end?

Not yet. I have stopped working with Discourse.

---

<div class="post-metadata">

### Author: ![jeanmonod](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jeanmonod/32/119952_2.png) [@jeanmonod](https://meta.discourse.org/u/jeanmonod)
#### Post date: [April 10, 2018, 3:08pm UTC](https://meta.discourse.org/t/switch-language-via-url/28937/15 "2018-04-10T15:08:07Z")

</div>

I continue a bit my exploration. Seems that the language selection happens here:

> <https://github.com/discourse/discourse/blob/main/app/controllers/application_controller.rb#L254>

Currently, it’s pretty easy simple and straightforward. What if we complete that with a possibility to force the locale by query-string:

```
if params[:locale].present?
  locale = locale_from_locale_param
else if SiteSetting.set_locale_from_accept_language_header
  locale = locale_from_header
else
  locale = SiteSetting.default_locale
end

```

If will allow use case like requested by @hongquan on [Switch language via URL? - #3 by hongquan](https://meta.discourse.org/t/switch-language-via-url/28937/3)

This have no impact on existing instances and it’s not impacting already created user, it will only help user that have already select a language in the main website to continue browsing in the same language in the forum section…

Is this something I can provide as a PR or should I look at doing this in a plugin? (although I’m not sure it’s possible to do this in a plugin…)

cc @simon

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [April 10, 2018, 4:07pm UTC](https://meta.discourse.org/t/switch-language-via-url/28937/16 "2018-04-10T16:07:16Z")

</div>

Adding the ability to set the locale through a query string could be done through a plugin. The code for setting the locale through the Accept Language header was initially in a plugin: [https://github.com/scossar/variable-language](https://github.com/scossar/variable-language)

There is an issue with setting the locale for anonymous users for on sites that are using a CDN. It can cause the wrong locale files to be served to anonymous users. See: [Swedish? meta just showed me the UI in Swedish](https://meta.discourse.org/t/swedish-meta-just-showed-me-the-ui-in-swedish/41137). For this reason, I think the approach that you have brought up in this post - [Set language for SSO users - #5 by jeanmonod](https://meta.discourse.org/t/set-language-for-sso-users/78458/5) - would be a better candidate for a PR.

---

<div class="post-metadata">

### Author: ![jeanmonod](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jeanmonod/32/119952_2.png) [@jeanmonod](https://meta.discourse.org/u/jeanmonod)
#### Post date: [April 10, 2018, 4:57pm UTC](https://meta.discourse.org/t/switch-language-via-url/28937/17 "2018-04-10T16:57:24Z")

</div>

> [@simon](#):
>
> Adding the ability to set the locale through a query string could be done through a plugin

Ok, I will go the plugin way to start, let’s see then if you have the interest to integrate that into the core.

> [@simon](#):
>
> There is an issue with setting the locale for anonymous users for on sites that are using a CDN

I’m not sure I understand the issue correctly, but I can understand the potential problem of caching pages that are built upon a user header such as `HTTP_ACCEPT_LANGUAGE `. Here we are talking about a query string, so the CDN should cache the answer separately…

> [@simon](#):
>
> For this reason, I think the approach that you have brought up in this post - Set language for SSO users - would be a better candidate for a PR.

It’s really two separate topics:

- The query string will help me to provide the appropriate interface to an anonymous user.
- The SSO extension should help me to avoid all user accounts to be created with the default locale.

Ok, in the coming days I will start to work on those two plugins, just ping me if you think I should do PR instead.

Thanks for your help so far

---

<div class="post-metadata">

### Author: ![GoMezzo](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@GoMezzo](https://meta.discourse.org/u/GoMezzo)
#### Post date: [May 14, 2018, 4:02pm UTC](https://meta.discourse.org/t/switch-language-via-url/28937/18 "2018-05-14T16:02:35Z")

</div>

Did you solve this? We want to use the Swedish language as well, but we cannot even get the main installation to change from English to Swedish. It is completely stuck on the English installation, which I do not want to present to a Swedish audience.

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [July 23, 2020, 12:31am UTC](https://meta.discourse.org/t/switch-language-via-url/28937/20 "2020-07-23T00:31:45Z")

</div>

As of this commit from @david, this should work cleanly now 🎉

[https://github.com/discourse/discourse/pull/10289](https://github.com/discourse/discourse/pull/10289)

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [July 23, 2020, 12:31am UTC](https://meta.discourse.org/t/switch-language-via-url/28937/21 "2020-07-23T00:31:48Z")

</div>


