Force lang for user

Stephanie Sherriff asked on March 16, 2018 08:23

Hi,

I have a specific requirement where I don't want a user to be able to change the culture they are viewing. Other than a login page the site is entirely authenticated, so for all important pages I will always have a logged in user. What I want to do is check the assigned culture for the user against the current culture, and if it is different, force to the user's culture.

I have something working with the URL Rewriting event 'ProcessRewritingResult.Before'. It isn't perfect, and I'm wondering if there is actually a better way of doing it.

I have the URLs setup so the culture is in the url like this:

For Australia: /au/blahblahbpage
For Brazil: /br/blhablahpage

In the rewrite event I just create a new path to the correct one and set context.RewritePath.

The content displaying on the page is correct if I do this, but the URL actually doesn't change. So let's say the user is set to Australia and I manually change the url to be /br instead, my code fixes it so the Australian content shows, but the url still has /br. On the next request, the url is correctly showing /au.

I'd love it if I can get the url correct as well as the content.

It would be even better if there were an easier way of just setting lang=en-AU (for example) and having the url handled by Kentico instead of doing what I'm doing. It seems sort of like a hack instead of a real way of changing the culture.

Thanks for your help!
Steph

Recent Answers


Stephanie Sherriff answered on March 16, 2018 08:51

I might have just solved my own issue, but maybe someone can tell me if there is a better way.

I actually ended up just adding to the end of the path ?lang=en-AU (or whatever the user's assigned culture is) and that seems to do all the work for me.

Cheers,
Steph

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on March 16, 2018 13:44

The lang url parameter is fine and what I use often. You could go a programmatic approach with a custom web part, I created one for a client that sets the path and language based on IP using a lookup service. It you want I can try to find the api call to set it, otherwise your approach is fine!

0 votesVote for this answer Mark as a Correct answer

Stephanie Sherriff answered on March 19, 2018 00:32

Hi Trevor,

If it isn't too much trouble I'd love to know the API call.

While my own solution is currently working, we decided that the site probably doesn't even need to have the country in the path, and I'd like to remove both the /au and /br, and when I do that, my solution stops working.

Cheers, Steph

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.