iterator = remaining.iterator();
while (iterator.hasNext())
{
Locale option = iterator.next();
if (option != null && !templateCountry.equals(option.getCountry()))
{
// It doesn't match language - remove
// Don't remove the iterator. If it matchs a langage but not the country, returns any matched language
// iterator.remove();
}
else
{
lastMatchingOption = option; // Keep a record of the last match
}
}
}
/*if (remaining.isEmpty())
{
return null;
}
else */
if (remaining.size() == 1 && lastMatchingOption != null)
{
return lastMatchingOption;
}
else
{
// We have done an earlier equality check, so there isn't a matching variant
// Also, we know that there are multiple options at this point, either of which will do.
// This gets any country match (there will be worse matches so we take the last the country match)
if(lastMatchingOption != null)
{
return lastMatchingOption;
}
else
{
for (Locale locale : remaining)
{
return locale;
}
}
}
// The logic guarantees that this code can't be called
throw new RuntimeException("Logic should not allow code to get here.");
}
/**
* Factory method to create a Locale from a lang_country_variant string.
*
* @param localeStr e.g. fr_FR
* @return Returns the locale instance, or the {@link Locale#getDefault() default} if the
* string is invalid
*/
public static Locale parseLocale(String localeStr)
{
if(localeStr == null)
{
return null;
}
Locale locale = Locale.getDefault();
StringTokenizer t = new StringTokenizer(localeStr, "_");
int tokens = t.countTokens();
if (tokens == 1)
{
locale = new Locale(t.nextToken());
}
else if (tokens == 2)
{
locale = new Locale(t.nextToken(), t.nextToken());
}
else if (tokens == 3)
{
locale = new Locale(t.nextToken(), t.nextToken(), t.nextToken());
}
return locale;
}
/**
* Register a resource bundle.
*
* This should be the bundle base name eg, alfresco.messages.errors
*
* Once registered the messges will be available via getMessage
*
* @param bundleBaseName the bundle base name
*/
public static void registerResourceBundle(String bundleBaseName)
{
try
{
writeLock.lock();
resouceBundleBaseNames.add(bundleBaseName);
}
finally
{
writeLock.unlock();
}
}
/**
* Get message from registered resource bundle.
*
* @param messageKey message key
* @return localised message string, null if not found
*/
public static String getMessage(String messageKey)
{
return getMessage(messageKey, getLocale());
}
/**
* Get a localised message string
*
* @param messageKey the message key
* @param locale override the current locale
* @return the localised message string, null if not found
*/
public static String getMessage(String messageKey, Locale locale)
{
String message = null;
Map props = getLocaleProperties(locale);
if (props != null)
{
message = props.get(messageKey);
}
return message==null? messageKey:message;
}
/**
* Get a localised message string, parameterized using standard MessageFormatter.
*
* @param messageKey message key
* @param params format parameters
* @return the localised string, null if not found
*/
public static String getMessage(String messageKey, Object ... params)
{
return getMessage(messageKey, getLocale(), params);
}
/**
* Get a localised message string, parameterized using standard MessageFormatter.
*
* @param messageKey the message key
* @param locale override current locale
* @param params the localised message string
* @return the localaised string, null if not found
*/
public static String getMessage(String messageKey, Locale locale, Object ... params)
{
String message = getMessage(messageKey, locale);
if (message != null && params != null)
{
message = MessageFormat.format(message, params);
}
return message;
}
/**
* @return the map of all available messages for the current locale
*/
public static Map getAllMessages()
{
return getLocaleProperties(getLocale());
}
/**
* @return the map of all available messages for the specified locale
*/
public static Map getAllMessages(Locale locale)
{
return getLocaleProperties(locale);
}
/**
* Get the messages for a locale.
*
* Will use cache where available otherwise will load into cache from bundles.
*
* @param locale the locale
* @return message map
*/
private static Map getLocaleProperties(Locale locale)
{
Set loadedBundles = null;
Map props = null;
int loadedBundleCount = 0;
try
{
readLock.lock();
loadedBundles = loadedResourceBundles.get(locale);
props = cachedMessages.get(locale);
loadedBundleCount = resouceBundleBaseNames.size();
}
finally
{
readLock.unlock();
}
if (loadedBundles == null)
{
try
{
writeLock.lock();
loadedBundles = new HashSet();
loadedResourceBundles.put(locale, loadedBundles);
}
finally
{
writeLock.unlock();
}
}
if (props == null)
{
try
{
writeLock.lock();
props = new HashMap();
cachedMessages.put(locale, props);
}
finally
{
writeLock.unlock();
}
}
if (loadedBundles.size() != loadedBundleCount)
{
try
{
writeLock.lock();
for (String resourceBundleBaseName : resouceBundleBaseNames)
{
if (loadedBundles.contains(resourceBundleBaseName) == false)
{
ResourceBundle resourcebundle = ResourceBundle.getBundle(resourceBundleBaseName, locale);
Enumeration enumKeys = resourcebundle.getKeys();
while (enumKeys.hasMoreElements() == true)
{
String key = enumKeys.nextElement();
props.put(key, resourcebundle.getString(key));
}
loadedBundles.add(resourceBundleBaseName);
}
}
}
finally
{
writeLock.unlock();
}
}
return props;
}
}