From 378cae0821bbd6979e14aa0edb7b8a703c1f34f9 Mon Sep 17 00:00:00 2001 From: Patryk Date: Thu, 22 Feb 2018 21:45:27 +0100 Subject: [PATCH] [BAEL-1536] A Guide to the Resource Bundle (#3602) * [BAEL-1536] A Guide to the Resource Bundle The essential code for the article * [BAEL-1536] A Guide to the Resource Bundle #2 The essential code for the article after the first article review. * [BAEL-1536] A Guide to the Resource Bundle #3 Essential changes after the second review. * [BAEL-1536] A Guide to the Resource Bundle #4 Essential changes after the second review: - Doubles to BigDecimals --- .../resourcebundle/ExampleControl.java | 15 +++++ .../resourcebundle/ExampleResource.java | 14 +++++ .../resourcebundle/ExampleResource_pl.java | 15 +++++ .../resourcebundle/ExampleResource_pl_PL.java | 17 +++++ .../resourcebundle/resource.properties | 6 ++ .../resourcebundle/resource_en.properties | 1 + .../resourcebundle/resource_pl_PL.properties | 3 + .../ExampleResourceUnitTest.java | 39 ++++++++++++ .../PropertyResourceUnitTest.java | 62 +++++++++++++++++++ 9 files changed, 172 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/resourcebundle/ExampleControl.java create mode 100644 core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource.java create mode 100644 core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource_pl.java create mode 100644 core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource_pl_PL.java create mode 100644 core-java/src/main/resources/resourcebundle/resource.properties create mode 100644 core-java/src/main/resources/resourcebundle/resource_en.properties create mode 100644 core-java/src/main/resources/resourcebundle/resource_pl_PL.properties create mode 100644 core-java/src/test/java/com/baeldung/resourcebundle/ExampleResourceUnitTest.java create mode 100644 core-java/src/test/java/com/baeldung/resourcebundle/PropertyResourceUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/resourcebundle/ExampleControl.java b/core-java/src/main/java/com/baeldung/resourcebundle/ExampleControl.java new file mode 100644 index 0000000000..88ce36c27f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/resourcebundle/ExampleControl.java @@ -0,0 +1,15 @@ +package com.baeldung.resourcebundle; + +import java.util.Arrays; +import java.util.List; +import java.util.Locale; +import java.util.ResourceBundle; + +public class ExampleControl extends ResourceBundle.Control { + + @Override + public List getCandidateLocales(String s, Locale locale) { + return Arrays.asList(new Locale("pl", "PL")); + } + +} diff --git a/core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource.java b/core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource.java new file mode 100644 index 0000000000..52ccc80f52 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource.java @@ -0,0 +1,14 @@ +package com.baeldung.resourcebundle; + +import java.util.ListResourceBundle; + +public class ExampleResource extends ListResourceBundle { + + @Override + protected Object[][] getContents() { + return new Object[][] { + { "greeting", "hello" } + }; + } + +} diff --git a/core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource_pl.java b/core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource_pl.java new file mode 100644 index 0000000000..7d4f11a5d3 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource_pl.java @@ -0,0 +1,15 @@ +package com.baeldung.resourcebundle; + +import java.util.ListResourceBundle; + +public class ExampleResource_pl extends ListResourceBundle { + + @Override + protected Object[][] getContents() { + return new Object[][] { + { "greeting", "cześć" }, + { "language", "polish" }, + }; + } + +} diff --git a/core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource_pl_PL.java b/core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource_pl_PL.java new file mode 100644 index 0000000000..252bfcb9cf --- /dev/null +++ b/core-java/src/main/java/com/baeldung/resourcebundle/ExampleResource_pl_PL.java @@ -0,0 +1,17 @@ +package com.baeldung.resourcebundle; + +import java.math.BigDecimal; +import java.util.ListResourceBundle; + +public class ExampleResource_pl_PL extends ListResourceBundle { + + @Override + protected Object[][] getContents() { + return new Object[][] { + { "currency", "polish zloty" }, + { "toUsdRate", new BigDecimal("3.401") }, + { "cities", new String[] { "Warsaw", "Cracow" } } + }; + } + +} diff --git a/core-java/src/main/resources/resourcebundle/resource.properties b/core-java/src/main/resources/resourcebundle/resource.properties new file mode 100644 index 0000000000..edeae3ef4b --- /dev/null +++ b/core-java/src/main/resources/resourcebundle/resource.properties @@ -0,0 +1,6 @@ +# Buttons +cancelButton=cancel +continueButton continue + +! Labels +helloLabel:hello diff --git a/core-java/src/main/resources/resourcebundle/resource_en.properties b/core-java/src/main/resources/resourcebundle/resource_en.properties new file mode 100644 index 0000000000..5d72b65e2f --- /dev/null +++ b/core-java/src/main/resources/resourcebundle/resource_en.properties @@ -0,0 +1 @@ +deleteButton=delete \ No newline at end of file diff --git a/core-java/src/main/resources/resourcebundle/resource_pl_PL.properties b/core-java/src/main/resources/resourcebundle/resource_pl_PL.properties new file mode 100644 index 0000000000..37dea822ad --- /dev/null +++ b/core-java/src/main/resources/resourcebundle/resource_pl_PL.properties @@ -0,0 +1,3 @@ +backButton=cofnij +helloLabel=cze\u015b\u0107 +helloLabelNoEncoding=cześć \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/resourcebundle/ExampleResourceUnitTest.java b/core-java/src/test/java/com/baeldung/resourcebundle/ExampleResourceUnitTest.java new file mode 100644 index 0000000000..1ce4f1bb8f --- /dev/null +++ b/core-java/src/test/java/com/baeldung/resourcebundle/ExampleResourceUnitTest.java @@ -0,0 +1,39 @@ +package com.baeldung.resourcebundle; + +import org.junit.Test; + +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.Locale; +import java.util.ResourceBundle; + +import static org.junit.Assert.*; + +public class ExampleResourceUnitTest { + + @Test + public void whenGetBundleExampleResourceForLocalePlPl_thenItShouldInheritPropertiesGreetingAndLanguage() { + Locale plLocale = new Locale("pl", "PL"); + + ResourceBundle exampleBundle = ResourceBundle.getBundle("com.baeldung.resourcebundle.ExampleResource", plLocale); + + assertTrue(exampleBundle.keySet() + .containsAll(Arrays.asList("toUsdRate", "cities", "greeting", "currency", "language"))); + assertEquals(exampleBundle.getString("greeting"), "cześć"); + assertEquals(exampleBundle.getObject("toUsdRate"), new BigDecimal("3.401")); + assertArrayEquals(exampleBundle.getStringArray("cities"), new String[] { "Warsaw", "Cracow" }); + } + + @Test + public void whenGetBundleExampleResourceForLocaleUs_thenItShouldContainOnlyGreeting() { + Locale usLocale = Locale.US; + + ResourceBundle exampleBundle = ResourceBundle.getBundle("com.baeldung.resourcebundle.ExampleResource", usLocale); + + assertFalse(exampleBundle.keySet() + .containsAll(Arrays.asList("toUsdRate", "cities", "currency", "language"))); + assertTrue(exampleBundle.keySet() + .containsAll(Arrays.asList("greeting"))); + } + +} diff --git a/core-java/src/test/java/com/baeldung/resourcebundle/PropertyResourceUnitTest.java b/core-java/src/test/java/com/baeldung/resourcebundle/PropertyResourceUnitTest.java new file mode 100644 index 0000000000..4da71567e1 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/resourcebundle/PropertyResourceUnitTest.java @@ -0,0 +1,62 @@ +package com.baeldung.resourcebundle; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.Locale; +import java.util.ResourceBundle; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class PropertyResourceUnitTest { + + @Test + public void givenLocaleUsAsDefualt_whenGetBundleForLocalePlPl_thenItShouldContain3ButtonsAnd1Label() { + Locale.setDefault(Locale.US); + + ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("pl", "PL")); + + assertTrue(bundle.keySet() + .containsAll(Arrays.asList("backButton", "helloLabel", "cancelButton", "continueButton", "helloLabelNoEncoding"))); + } + + @Test + public void givenLocaleUsAsDefualt_whenGetBundleForLocaleFrFr_thenItShouldContainKeys1To3AndKey4() { + Locale.setDefault(Locale.US); + + ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("fr", "FR")); + + assertTrue(bundle.keySet() + .containsAll(Arrays.asList("deleteButton", "helloLabel", "cancelButton", "continueButton"))); + } + + @Test + public void givenLocaleChinaAsDefualt_whenGetBundleForLocaleFrFr_thenItShouldOnlyContainKeys1To3() { + Locale.setDefault(Locale.CHINA); + + ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("fr", "FR")); + + assertTrue(bundle.keySet() + .containsAll(Arrays.asList("continueButton", "helloLabel", "cancelButton"))); + } + + @Test + public void givenLocaleChinaAsDefualt_whenGetBundleForLocaleFrFrAndExampleControl_thenItShouldOnlyContainKey5() { + Locale.setDefault(Locale.CHINA); + + ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("fr", "FR"), new ExampleControl()); + + assertTrue(bundle.keySet() + .containsAll(Arrays.asList("backButton", "helloLabel"))); + } + + @Test + public void givenValuesDifferentlyEncoded_whenGetBundleForLocalePlPl_thenItShouldContain3ButtonsAnd1Label() { + ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("pl", "PL")); + + assertEquals(bundle.getString("helloLabel"), "cześć"); + assertEquals(bundle.getString("helloLabelNoEncoding"), "czeÅ\u009BÄ\u0087"); + } + +}