Re: Currently selected language?
Re: Currently selected language?
- Subject: Re: Currently selected language?
- From: Douglas Davidson <email@hidden>
- Date: Wed, 30 May 2001 13:25:23 -0700
On Tuesday, May 29, 2001, at 04:36 PM, Dave Swan wrote:
Is there an easy way to determine what language (English, Japanese,
etc) the user is currently running under OS X?
You can determine what localizations are being used, but it isn't as
simple as you think, and there are usually better ways to do what you
want to do (whatever that is).
If you call
CFBundleCopyPreferredLocalizationsFromArray
(CFBundleCopyBundleLocalizations(CFBundleGetMainBundle()))
you will get an array containing the names of the localizations
currently in use within the main bundle. There may be up to two of
these at the moment; for example, both en_US and en might be in use,
with some region-specific items coming from en_US and some others from
the generic en. The entries in this array are in the format used in the
main bundle, which might be language names (English, French), language
abbreviations (en, fr), or locale abbreviations (en_US, fr_CA). If your
bundle is not the main bundle, then you may wish to use your bundle
instead of the main bundle in the above.
Do you see why this is complicated, and why I recommend doing something
else? Usually you don't want to know the localization in use directly;
what you want is to choose some value based on the current
localization. If the value you want is a string, or a resource file, or
a Resource Manager resource, then the appropriate thing to do is just to
make it a localized value, and get it normally using CFBundle or
NSBundle APIs. You can do quite a bit with localized strings, resource
files, or Resource Manager resources.
For example, if you wanted to display to the user what language he/she
was running in, the value you want isn't "French" or "Japanese", it is
the correct localized language name; things like "French" or "fr" or
"fr_CA" should not be user-visible. So what you want is a localized
string.
If you have more complicated information, you can put it in a separate
file, or in a Resource Manager resource, or even encode it in a
localized string. You could store an arbitrary property list in any of
these places, and you can put just about anything in that.
If you have a situation that is somewhat more complicated, you can use
CFBundleCopyPreferredLocalizationsFromArray to help you out. For
example, if you have a CFDictionary whose keys refer to the
localizations you have available (in any format you like) and whose
values are the values you want to choose among (which could be
anything), then you can get the list of keys, apply
CFBundleCopyPreferredLocalizationsFromArray to that, and if the result
is non-empty, take the 0th element of the result and look it up in your
CFDictionary.
Douglas Davidson