Switch language via URL?


(Quân Nguyễn) #1

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 Malaysia’s #1 Website About Studying University / College (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?


(Jeff Atwood) #2

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


(Quân Nguyễn) #3

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


(Manthan Mallikarjun) #4

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


(Jeff Atwood) #5

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.


(Anton) #6

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.


(Simon Cossar) #7

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


(Anton) #8

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.


(Simon Cossar) #9

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


(Anton) #10

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


(Simon Cossar) #11

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.


(Anton) #12

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.


(Jeanmonod David) #13

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?


(Quân Nguyễn) #14

Not yet. I have stopped working with Discourse.


(Jeanmonod David) #15

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

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?

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_Cossar


(Simon Cossar) #16

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: GitHub - scossar/variable-language: sets the locale for logged in and non-logged in users off the http accept-language header

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 - would be a better candidate for a PR.


(Jeanmonod David) #17

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


#18

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.