Re: comparing Locales for equality and degree of specificity
tom forsmo wrote:
Hi
I have the following comparison problem with regards to Locale.
I need to compare two Locales and find out if they are related and which
is more specific than the other. In the same way as a ResourceBundle
finds the most specific resource property file. E.g for
locale 1: nb_NO
locale 2: NO
locale 1 is the most specific of them. locales of two unrelated
countries/languages should be treated as different locales no matter. So
locale 1: nb_NO
locale 2: UK
are different and should not be reported as that instead of locale 1
being more specific.
Any ideas if there is an implementation that does this out there?
No idea sorry.
Isn't it a case of comparing strings?
Of course, you may also have to decide which of the following is "more
specific"
de_CH
fr_CH
fr_FR
fr_FR_Loire
You're saying that region is more "specific" than language, I'm not sure
I agree, surely fr_CH is in some senses more specific than fr_FR.
You might also need to decide if PS is more specific than IL, a
contentious point. How about IM and GB? GS and GB? UM and US?
Maybe your rule is: If one string is a substring of the other then the
longest string is most specific. In which case it ought to be reasonably
straightforward to code.
All the locales on my JVM appear to be of one of three forms:
language
language_REGION
language_REGION_Variation
There are no cases of
REGION
suggesting your NO and UK are unlikely to occur as locales? (UK would
anyway typically be GB in locales)
public class ListLocales {
public static void main(String[] args) {
Locale[] localeList = NumberFormat.getAvailableLocales();
String[] localeStringList = new String[localeList.length];
for (int i = 0; i < localeList.length; i++ ) {
localeStringList[i] = localeList[i].toString();
}
Arrays.sort(localeStringList);
for (int i = 0; i < localeStringList.length; i++ ) {
System.out.println(localeStringList[i]);
}
}
}
Just my en_GB 0.02 worth