HighTechTalks DotNet Forums  

Runtime detection of available languages?

Dotnet Internationalization microsoft.public.dotnet.internationalization


Discuss Runtime detection of available languages? in the Dotnet Internationalization forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
AT
 
Posts: n/a

Default Runtime detection of available languages? - 05-30-2006 , 01:42 PM






Hi all. I am writing an application that will allow the user to change
cultures at runtime. I've found info on how to do that and I think I'm
good there.

I'd like to provide the user a menu (or something similar) that lists
the available cultures to choose from. It's simple to just add these
in statically at build time (or have an external data file). But I'd
much rather prefer that there was some way to detect what languages are
available to the application (or to a given Form; it makes no
difference to me). That way as we add new languages we don't have to
change the program or extra data files.

I've looked through the MSDN documentation and tried several things,
with no success. I've searched on the web and not had any luck either.

Is this possible? Even a routine that would just enumerate what
satellite assemblies were available (again, for the application or just
a single Form is fine) would be good. Anyone know how to do this or
have any tips on where to look?

Thanks!

Carl


Reply With Quote
  #2  
Old   
Michael \(michka\) Kaplan [MS]
 
Posts: n/a

Default Re: Runtime detection of available languages? - 05-30-2006 , 03:37 PM






See

http://blogs.msdn.com/michkap/archiv...25/560838.aspx

and also

http://blogs.msdn.com/michkap/archiv...27/608625.aspx

for some info that may be helpful here (that first post, especially).


--
MichKa [Microsoft]
NLS Collation/Locale/Keyboard Technical Lead
Globalization Infrastructure, Fonts, and Tools
Blog: http://blogs.msdn.com/michkap

This posting is provided "AS IS" with
no warranties, and confers no rights.




<carl_bevil (AT) yahoo (DOT) com> wrote

Quote:
Hi all. I am writing an application that will allow the user to change
cultures at runtime. I've found info on how to do that and I think I'm
good there.

I'd like to provide the user a menu (or something similar) that lists
the available cultures to choose from. It's simple to just add these
in statically at build time (or have an external data file). But I'd
much rather prefer that there was some way to detect what languages are
available to the application (or to a given Form; it makes no
difference to me). That way as we add new languages we don't have to
change the program or extra data files.

I've looked through the MSDN documentation and tried several things,
with no success. I've searched on the web and not had any luck either.

Is this possible? Even a routine that would just enumerate what
satellite assemblies were available (again, for the application or just
a single Form is fine) would be good. Anyone know how to do this or
have any tips on where to look?

Thanks!

Carl




Reply With Quote
  #3  
Old   
AT
 
Posts: n/a

Default Re: Runtime detection of available languages? - 05-30-2006 , 05:34 PM



Excellent, thank you!

It's unfortunate that we have to cycle through them all (not to mention
it takes a few seconds). I did have to add this to the code
("cultures" is a generic list of CultureInfos):

catch (FileNotFoundException)
{
// Swallow this exception, it means no such resources exist for the
given language

// But... if the CurrentUICulture is not found, it means we
// are using the default culture, and we need to add it to the list.
if (ci.Equals(Thread.CurrentThread.CurrentUICulture))
cultures.Add(ci);
}

.... since my default culture is geared for en-US, and I don't have
specific en-US resources. Perhaps this is not "best practice", I'm not
sure (new to .NET).

Unfortunately, if the code is run on a machine whose CurrentUICulture
isn't en-US and isn't represented by a satellite assembly, the
machine's current culture will be incorrectly added as the default
culture. I'm not sure how to get around this issue (other than making
blank en-US resources, which I'm not too hot on).

Thanks again!

Carl


Reply With Quote
  #4  
Old   
Michael \(michka\) Kaplan [MS]
 
Posts: n/a

Default Re: Runtime detection of available languages? - 05-31-2006 , 10:35 AM



Well, since you are looking for your app's languages rather than the .NET
Framework's, you would change the code that gets the assembly from the .NET
Framework itself to something that gets your own assembly!


--
MichKa [Microsoft]
NLS Collation/Locale/Keyboard Technical Lead
Globalization Infrastructure, Fonts, and Tools
Blog: http://blogs.msdn.com/michkap

This posting is provided "AS IS" with
no warranties, and confers no rights.


