Results 1 to 10 of 10

Thread: [RESOLVED] Enumerate installed cultures in satellite assembly

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    72

    Resolved [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.

  2. #2

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    72

    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).

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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:
    1. For Each c As Control In Me.Controls
    2.             Dim resources As New System.ComponentModel.ComponentResourceManager(GetType(Form1))
    3.             Dim culture As Globalization.CultureInfo = System.Globalization.CultureInfo.InstalledUICulture
    4.             resources.ApplyResources(c, c.Name, culture)
    5. 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.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    72

    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?

  6. #6
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    72

    Re: Enumerate installed cultures in satellite assembly

    Thanks Cicatrix.
    Anyone else knows how to solve my problem please?

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    72

    Re: Enumerate installed cultures in satellite assembly

    Thanks Cicatrix.
    Anyone else knows how to solve my problem please?

  9. #9
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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.

    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:
    1. Imports System.Reflection
    2. Imports System.Globalization
    3.  
    4. Public Class Form1
    5.  
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.  
    8.         Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
    9.  
    10.         For Each ci As CultureInfo In CultureInfo.GetCultures(CultureTypes.SpecificCultures Or CultureTypes.NeutralCultures)
    11.             Try
    12.                 Assembly.GetExecutingAssembly.GetSatelliteAssembly(ci)
    13.                 Dim str As String = String.Format("{0} ({1})", ci.NativeName, ci.EnglishName)
    14.                 ComboBox1.Items.Add(str)
    15.             Catch ex As Exception
    16.                 ' Do nothing (no resource exist)
    17.             End Try
    18.         Next
    19.     End Sub
    20. End Class

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    72

    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.
    Last edited by axplains; Feb 8th, 2010 at 06:29 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width