Click here to Skip to main content
15,887,256 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello.

I'm creating a multi language application. I have successfully changed the CurrentCulture without "close & reopening" my Application, But my problem is the (Status Labels & Messages) since i can only change it Programmatically. Anyway, I needed to change the (Status Labels & Messages) by creating a resource file of the language and get the "Text" from it programmatically like the code below.
VB
If LangCB.Text = "English" Then
    MsgBox(My.Resources.EN.FirstMsg)
Else
    MsgBox(My.Resources.ES.FirstMsg)
End If
If LangCB.Text = "English" Then
    MsgBox(My.Resources.EN.SecondMsg)
Else
    MsgBox(My.Resources.ES.SecondMsg)
End If

And the code dose work but i have to do it in EVERY (Status Labels & Messages). But i was wondering if there is a possibility to do it with less code like doing a loop or so (since I'm not that good at "Loops" & "For Each").

I was thinking it will be something like this (funny i guess)
VB
Private Sub ButtonClick
        StringInNewLang(FirstMsg)
End Sub

Private Sub StringInNewLang("string to search(FirstMsg)")
        If LangCB.Text = "English" Then
        For Each S As "string to search(FirstMsg)" In My.Resources.EN
        MsgBox(S)
        Next
        Else
        For Each S As "string to search(FirstMsg)" In My.Resources.ES
        MsgBox(S)
        Next
        End If
End Sub

Thanks in Advance :D
Posted

I don't exactly see why you are trying to reinvent the wheel :)
Localization framework already has got all what you need.

The first wrong thing to do, IMHO, is to handle localizations directly by comparing to a ComboBox' text property. Ideally, you should handle the SelectedIndexChanged event of the ComboBox to set the current UI culture:
VB
Private Sub LangCB_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles LangCB_SelectedIndexChanged
   Thread.CurrentThread.CurrentCulture = new CultureInfo(LangCB.Text)
   '' or (I'm never sure about these two)
   Thread.CurrentThread.CurrentUICulture = new CultureInfo(LangCB.Text)
End Sub

And then you just call the resource from its name, you do not have to specify the culture, as the ResourceManager will handle that for you, observing the current culture value to determine which resource file to search for matching entry:
VB
MsgBox(My.Resources.FirstMsg)
MsgBox(My.Resources.SecondMsg)

If a value for a given culture is not provided, the resource manager will default to the default culture (the .resx file without culture specification).

Or, there is something in your issue I'm totally missing.

[Edit] Added example of a localized resource

- Res.resx file (default language = english)
Resource name: InvalidValue
Resource value: Invalid value

- Res.de.resx file (for German culture)
Resource name: InvalidValue
Resource value: Ungültiger Wert (sorry for translation, eventually ;) )

- Res.fr.resx file (for french culture)
Resource name: InvalidValue
Resource value: Valeur invalide

- etc.

You can see that:
- Every localization file is named with the default culture file name plus the culture identifier
- In each localization file, resource names must be the same

Please let me know if it is still unclear.

[Edit]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 28-Oct-15 20:13pm    
I voted 5, but... even though you explained one code thing, I doubt it will be enough to grasp the idea of globalization and localization. I have my way to explain such things, please see Solution 2.
—SA
phil.o 28-Oct-15 20:52pm    
Sure, Sergey, thanks. My point was not drawing a complete picture, but rather checking if basic usage is understood.
I already saw your answers on that subject, I agree with you they are more detailed than mine :)
Fa3o 28-Oct-15 22:01pm    
Thanks for the answer, But I'm using 4 Languages (EN, ES, FR) and lastly is "Kurdish" which it doesn't exists in the "Language" Options and its Unicode so i have to do it by creating my own resource, So the loop would be much better (If it does exists), And thanks for your time :D
phil.o 28-Oct-15 22:40pm    
According to MSDN: Supported Culture Codes, there is a "ku-Arab" Central Kurdish culture definition. I apologize if that does not correspond to your culture, I must admit my ignorance on that matter.
Technically, imagine you have a Res.resx default-culture resource file (let's say english), you just have to add another resource file in the same directory, named Res.ku-Arab.resx. A special trick exists when the .resx file resides in the project's "Properties" directory, as it is impossible to create a file directly in this directory. Just create the file at the root directory of the project, and drag -and-drop it, in the solution explorer, to the "Properties" directory.
Fa3o 30-Oct-15 8:05am    
I did what you commented and it did work. Thanks

As for your Reply for my post, Did you meant that i should change the message resource name for each language like the following.

English = FirstMsg
Spanish = PrimerMsg

Because i have at least 21 Messages in my code that it needs to be translated to another 3 languages !
In addition to Solution 1.

I personally think that "reinventing the wheel" has been one of the most productive activity in the history of technology, especially when it comes to real car/bike wheels. The problem is when the potential inventor is not up to the level achieved by the predecessors. Fresh look of an amateur can also be a good thing, but not if you keep ignoring existing achievement for too long and take you home-baked approach too serious. You have done your attempt, and it's quite naive and bad, which you could find out by learning existing globalization and localization technology.

With .NET, you never need to read resources by yourself. Instead, you need to use auto-generated code file created when you add resources. The idea is: you create all resources for one culture, say, one of English cultures. Then you create separate resource-only assemblies which are translated copies of the main assembly, but contain only resources translated into different language; output directories of those assemblies should be named according to the names of the cultures. Note that this mechanism allows to outsource translation works without giving full source code to localization worker or a team. Such assemblies are called satellite assemblies. After localization, the main assembly can switch to the satellite resources automatically, by changing the thread's culture and UI culture, as explained in Solution 1.

For more detail, please see my past answers:
How to use a Single resx file for 3 languages[^],
globalization/localization problem in winform .net[^],
globalization in winforms[^].

—SA
 
Share this answer
 
Comments
Fa3o 28-Oct-15 22:04pm    
Thanks for the answer, I commented on Phil's Post, I think you should take a look, And i really appreciate your time for answering :D
Sergey Alexandrovich Kryukov 28-Oct-15 23:30pm    
You are welcome. Consider accepting the answer formally.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900