From 7e8a24b7a52b1ed969c3bab57773e4439aab9e6b Mon Sep 17 00:00:00 2001 From: Derek Hulley Date: Thu, 7 Dec 2006 14:10:17 +0000 Subject: [PATCH] Locale matching git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@4551 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../repo/node/MLPropertyInterceptor.java | 1 - .../service/cmr/repository/MLText.java | 45 +++++++++---------- .../service/cmr/repository/MLTextTest.java | 4 +- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/source/java/org/alfresco/repo/node/MLPropertyInterceptor.java b/source/java/org/alfresco/repo/node/MLPropertyInterceptor.java index 458528bf7c..bf34ea6b1e 100644 --- a/source/java/org/alfresco/repo/node/MLPropertyInterceptor.java +++ b/source/java/org/alfresco/repo/node/MLPropertyInterceptor.java @@ -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; diff --git a/source/java/org/alfresco/service/cmr/repository/MLText.java b/source/java/org/alfresco/service/cmr/repository/MLText.java index 4205761c48..c1e809401e 100644 --- a/source/java/org/alfresco/service/cmr/repository/MLText.java +++ b/source/java/org/alfresco/service/cmr/repository/MLText.java @@ -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 * @param locale the locale to use as the starting point of the value search * @return Returns a default String value or null if one isn't available. * null will only be returned if there are no values associated with - * this instance. With or without a match, the return value may be null. + * this instance. With or without a match, the return value may be null, + * depending on the values associated with the locales. */ public String getClosestValue(Locale locale) { @@ -149,34 +149,31 @@ public class MLText extends HashMap { return null; } - // Is there an exact match? - if (containsKey(locale)) + // Use the available keys as options + Set options = keySet(); + // Get a match + Locale match = I18NUtil.getNearestLocale(locale, options); + if (match == null) { - return get(locale); - } - // Hunt for a similar language - Map.Entry lastEntry = null; - for (Map.Entry entry : this.entrySet()) - { - lastEntry = entry; // Keep in case we need an arbitrary value later - Locale mapLocale = entry.getKey(); - if (mapLocale == null) + // No close matches for the locale - go for the default locale + locale = defaultLocale; + match = I18NUtil.getNearestLocale(locale, options); + if (match == 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(); } /** diff --git a/source/java/org/alfresco/service/cmr/repository/MLTextTest.java b/source/java/org/alfresco/service/cmr/repository/MLTextTest.java index 8248be4439..902f2daadb 100644 --- a/source/java/org/alfresco/service/cmr/repository/MLTextTest.java +++ b/source/java/org/alfresco/service/cmr/repository/MLTextTest.java @@ -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)); } }