Using Localization in the MasterPage and other pages (VB-sample) – Part 2
Posted by BottOm on October 2, 2007
The following steps are pretty easy. In Part 1 I explained how to set up the MasterPage and other pages by creating a BasePage.vb file and using a cookie and sessionobject. Your site should now be ready to use localization.
Part 2 is about adding resourcefiles and using those resources on the different pages (including your MasterPage).
Haven’t read Part 1? Then you have missed something!
Step 1. Folders for your resources
In your solution, you can have 2 different resourcefolder: App_GlobalResources and App_LocalResources. You can add them by rightclicking the solutionfile and choosing “Add ASP.NET Folder”.
The folder GlobalResources is used for resources available to the whole website. I use it for storing my sitemapresources (a detailed tutorial about this feature is on my to-do list).
Naming your global resource files:
name.language.resx or
name.language-culture.resx
LocalResources are used to store items used for a single page or control.
Naming your local resource files:
pageOrControlName.extension.language.resx or
pageOrControlName.extension.language-culture.resx
Step 2. Adding resourcefiles
Say you have 2 languages in your dropdownlist created in Part 1. English, which is the default language and French.
Then create the following files for your MasterPage:
MasterPage.master.resx (default)
MasterPage.master.fr.resx (French)
For the homepage:
Home.aspx.resx
Home.aspx.fr.resx
Every resource in those resx-files has to have a Name and a Value.
Place an asp-label named lblWelcome on your homepage (or MasterPage or other page, the principle is the same so I will only discuss it once).
In the resx-file for the English page type lblWelcome.Text as Name and Welcome as Value. For the French version typ: lblWelcome.Text and Bienvenue as Value.
Step 3. Last step
Change in the aspx-file for your website (eg. Home.aspx) the text of your labelcontrol: from Text=”" to Text=”<%$ Resources:lblWelcome.Text %>”.
It should look similar to this:
<asp:Label ID=”lblWelcome” runat=”server” Text=”<%$ Resources:lblWelcome.Text %>” />
Code sample:
Download the code sample for this tutorial here.
Please report any bugs or problems, it hasn’t been tested on another computer.
End note:
You have (hopefully) succesfull completed the tutorial and are ready to use localization throughout your website!
If you have any remarks or suggestions, please feel free to comment this topic.
Andrei Petrut said
Hi. I tried to implement you solution, but I get an error in the BasePage class at runtime, when checking for Session variable. It says it doesn’t exist !
BottOm said
What name did you give your sessionvariable? Make sure it’s the same name everywhere it’s used.
Maybe you can try adding these lines to the InitializeCulture-method of the basepage:
If Session(“yourSessionName”) Is Nothing Then
Session(“yourSessionName”) = “en”
End If
Anyways, I started creating a codesample this weekend which is almost finished now.
So you’ll soon have a live and working sample.
I’ll try to post it asap
Using Localization in the Masterpage (VB-sample) - Part 1 « Visual Studio Web Developer Tutorial Corner said
[...] (RSS) « Enabeling IntelliSense in a Skinfile Using Localization in the MasterPage and other pages (VB-sample) – Part 2 [...]
frank said
Hi Koen,
Great tutorial, it solved my problem but :
I have two “top” masterpages wich both have different nested masterpages and child pages.
The language selection buttons are on both “top” masterpages. Everthing works as long as I stay on one of the two masterpages and their childeren.
When I interchange form one to another I need to refresh the page in order to make the language switch. I want to avoid a refresh or make the page expire since they both hold a flash header that will reload each time.
Hope you understand issue.
Do you have a solution for this problem?
Thank you very much.(groetjes)
Roman said
Hi,Koen,
It’s realy great tutorial
BUT
I have error like in first post
I’m work in VS2008 in .net2 framework.
Hope you understand issue.
Do you have a solution for this problem?
Diego said
Excelent Tutorial!!!!
Thank you very much for all the explanation!!!
BottOm said
Thanks!
I’m glad people still appreciate it!
sam said
I created localization on all my pages and later some date when want to change some data in the french master resource (MasterPage.master.fr.resx ) it is not reflecting the pages!!!
Any ideas