Locale matching

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@4551 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2006-12-07 14:10:17 +00:00
parent c012545bfa
commit 7e8a24b7a5
3 changed files with 23 additions and 27 deletions

View File

@@ -20,7 +20,6 @@ import java.io.Serializable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.repository.MLText; import org.alfresco.service.cmr.repository.MLText;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;

View File

@@ -19,7 +19,6 @@ package org.alfresco.service.cmr.repository;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.Set; import java.util.Set;
import org.alfresco.i18n.I18NUtil; import org.alfresco.i18n.I18NUtil;
@@ -141,7 +140,8 @@ public class MLText extends HashMap<Locale, String>
* @param locale the locale to use as the starting point of the value search * @param locale the locale to use as the starting point of the value search
* @return Returns a default <tt>String</tt> value or null if one isn't available. * @return Returns a default <tt>String</tt> value or null if one isn't available.
* <tt>null</tt> will only be returned if there are no values associated with * <tt>null</tt> will only be returned if there are no values associated with
* this instance. With or without a match, the return value may be <tt>null</tt>. * this instance. With or without a match, the return value may be <tt>null</tt>,
* depending on the values associated with the locales.
*/ */
public String getClosestValue(Locale locale) public String getClosestValue(Locale locale)
{ {
@@ -149,34 +149,31 @@ public class MLText extends HashMap<Locale, String>
{ {
return null; return null;
} }
// Is there an exact match? // Use the available keys as options
if (containsKey(locale)) Set<Locale> options = keySet();
// Get a match
Locale match = I18NUtil.getNearestLocale(locale, options);
if (match == null)
{ {
return get(locale); // No close matches for the locale - go for the default locale
} locale = defaultLocale;
// Hunt for a similar language match = I18NUtil.getNearestLocale(locale, options);
Map.Entry<Locale, String> lastEntry = null; if (match == null)
for (Map.Entry<Locale, String> entry : this.entrySet())
{
lastEntry = entry; // Keep in case we need an arbitrary value later
Locale mapLocale = entry.getKey();
if (mapLocale == null)
{ {
continue; // just get any locale
} match = I18NUtil.getNearestLocale(null, options);
if (mapLocale.getLanguage().equals(locale.getLanguage()))
{
// we found a language match
return entry.getValue();
} }
} }
// Nothing found. What about locale as per constructor? // Did we get a match
if (containsKey(defaultLocale)) if (match == null)
{ {
return get(defaultLocale); // We could find no locale matches
return null;
}
else
{
return get(match);
} }
// Still nothing. Just get a value.
return lastEntry.getValue();
} }
/** /**

View File

@@ -45,7 +45,7 @@ public class MLTextTest extends TestCase
assertNull("Expected nothing for German", mlText.getValue(Locale.GERMAN)); assertNull("Expected nothing for German", mlText.getValue(Locale.GERMAN));
assertEquals(Locale.US.toString(), mlText.get(Locale.US)); assertEquals(Locale.US.toString(), mlText.get(Locale.US));
assertEquals(Locale.UK.toString(), mlText.get(Locale.UK)); assertEquals(Locale.UK.toString(), mlText.get(Locale.UK));
assertNull("Expected nothing for French general", mlText.getValue(Locale.FRENCH)); assertNull("Expected no value for Japanese", mlText.getValue(Locale.JAPANESE));
// assertEquals("Expected Canada French to be found", Locale.CANADA_FRENCH.toString(), assertNotNull("Expected an arbirary value for Japanese", mlText.getClosestValue(Locale.JAPANESE));
} }
} }