Last post Nov 10, 2007 02:33 PM by vivek_iit
Nov 10, 2007 06:42 AM|jcnet|LINK
I have an English site I need to make available in Spanish. Ideally there will be a preset switch in the users profile to indicate they would like to see the site in Spanish.
My first inclination is to make a copy of the site redirect them after the login.
Outside of doing it this way, or purchasing/using third party products or controls that deal with localization and maybe use xml to drive those controls dynamically. Is their an easy way to handle this in native asp.net 2.0?
As a simple example, say I have a bunch of asp:Labels with hard coded text= properties in my markup. Is the technique to basically set these in the codebhind based on the selected language? Is that the typical and expected route? If so, sound kinda painful
and I'd almost rather maintain a separate site.
Thanks for any help or information.
Nov 10, 2007 07:35 AM|Jos Branders|LINK
It's much easier than that.
<globalization culture="auto" uiCulture="auto" enableClientBasedCulture="true" />
in web.config. This will change the language of your site according to the language of the visitor's browser.
Then, add a resource file by choosing "New item". In that file, add all the strings that you want in your labels. Save it as GlobResource.en-US.resx.
Make a copy of the same strings, now in Spanish (use the same names, but other values). Save it as GlobResource.es-MX.resx.
Now, modify your labels like this:
<asp:Label id="lbl1" Text="<%$Resources:GlobResource, resourcename %>" runat="server"></asp:Label>
Replace "resourcename" by the name that you used in your resource file.
You can also set each label (and other controls) by clicking on the three dots next to "(Expres sions)" in its properties panel.
Select "Resources" in the dropdownlist. Set "GlobResource" for ClassKey and the corresponding name for ResourceKey.
Nov 10, 2007 10:40 AM|jcnet|LINK
Nice! I just did a simple vb.net asp.net project to test it. And do see it using the generated resx file over fixed values on my markup.
Dumb question, I made a copy of the that as resx file .. called it NEW.aspx.resx file under the App_LocalResources folder. Now how do I get my site to use the alternative resx file? I won't be using the browser setting to drive which language will be used.
I will want to control that from a setting in the users profile.
Also, where would be a place to make that switch? any chance I could make it only once and it would stay set for the duration of the session for all pages called by that session?
Thanks in advance!
Nov 10, 2007 11:18 AM|david wendelken|LINK
The prior poster told you what file name to use for the spanish version of the resource file, use it as directed. :)
I don't happen to know the call to set the culture to use, but when you find it, the resource file needs to be that name.
Nov 10, 2007 01:46 PM|Jos Branders|LINK
You can make the switch at any time with this code:
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("es-MX")
Just put the "es-MX" value in the user's profile, and retrieve it from there as well.
Nov 10, 2007 02:33 PM|vivek_iit|LINK
You may want to check this article too:
Hope this helps,