<carl_bevil (AT) yahoo (DOT) com> wrote

Quote:
Excellent, thank you!

It's unfortunate that we have to cycle through them all (not to mention
it takes a few seconds). I did have to add this to the code
("cultures" is a generic list of CultureInfos):

catch (FileNotFoundException)
{
// Swallow this exception, it means no such resources exist for the
given language

// But... if the CurrentUICulture is not found, it means we
// are using the default culture, and we need to add it to the list.
if (ci.Equals(Thread.CurrentThread.CurrentUICulture))
cultures.Add(ci);
}

... since my default culture is geared for en-US, and I don't have
specific en-US resources. Perhaps this is not "best practice", I'm not
sure (new to .NET).

Unfortunately, if the code is run on a machine whose CurrentUICulture
isn't en-US and isn't represented by a satellite assembly, the
machine's current culture will be incorrectly added as the default
culture. I'm not sure how to get around this issue (other than making
blank en-US resources, which I'm not too hot on).

Thanks again!

Carl




Reply With Quote
  #5  
Old   
AT
 
Posts: n/a

Default Re: Runtime detection of available languages? - 05-31-2006 , 01:57 PM



I think I have that:

Assembly assembly = Assembly.GetExecutingAssembly();

.... I use that in place of the line that accessed the mscorlib
assembly.

But I'm not sure if this will solve the problem... if someone is
running our app on, say, a machine with the Finnish culture installed,
and we haven't localized for this culture, then this:

if (ci.Equals(Thread.CurrentThread.CurrentUICulture))
cultures.Add(ci);

.... will return true when ci is the Finnish culture, and add Finnish to
the cultures list. When what I really want is it to take account of
the special case of adding the "default" culture (in this case, en-US).
I could hard code for this I suppose, but it would break if the
default culture was anything but en-US. Perhaps this will never be the
case and is "good enough".

Carl

Michael (michka) Kaplan [MS] wrote:
Quote:
Well, since you are looking for your app's languages rather than the .NET
Framework's, you would change the code that gets the assembly from the .NET
Framework itself to something that gets your own assembly!


Reply With Quote
  #6  
Old   
AndrewEames
 
Posts: n/a

Default Re: Runtime detection of available languages? - 06-07-2006 , 04:33 PM



I already do a lot of what you are doing. (Dynamic changing of cultures was a
fun little exercise!)

I use the following function to determine what the actual culture of
resources used will be
HTH
Andrew

/// <summary>
/// Returns the actual culture that will be used by resource lookup
/// </summary>
/// <returns></returns>
public static CultureInfo ActualCulture
{
get
{
CultureInfo currentCulture = Thread.CurrentThread.CurrentUICulture;
Assembly entryAssembly = Assembly.GetEntryAssembly();
CultureInfo neutralCulture = entryAssembly != null ?
GetNeutralResourcesCulture(entryAssembly) : CultureInfo.InvariantCulture;
if (currentCulture == neutralCulture)
return currentCulture;

string appDir = Path.GetDirectoryName(Application.ExecutablePath);
if (Directory.Exists(Path.Combine(appDir, currentCulture.Name)))
return currentCulture;

CultureInfo parentCulture = currentCulture.Parent;
if (Directory.Exists(Path.Combine(appDir, parentCulture.Name)))
return parentCulture;

return neutralCulture;
}
}

"carl_bevil (AT) yahoo (DOT) com" wrote:

Quote:
I think I have that:

Assembly assembly = Assembly.GetExecutingAssembly();

.... I use that in place of the line that accessed the mscorlib
assembly.

But I'm not sure if this will solve the problem... if someone is
running our app on, say, a machine with the Finnish culture installed,
and we haven't localized for this culture, then this:

if (ci.Equals(Thread.CurrentThread.CurrentUICulture))
cultures.Add(ci);

.... will return true when ci is the Finnish culture, and add Finnish to
the cultures list. When what I really want is it to take account of
the special case of adding the "default" culture (in this case, en-US).
I could hard code for this I suppose, but it would break if the
default culture was anything but en-US. Perhaps this will never be the
case and is "good enough".

Carl

Michael (michka) Kaplan [MS] wrote:
Well, since you are looking for your app's languages rather than the .NET
Framework's, you would change the code that gets the assembly from the .NET
Framework itself to something that gets your own assembly!



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.