Multilingual and resource files
I have a question about creating a multilingual application and using resource files. I will explain what I did and what I would like the final product to be like this.
I am doing this in VB.Net and using Visual Studio 2008
After creating a new project, I added a resource file to the project (Add -> New Item, Selected Resource File named Resource1.resx).
Then I double clicked on the resource file and was able to add some names and values. For example,
Name - lblFirstName, value - John Name - lblLastName, value - Smith
In my form, I have 2 labels: FirstName and LastName
In the code, I added
FirstName.Text = My.Resources.Resource1.lblFirstName
LastName.Text = My.Resources.Resource1.lblLastName
If I run this code, it works fine. John and Smith are displayed on labels.
Now for my question. Let's say instead of the first and last name the labels (buttons, menu items, etc.) were actually words that would be different in different languages. I would like to have something like
EnglishText.resx SpanishText.resx GermanText.resx
Each resource file will contain the same names, only different values. Depending on the language selected by the user (from the menu), how can I get the corresponding resource file to be used.
Basically, I want
FirstName.Text = My.Resources.<Language Specific Resource File>.lblFirstName
Is something like this possible? Is this an acceptable approach? Is there a better way to do this?
Any advice or guidance is greatly appreciated. I try to check often to see if there are additional questions or more information needed.
The .NET platform is built with localization in mind. There is already an innate mechanism for localizing assemblies and resources based on the current culture. Here are some starter links that you should read before trying to roll your own.
http://msdn.microsoft.com/en-us/library/bb398937.aspx
http://msdn.microsoft.com/en-us/goglobal/bb688096.aspx
source to share
Imports System.Globalization
Imports System.Resources
Public Class Form1
Public rm As Resources.ResourceManager
Private Property CultureInfo As CultureInfo
Public Function getRMValue(ByVal strValue As String)
Dim strLanguage As String
If IsNothing(rm) Then
strLanguage = CultureInfo.CurrentCulture.ToString.ToUpper.Substring(0, 2)
If strLanguage = "EN" Then
rm = My.Resources.English.ResourceManager
Else
rm = My.Resources.Turkce.ResourceManager
End If
End If
getRMValue = rm.GetString(strValue)
End Function
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub btnBye_Click(sender As Object, e As EventArgs) Handles btnBye.Click
MessageBox.Show(getRMValue("messagebox"))
End Sub
End Class
source to share
Let's say you have 3 languages that you could do something like this:
If LanguageChanger<change this to the way you let people change languages> = "English" Then
Language = My.Resources.EnglishText
else if LanguageChanger = "Spanish" Then
Language = My.Resources.SpanishText
else if LanguageChanger = "German" Then
Language = My.Resources.GermanText
End if
Then you can use it:
FirstName.Text = Language.lblFirstName
I just did it from this form, it hasn't tested so sorry if it doesn't work.
source to share