How to set language for SSO users

how can I set the language for a logged in user using sso. I tried to use “locale” key if user is accessing for the first time, it did not work. I believe the “locale” key works if user is anonymous.

Hello @Ankur_Tyagi :wave:t2: welcome to Meta :slight_smile:

This post should help. there are two parameters you need.

1 Like

For a new user who has never logged into Discourse before, it should work if the locale parameter is set in the payload the first time the user logs into the site. For example, using the npm docs that you linked to:

var userparams = {
	// Required, will throw exception otherwise
	"nonce": nonce,
	"external_id": "some user id here",
	"email": "some user email",
	// Optional
	"username": "some username",
	"name": "some real name",
    "locale": "es"
};

For an existing user who already has an account on Discourse, you will need to add the locale_force_update parameter to the userparams and set it to true. I think that for your case you could include the locale_force_update param both when you create a new user and when you update a user:

var userparams = {
	// Required, will throw exception otherwise
	"nonce": nonce,
	"external_id": "some user id here",
	"email": "some user email",
	// Optional
	"username": "some username",
	"name": "some real name",
    "locale": "es",
    "locale_force_update": true
};

Unfortunately, to update the locale, or any other attributes via the DiscourseConnect payload, the user will have to log out of Discourse, and then log back in again. This means that it is unlikely that the user will get the updated locale right away. There is a workaround for this, but I do not think it is handled by the discourse-sso npm package. To update a user via DiscourseConnect without requiring them to log out and log back into Discourse, you need to make a call to the sync_sso route. Details about that are here: Sync DiscourseConnect user data with the sync_sso route.

If you are having trouble getting the locale set correctly for new users, or for users who log out and back in after adding the locale parameters, try enabling the verbose discourse connect logging Discourse site setting. That will allow you to see the parameters that are being sent to Discourse from your site’s /logs page (found at Admin / Logs / Error Logs.)

2 Likes

Hello @Lilly
Thanks for your reply.
I tried it with a new user as well as updating locale for an existing user, but it did not work. I think it only works for anonymous users case. I am looking for a solution for logged in users.

It would be helpful if you can review the setup configuration details below, may be there is something I need to update to fix it, not sure.

  1. Discourse forum version: Discourse 3.2.0.beta1-dev
  2. JS package used for sso: discourse-sso ( version: 1.0.5 )
  3. Discourse package link: discourse-sso - npm

Hi @Ankur_Tyagi I am sorry you are still having problems. Did you review Simon’s advice using the locale_force_update parameter? I think everything I know about SSO is from @simon :slight_smile:

I would carefully go through this information @simon posted as well as the official DiscourseConnect SSO How-To that has been linked.

The verbose discourse connect logging setting and logs can help you troubleshoot this:

1 Like

Thanks @Lilly, i will try this solution