Last post Sep 30, 2006 02:46 PM by vivek_iit
Sep 28, 2006 02:42 PM|MrPimp|LINK
I need to implement localization in a web site we have at work. It's a demo web site to show our clients a sample way to publish tutorials videos covering the use of our software. The site is fully functional in French, but I need a way to provide the
same information in english.
All the content is already available in both languages (the navigation treeview is populated from an XML-based datatable, and so is the tutorials' description panel.
Now I need to implement that for the html controls on the page (labels.innertext, button captions, page title, and so on.
I think the most efficient way to do that is through resource files, but I just can't find information on that subject that doesn't presume that I know all about .resources, .resx, compiled /satellite and whatnots... :(
I'm new to ASP and I'd like to begin the right way.
For now I'm using a mypage.aspx.resx file where I entered the french/english strings that should be the content of my controls, but I just can't access those value...
1 private void ManagePageTranslation(System.Globalization.CultureInfo Culture)
3 string strCult;
4 strCult = "_"+Culture.Name.Substring(0, 2);
5 System.Resources.ResourceManager resMngr =
new System.Resources.ResourceManager("FITutorials.aspx", System.Reflection.Assembly.GetExecutingAssembly());
6 //Where FITutorials.aspx is the name of the main page of the site
7 this.Header.Title = resMngr.GetString(this.ToString() +
"." + "Titre" + strCult);
8 //The ressource entries containing the localised title are "Titre_fr" and "Titre_en"
9 foreach( Control Ctrl
//That section is from the .NET 1.1 version; I figured another way to cycle through my page controls
14 ((TextBox)Ctrl).Text = resMngr.GetString(this.ToString() +
"." + ((TextBox)Ctrl).ID + strCult);
17 ((Label)Ctrl).Text = resMngr.GetString(this.ToString() +
"." + ((Label)Ctrl).ID + strCult);
20 ((Button)Ctrl).Text = resMngr.GetString(this.ToString() +
"." + ((Button)Ctrl).ID + strCult);
23 ((LinkButton)Ctrl).Text = resMngr.GetString(this.ToString() +
"." + ((LinkButton)Ctrl).ID + strCult);
26 ((HyperLink)Ctrl).Text = resMngr.GetString(this.ToString() +
"." + ((HyperLink)Ctrl).ID + strCult);
Any insight as to where to begin? Is my ressource manager correctly defined? Am I right to store the localized strings in mypage.aspx.resx?
Sep 29, 2006 02:55 AM|vivek_iit|LINK
You can store Page specific resources in the App_LocalResources folder having resource files with names starting with the individual Page names (like MyPage.aspx.resx).
See my article for further details:
Hope this helps,
Sep 29, 2006 02:58 AM|vivek_iit|LINK
Sep 29, 2006 10:48 AM|MrPimp|LINK
"lblTSearch" Text="Rechercher:" runat="server" height="20px">
Thanks for that article it did indeed «explains all important concepts reagrding globalization/localization practically and comprehensively» ;)
I made a few leaps forward! I'm stil not grasping it enough to make it work, though... :'(
I'm trying to use the implicit localization with aspx.fr-CA.resx and asp.en-US.resx for all of my pages (I know it's not the best way, but there's only 5 of them, and the site I'm building is only a demo, as I said earlier... plus, I'm in a bit a bit of a hurry.) I chose the implicit version instead of explicit because nearly all pages have controls with the same names, so it's be easier to tell them apart that way.
Here's a asp:label as used in my page :
<asp:label id=lblTSearch meta:resourcekey="lblTSearch" Text="Rechercher:" runat="server" height="20px"></asp:label><asp:label id=lblTSearch meta:resourcekey="lblTSearch" Text="Rechercher:" runat="server" height="20px"></asp:label>
it's in the frmTutorials.aspx page and I have resource files named frmTutorials.aspx.fr-CA.resx and frmTutorials.aspx.en-US.resx in App_LocalResources folder, with an entry in both of them named «lblTSearch», but nothing will translate when I click on the language LinkButton. Could you tell me what the difference is between the «text» attribute, and text between the opening and closing tags? Should they both be empty?
(code for the button, plus the setCulture function, which you may recognise ;) )
protected void lbtnLanguage_Click(object sender, EventArgs e)
if (((CultureInfo)Session["UICulture"]).Name == "fr-CA")
protected void SetCulture(string name)
Thread.CurrentThread.CurrentUICulture = new CultureInfo(name);
Thread.CurrentThread.CurrentCulture = new CultureInfo(name);
Session["UICulture"] = Thread.CurrentThread.CurrentUICulture;
Session["MyCulture"] = Thread.CurrentThread.CurrentCulture;
Am I supposed to work with a ressource manager at some point, of should the framework be able handle it all behind the scene, from the moment I set the currentCulture and currentUICulture?
I've downloaded the sample code that came with your excellent article on The Code-Project, to look it up, bu didn't find any instanciation of a resource manager.
As ever, any insight on that situation is most welcome.
Sep 29, 2006 11:07 AM|MrPimp|LINK
I forgot to mention that the site was was converted from asp 1.1 when I begun working on it n VS2005. It might be important (who knows) or give a clue to someone who had similar experiences....
Sep 30, 2006 02:46 PM|vivek_iit|LINK
Thanks for your feedback on my article! As for your query, you can use implicit as well as explicit localization. Implicit localization helps trimming down the size of the global resource files and helps in better overall resource management.
Also, you don't need to call ResourceManager class explicitly. Here is how I use it:
You do not need to do anything manually to set these implicit localization properties. Just open your web page in the Design mode, go to Tools->Generate Local Resources. This will automatically create
a resource file for your Page. You only need to set the values (Control.Property) of different fields for each control in the resource file editor in Visual Studio 2005.