ASP.net Maintaining CurrentUICulture for bookmarks and linked urls
So I have a website that is in two different languages and I've set it up to pull text from a resources file based on CurrentUICulture.
I have two link buttons that a user can click to switch between english and russian.
Example:
Thread.Curre开发者_StackOverflow中文版ntThread.CurrentUICulture = new CultureInfo("ru-RU");
And this switches the language just fine. But I'd like to have the current language somehow reflected in the current URL for bookmarking purposes or for sending out links to a page ina specific language. Like on of these examples:
- www.mysite.com/contact/index.aspx?lang=ru
- www.ru.mysite.com/index.aspx
- www.mysite.com/ru/contact.aspx (altough this directory doesnt actually exist)
I'm also wondering how to best segregate Google Analytics data between the two different languages.
I'd recommend an HttpModule to read this parameter out. Have you thought about just storing this in a persistent cookie for the user as well?
See this link -
Is it safe to use an HttpModule for localization?
Pass the language parameter in the url, and parse this parameter in InitializeCulture to set the CurrentUICulture
. You can use querystring or routing for the language parameter.
In addition to url, you can use other state management options as fallbacks to decide the language. However, it is usually good to be able to reset the state with a url.
For the "switch language" button in ASP.NET, you can use:
<asp:Hyperlink>
, with the language parameter in NavigateUrl<asp:LinkButton>
, with Response.Redirect in the event handler
See Also:
Best way to handle URLs in a multilingual site in ASP.net
精彩评论