You can use ResourceManager's GetString(string, CultureInfo)
overload to get strings in correct language.
BTW. global.asax is probably the better place to set CurrentCulture and CurrentUICulture.
EDIT Providing code sample per Peter's request
Globabal.asax
For starters, I would detect and store culture:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request '
Dim culture As CultureInfo
Dim uri As Uri = Request.Url
If uri.ToString().Contains("nederlands") Then
culture = New CultureInfo("nl-NL")
ElseIf uri.ToString().Contains("polski") Then
culture = New CultureInfo("pl-PL")
Else
culture = New CultureInfo("en-US")
End If
Thread.CurrentThread.CurrentCulture = culture
Thread.CurrentThread.CurrentUICulture = culture
End Sub
Depending on what you want to do, it might not be the best Function to overload, in your case I believe Session_Start
is better (that is unless you want to give users an ability to switch language).
Another thing is, I didn't test it with domain names, I used query parameters and haven't validate them properly but it was just for the purpose of example only.
Create App_LocalResources
Instead of instantiating ResourceManager and compiling your resources manually, I can only suggest you to use App_LocalResources. You just need to right-click on your project in the Solution Explorer, choose Add -> Add Asp.Net Folder -> App_LocalResources.
Create resource files
Create appropriately named resource files: Default.aspx.resx, Default.aspx.nl.resx and add the contents you want to localize to them. In my case it was:
Default.aspx.resx (neutral aka fall-back culture resource file)
label1 Some label
label2 Some other label
Default.aspx.nl.resx (Sorry for my poor Dutch)
label1 Dit is een etiket
label2 Dit is ander etiket
Default.aspx.pl.resx
label1 Jaka? tam labelka
label2 Jaka? inna labelka
Default.aspx.vb (code behind)
In code behind you can programmatically modify contents of your web page like this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label2.Text = Me.GetLocalResourceObject("label2")
End Sub
For many reasons I would not recommend this method (especially it should not be used in page load handler), however it is sometimes needed.
Default.aspx
Finally we are at our beautiful (and very useful) web page:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="<%$ Resources:label1 %>"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
As you can see, you can access the resources with following expression: <%$ Resources:label1 %>
. This will give you the string associated with label1 resource key. Important thing to note: runat="server" is not optional.
HTH. You may want to read the tutorial on Asp.Net Localization as well.