This is what I have derived from the video.
- This only applies for Framework 2.0 and above (not applicable for Framework 1.1).
- We need to override the InitializeCulture in the page class (or codebehind class).
The resulting html tag will be as follows:
<asp:Label ID="lblMessage" runat="server"
Text="<%$ Resources:LocalizedText, Msg1 %>"></asp:Label>
For example if the ClassKey is "LocalizedText", then the resx files will be:
LocalizedText.fr.resx for French
LocalizedText.ms.resx for Malay
LocalizedText.resx for the default.
In the above example the files should all be placed in App_GlobalResources under the application folder (just like there is a bin under the application folder).
As for the resource entry, this is a sample of the resource file entry interface in Visual Studio:
This is a sample of the overridden method:
Protected Overrides Sub InitializeCulture()
Dim lang As String = Request("Language1")
If lang IsNot Nothing Or lang <> "" Then
Thread.CurrentThread.CurrentUICulture = New CultureInfo(lang)
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang)
End If
End Sub
In the above example, I used the dropdownlist similar to the video:
<asp:DropDownList ID="Language1" runat="server" AutoPostBack="True">
<asp:ListItem>auto</asp:ListItem>
<asp:ListItem Value="en-US">English</asp:ListItem>
<asp:ListItem Value="ms-MY">Malay</asp:ListItem>
<asp:ListItem Value="fr-FR">French</asp:ListItem>
</asp:DropDownList>
However, given the logic, we can also use a url parameter, radio button etc to identify the language.
To deployment, it will be like normal with the additional App_GlobalResources folder to copy.
This was the key thing I was looking for. In majority of the examples I saw, the language is automatically detected based on the browser setting. This will not work if the users are sharing a public computer to access the site.
13 comments:
Your example is working, but it doesn't work with a masterpage, because the a masterpage doesn't have a method called initializeculture, how can i solve this. I prefer not working with querystrings
I have not tried master pages. Based on your comment of preferring not to work use querystrings, how will your users select the culture?
If it is based on the IE settings then majority of the articles already do that. The querystrings is just a way to check. The setting could come from a database or even a cookie setting.
All said, let me check on the masterpage and get back to you.
I have verified that it is not possible to perform this setting on the master page.
However, you can create a class and overwrite the initilizeculture in the class and have your aspx codebehind inherit from that class instead:
Imports Microsoft.VisualBasic
Imports System.Threading
Imports System.Globalization
Imports System.Web
Public Class clsPage
Inherits System.Web.UI.Page
Protected Overrides Sub InitializeCulture()
Dim lang As String = Request("lang")
If lang IsNot Nothing Or lang <> "" Then
Thread.CurrentThread.CurrentUICulture = New CultureInfo(lang)
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang)
End If
End Sub
End Class
Then inherit as follows:
Partial Class Sample2
Inherits clsPage
End Class
This way, you to override the sub in every page in your application. I hope this helps. Let me know.
Hello Strovek,
Nice presentation. However I need to go further. Maybe you have an answer : I'm trying to change the culture of the sitemap's menu control in the master page. but i can't find a way to pass the new value of the currentCulture to the sitemap.
The sitemap is localize following these instruction :
http://msdn.microsoft.com/en-us/library/ms178427.aspx
. It works fine. When I change the language of IE, The localization of the sitemap changes and the menu in the master page switches languages. But when the user selects another culture using a click event handlers on the masterpage, the pages are translated but not the menu.
Any ideas ?
Thank you for your attention,
Eve
Off hand I do not have a solution since I have not used sitemap before but I will try it out.
I am curious, how is the sitemap rendered, is it via the master page. This article uses explicit changing of the culture but what you have described is implicit changing of the culture (i.e. depending on the browser culture setting).
You can try not setting enableLocalization="true" on the sitemap and let the page do the culture instead as my previous reply to samet kaya.
If it works, please let me know.
The sitemap is rendered into the masterpage via the asp menu control. The Datasource of the menu control is the default sitmapdatasource which is web.sitemap.
Websitemap is localize with a resource file SiteMapLocazations(.en.resx,.fr.resx,.resx)
Could you post the code? Then I can modify from them and help you fix the problem. I am not that familiar with Sitemap so don't have an answer off hand.
I'd a problem similiar to Eve's one.
When I choosed new language - every element on page became translated after post back, except menu control. I'd solve this problem by setting property "EnableViewState" of menu control to "False".
Thank you for the feedback. I am sure it will help others facing the same situation :)
Hi Strovek,
I did the class with the override and then made the code behind inherit from it, like you said (post on April 15, 2008 1:21 PM), but it didn't work :(
Do you have other solution for this?
Thank you!
One thing I found is the file naming convention and the location of the resource file is very important.
That might be the source of the problem. I can't be certain without seeing the code.
Hello !
Thank you for this nice article :)
I've a problem with the App_LocalResources folder. Imagine that you have two different Default.aspx pages in two different folders:
* Default.aspx (in the root folder)
* /Orders/Default.aspx (in the /Orders folder).
How can you localize thoses pages ?
I tried to create two localized Default.aspx.resx files in two different folders in the App_LocalResources folder:
* App_LocalResources/Default.aspx.resx
* App_LocalResources/Orders/Default.aspx.resx
And I've the following error message at the runtime:
"Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: The resource file '/App_LocalResources/Default.aspx.resx' cannot be used, as it conflicts with another file with the same name."
How can you localize all the different Default.aspx pages ?
Thanks in advance for your support :)
Regards,
John
John,
Are the /Default.aspx.resx and /Orders/Default.aspx.resx different, if so then convert Orders into an application folder so it will /Orders will be the root for the pages under /Orders folder.
Let me know if it works.
Post a Comment