[RESOLVED] Enumerate installed cultures in satellite assembly
Hello,
I use a satellite assembly to hold all the localization resources in a Visual Basic 2008 application.
How can I list the available languages that exists for the application?
"CultureInfo.GetCultures" retrieves all available cultures in the OS, I only need the list of the ones I have created.
Thank you.
Re: Enumerate installed cultures in satellite assembly
What do you mean "created"? How do you create a culture?
Re: Enumerate installed cultures in satellite assembly
I mean, set a form "Localizable = True", then change its "Language" property, then change labels etc. to the new language.
That creates new resource files named by the language/culture, for example:
form1.en-US.resx
form1.fr-FR-resx
form1.it-IT-resx
etc.
I need to enumerate only these languages (English, French, Italian, etc.) and not all available cultures (which is what CultureInfo.GetCultures does).
Re: Enumerate installed cultures in satellite assembly
Well, I'm not sure about it, but from all I know, localization takes place automatically basing on the installed UI culture of a user.
If you want to switch culture during run-time then you should do something like this:
vb.net Code:
For Each c As Control In Me.Controls
Dim resources As New System.ComponentModel.ComponentResourceManager(GetType(Form1))
Dim culture As Globalization.CultureInfo = System.Globalization.CultureInfo.InstalledUICulture
resources.ApplyResources(c, c.Name, culture)
Next c
If the installed culture is not found then a fallback culture will be used.
I don't know if there a way to enumerate the localized cultures, but localization data resides in the resource files of your form and I think it should be obvious to a developer which localizations are available.
By the way, I find the localization model provided a bit too heavy. I presumed that a user might want to swtich the ui language even if his installed ui culture says otherwise (for example I use a Russian culture but choose English UI for many programs simply because English words are shorter than Russian and occupy less space). So I always designed my apps in such a way that a user could explicitly assign an UI language which is too cumbersome with the localization model that is offered by Microsoft.
I use simple resource strings and reload them if a user changes language. And of course I can store the list of supported languages somewhere.
Re: Enumerate installed cultures in satellite assembly
Thanks Cicatrix, but I know how to switch culture etc., I save it in My.Settings.
I just wanted to list the evailable cultures so that the user could have a dropdown of the (few) languages of the software, not all of the available languages.
What technique do you use? Something like a database table with all your strings?
Re: Enumerate installed cultures in satellite assembly
I said - I use resource files, but any external storage will do. Even plain text files. External files are useful when you plan to add some languages later.
Re: Enumerate installed cultures in satellite assembly
Thanks Cicatrix.
Anyone else knows how to solve my problem please?
Re: Enumerate installed cultures in satellite assembly
Thanks Cicatrix.
Anyone else knows how to solve my problem please?
Re: Enumerate installed cultures in satellite assembly
Well, I've finally managed to enumerate the languages basing on the example from here:
http://blogs.msdn.com/michkap/archiv...25/560838.aspx
But I don't particularly like this method since it takes a lot of time and generates many exceptions which I simply ignore.
Quote:
The resource model clearly seems to rely on more of a "let the user choose and fall back p.r.n." mechanism than an "enumerate and choose" mechanism.
(Add a combobox named Combobox1 on a form):
vb.net Code:
Imports System.Reflection
Imports System.Globalization
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
For Each ci As CultureInfo In CultureInfo.GetCultures(CultureTypes.SpecificCultures Or CultureTypes.NeutralCultures)
Try
Assembly.GetExecutingAssembly.GetSatelliteAssembly(ci)
Dim str As String = String.Format("{0} ({1})", ci.NativeName, ci.EnglishName)
ComboBox1.Items.Add(str)
Catch ex As Exception
' Do nothing (no resource exist)
End Try
Next
End Sub
End Class
Re: Enumerate installed cultures in satellite assembly
Thank you, it works (but in fact it is very slow, and enumerates all "installed" languages EXCEPT the default...)
You have been very helpful, thanks.