Switch language via URL?

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?

4 Likes

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

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.

1 Like

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

3 Likes

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.

2 Likes

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.

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

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.

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: 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.

2 Likes

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

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.

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

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.

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?

Not yet. I have stopped working with Discourse.

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

https://github.com/discourse/discourse/blob/master/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

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

2 Likes

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

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. For this reason, I think the approach that you have brought up in this post - Set language for SSO users - #5 by jeanmonod - would be a better candidate for a PR.

5 Likes

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

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…

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

2 Likes

Did you solve this? We want to use Swedish language too but we can not even get the main installation to change from English to Swedish. Its completely stuck at the English install which I dont want to present to a Swedish audience.

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

https://github.com/discourse/discourse/pull/10289

1 Like