Detecting user language

סיכום מבוסס בינה מלאכותית

The discussion revolves around detecting the user’s chosen language in Discourse. gberaudo asks if a custom attribute can be added to the page to reflect the selected language, necessary for translating UI customizations. jomaxro references a prior discussion on variable default interface languages. simon mentions a plugin that handles language detection but needs updating for the new plugin API. gberaudo clarifies that the plugin doesn’t add a custom attribute, but simon confirms this is possible. gberaudo explains the need for language detection to localize a custom menu. simon suggests using I18n.locale on the client side, which gberaudo acknowledges as helpful. gberaudo later praises the plugin’s functionality and requests assistance in updating it for the new API, highlighting its usefulness for their setup.

Is there a way to detect what language was chosen by the user?

The default language is availabe in the lang attribute of the html tag.
Would it be possible to add a custom attribute with the chosen language?

This is necessary to translate UI customizations.

This has come up before:

There’s a plugin for this that works well. It needs to be updated to work with the new plugin API. I can do that later today.

Simon, I do not see where your plugin adds an attribute to the page with
the chosen language code.
Am I missing something? Maybe it is easy to add?

Anyway, thank you for proposing to update it. However, I don’t know yet if
I will be able to use it.

It sets the locale. The UI is then automatically translated by Discourse.

What is the attribute that you would like to be adding?

edit - I see :slight_smile: - yes that would be possible.

It seems possible to add a custom menu through admin->Customize->css_html section.
This menu must be localized. If I can find out the chosen language from the page, I can chose the correct translation for the menu.

The chosen locale is always available on the client as I18n.locale. Maybe you can find it that way in your code.

Thank you, that will help.

Hi @simon, indeed your plugin is great. I gave it a try on my dev instance (v1.5.0.beta8 +164) and it was fantastic!

I found out about the new plugin API.
Indeed, it woud be great to be able to use the plugin with newer Discourse versions. Your help/guidance will be appreciated.