mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
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:
@@ -20,7 +20,6 @@ import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.service.cmr.dictionary.DictionaryService;
|
||||
import org.alfresco.service.cmr.repository.MLText;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
|
@@ -19,7 +19,6 @@ package org.alfresco.service.cmr.repository;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
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
|
||||
* @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
|
||||
* 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)
|
||||
{
|
||||
@@ -149,34 +149,31 @@ public class MLText extends HashMap<Locale, String>
|
||||
{
|
||||
return null;
|
||||
}
|
||||
// Is there an exact match?
|
||||
if (containsKey(locale))
|
||||
// Use the available keys as options
|
||||
Set<Locale> options = keySet();
|
||||
// Get a match
|
||||
Locale match = I18NUtil.getNearestLocale(locale, options);
|
||||
if (match == null)
|
||||
{
|
||||
return get(locale);
|
||||
}
|
||||
// Hunt for a similar language
|
||||
Map.Entry<Locale, String> lastEntry = null;
|
||||
for (Map.Entry<Locale, String> entry : this.entrySet())
|
||||
// No close matches for the locale - go for the default locale
|
||||
locale = defaultLocale;
|
||||
match = I18NUtil.getNearestLocale(locale, options);
|
||||
if (match == null)
|
||||
{
|
||||
lastEntry = entry; // Keep in case we need an arbitrary value later
|
||||
Locale mapLocale = entry.getKey();
|
||||
if (mapLocale == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (mapLocale.getLanguage().equals(locale.getLanguage()))
|
||||
{
|
||||
// we found a language match
|
||||
return entry.getValue();
|
||||
// just get any locale
|
||||
match = I18NUtil.getNearestLocale(null, options);
|
||||
}
|
||||
}
|
||||
// Nothing found. What about locale as per constructor?
|
||||
if (containsKey(defaultLocale))
|
||||
// Did we get a match
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -45,7 +45,7 @@ public class MLTextTest extends TestCase
|
||||
assertNull("Expected nothing for German", mlText.getValue(Locale.GERMAN));
|
||||
assertEquals(Locale.US.toString(), mlText.get(Locale.US));
|
||||
assertEquals(Locale.UK.toString(), mlText.get(Locale.UK));
|
||||
assertNull("Expected nothing for French general", mlText.getValue(Locale.FRENCH));
|
||||
// assertEquals("Expected Canada French to be found", Locale.CANADA_FRENCH.toString(),
|
||||
assertNull("Expected no value for Japanese", mlText.getValue(Locale.JAPANESE));
|
||||
assertNotNull("Expected an arbirary value for Japanese", mlText.getClosestValue(Locale.JAPANESE));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user