Author: athimel Date: 2011-08-03 09:27:28 +0200 (Wed, 03 Aug 2011) New Revision: 1938 Url: http://nuiton.org/repositories/revision/i18n/1938 Log: #1653 : Be able to check is some key is present Modified: trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18n.java trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18nLanguage.java trunk/nuiton-i18n/src/test/java/org/nuiton/i18n/I18nTest.java trunk/nuiton-i18n/src/test/resources/META-INF/I18nStoreTest_fr_FR.properties Modified: trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18n.java =================================================================== --- trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18n.java 2011-08-03 06:47:08 UTC (rev 1937) +++ trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18n.java 2011-08-03 07:27:28 UTC (rev 1938) @@ -191,6 +191,61 @@ } /** + * Look into the default {@link I18nLanguage} if the given {@code message} + * can be found. + * + * @param message the message to check presence + * @return true/false whether the message is present in the default language + * @see #getDefaultLocale() + * @see #hasKey(java.util.Locale, String) + * @since 2.4.1 + */ + public static boolean hasKey(String message) { + + boolean result = false; + + // if the key to find is null, just return false + if (message != null) { + + // checkInit() will be done by 'getDefaultLocale' or 'hasKey' method + + // get current locale (from the language in the store) + Locale locale = getDefaultLocale(); + result = hasKey(locale, message); + } + + return result; + } + + + /** + * Look into the {@link I18nLanguage} associated to the {@code locale} if + * the given {@code message} can be found. + * + * @param locale the locale to be used to get the I18nLanguage + * @param message the message to check presence + * @return true/false whether the message is present for the given locale + * @since 2.4.1 + */ + public static boolean hasKey(Locale locale, String message) { + + boolean result = false; + + // if the key to find is null, just return false + if (message != null) { + + checkInit(); + + // Get the language associated to this locale... + I18nLanguage language = getLanguage(locale); + // ... and check key presence + result = language.hasRecord(message); + } + + return result; + } + + /** * Retourne la chaine traduite si possible dans la locale demandée. * * @param locale la locale dans lequel on souhaite la traduction Modified: trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18nLanguage.java =================================================================== --- trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18nLanguage.java 2011-08-03 06:47:08 UTC (rev 1937) +++ trunk/nuiton-i18n/src/main/java/org/nuiton/i18n/I18nLanguage.java 2011-08-03 07:27:28 UTC (rev 1938) @@ -180,6 +180,7 @@ } try { String result = resource.getProperty(sentence); + // Empty String is also considered as missing if (result == null || "".equals(result)) { recordNotFound(sentence); if (missingKeyReturnNull) { @@ -281,6 +282,17 @@ } } + public boolean hasRecord(String sentence) { + boolean result = false; + if (sentence != null) { + result = resource.containsKey(sentence); + // Empty String is considered as missing + String value = resource.getProperty(sentence); + result &= !"".equals(value); + } + return result; + } + @Override protected void finalize() throws Throwable { super.finalize(); Modified: trunk/nuiton-i18n/src/test/java/org/nuiton/i18n/I18nTest.java =================================================================== --- trunk/nuiton-i18n/src/test/java/org/nuiton/i18n/I18nTest.java 2011-08-03 06:47:08 UTC (rev 1937) +++ trunk/nuiton-i18n/src/test/java/org/nuiton/i18n/I18nTest.java 2011-08-03 07:27:28 UTC (rev 1938) @@ -226,6 +226,8 @@ @Test public void testMissingKeyReturnNull() { + DefaultI18nInitializer initializer = new DefaultI18nInitializer( + I18nStoreTest.class.getSimpleName()); initializer.setMissingKeyReturnNull(false); I18n.init(initializer, Locale.FRANCE); @@ -234,6 +236,8 @@ Assert.assertNotNull(text); Assert.assertEquals(key, text); + initializer = new DefaultI18nInitializer( + I18nStoreTest.class.getSimpleName()); initializer.setMissingKeyReturnNull(true); I18n.init(initializer, Locale.FRANCE); @@ -241,4 +245,23 @@ Assert.assertNull(text); } + @Test + public void testHasKey() { + + I18n.init(initializer, Locale.FRANCE); + + Assert.assertFalse(I18n.hasKey("key.fr.only.missing")); + Assert.assertFalse(I18n.hasKey("key.fr.only.empty")); + Assert.assertTrue(I18n.hasKey("key.fr.only.filled")); + + Assert.assertFalse(I18n.hasKey(Locale.FRANCE, "key.fr.only.missing")); + Assert.assertFalse(I18n.hasKey(Locale.FRANCE, "key.fr.only.empty")); + Assert.assertTrue(I18n.hasKey(Locale.FRANCE, "key.fr.only.filled")); + + Assert.assertFalse(I18n.hasKey(Locale.JAPAN, "key.fr.only.missing")); + Assert.assertFalse(I18n.hasKey(Locale.JAPAN, "key.fr.only.empty")); + Assert.assertFalse(I18n.hasKey(Locale.JAPAN, "key.fr.only.filled")); + + } + } Modified: trunk/nuiton-i18n/src/test/resources/META-INF/I18nStoreTest_fr_FR.properties =================================================================== --- trunk/nuiton-i18n/src/test/resources/META-INF/I18nStoreTest_fr_FR.properties 2011-08-03 06:47:08 UTC (rev 1937) +++ trunk/nuiton-i18n/src/test/resources/META-INF/I18nStoreTest_fr_FR.properties 2011-08-03 07:27:28 UTC (rev 1938) @@ -26,3 +26,5 @@ key.two=Seconde key.with.param=Clé avec %s key.with.date=Clé avec {0,date} +key.fr.only.empty= +key.fr.only.filled=Hello