S
SBC Global News Server
I have a situation in which given a specific locale I need to know which
locale from a specific list it best matches. Can anyone suggest a simple
and effective approach?
The reason for this is I have some code which uses a Locale object to
determine what to do. As this code only supports a limited number of
specific locales I must only pass it a locale it likes. Think of it as a
locale being used in a switch like statement. (For purposes of brevity I
won't go into all the reasons I have intentionally done what is probably a
very unusual thing.)
One kludgey approach I have considered is to create a resource bundle with
defined resources for only the specific list of locales plus one for the
default locale. I can then associate a different value for a single
specific key with each resource (be it a properties file or a subclass of
java.util.ResourceBundle).
What I would prefer would be something like the following:
List localeList = new ArrayList();
localeList.add(Locale.US);
localeList.add(Locale.UK);
localeList.add(Locale.GERMANY);
Locale defaultLocale = Locale.US;
LocaleDecider localeDecider = new LocaleDecider(localeList, defaultLocale);
Locale someLocaleTakenFromHTTPRequestHeader = Locale.FRANCE;
Locale bestLocaleChoice =
localeDecider.findBestLocale(someLocaleTakenFromHTTPRequestHeader);
//bestLocaleChoice will be either Locale.US, Locale.UK or Locale.GERMANY.
//The algorithm for deciding which to use should be similar to the way
//a resource bundle works.
Thank you for your time and effort in reading and possibly responding to
this posting.
Sincerely,
James Carpenter
(e-mail address removed) me.com
locale from a specific list it best matches. Can anyone suggest a simple
and effective approach?
The reason for this is I have some code which uses a Locale object to
determine what to do. As this code only supports a limited number of
specific locales I must only pass it a locale it likes. Think of it as a
locale being used in a switch like statement. (For purposes of brevity I
won't go into all the reasons I have intentionally done what is probably a
very unusual thing.)
One kludgey approach I have considered is to create a resource bundle with
defined resources for only the specific list of locales plus one for the
default locale. I can then associate a different value for a single
specific key with each resource (be it a properties file or a subclass of
java.util.ResourceBundle).
What I would prefer would be something like the following:
List localeList = new ArrayList();
localeList.add(Locale.US);
localeList.add(Locale.UK);
localeList.add(Locale.GERMANY);
Locale defaultLocale = Locale.US;
LocaleDecider localeDecider = new LocaleDecider(localeList, defaultLocale);
Locale someLocaleTakenFromHTTPRequestHeader = Locale.FRANCE;
Locale bestLocaleChoice =
localeDecider.findBestLocale(someLocaleTakenFromHTTPRequestHeader);
//bestLocaleChoice will be either Locale.US, Locale.UK or Locale.GERMANY.
//The algorithm for deciding which to use should be similar to the way
//a resource bundle works.
Thank you for your time and effort in reading and possibly responding to
this posting.
Sincerely,
James Carpenter
(e-mail address removed) me.com