joerghoh commented on code in PR #17:
URL:
https://github.com/apache/sling-org-apache-sling-i18n/pull/17#discussion_r1808436046
##########
src/test/java/org/apache/sling/i18n/impl/JcrResourceBundleProviderTest.java:
##########
@@ -112,4 +182,58 @@ public void testToLocaleWithPrivateUseCountryCode() {
// Lowercase Private use Country 'xa'
Assert.assertEquals(new Locale(Locale.GERMAN.getLanguage(), "XA"),
JcrResourceBundleProvider.toLocale("de_xa"));
}
+
+ @Test
+ public void testGetParentLocale() {
+ JcrResourceBundleProvider provider = new JcrResourceBundleProvider();
+ String variant = "variant1";
+ String script = "Hans";
+ // Locale with script and variant
+ Locale locale = new Locale.Builder()
+ .setLanguage(Locale.CHINA.getLanguage())
+ .setScript(script)
+ .setRegion(Locale.CHINA.getCountry())
+ .setVariant(variant)
+ .build();
+ Assert.assertEquals(
+ new Locale.Builder()
+ .setLanguage(Locale.CHINA.getLanguage())
+ .setScript(script)
+ .setRegion(Locale.CHINA.getCountry())
+ .build(),
+ provider.getParentLocale(locale));
+
+ // Locale with script and country
+ locale = new Locale.Builder()
+ .setLanguage(Locale.CHINA.getLanguage())
+ .setScript(script)
+ .setRegion(Locale.CHINA.getCountry())
+ .build();
+ Assert.assertEquals(
+ new Locale.Builder()
+ .setLanguage(Locale.CHINA.getLanguage())
+ .setScript(script)
+ .build(),
+ provider.getParentLocale(locale));
+
+ // Locale with script only
+ locale = new Locale.Builder()
+ .setLanguage(Locale.CHINA.getLanguage())
+ .setScript(script)
+ .build();
+ Assert.assertEquals(new Locale(Locale.CHINA.getLanguage()),
provider.getParentLocale(locale));
+
+ // Locale with variant only
+ locale = new Locale(Locale.CHINA.getLanguage(),
Locale.CHINA.getCountry(), variant);
+ Assert.assertEquals(
+ new Locale(Locale.CHINA.getLanguage(),
Locale.CHINA.getCountry()), provider.getParentLocale(locale));
+
+ // Locale with country only
+ locale = new Locale(Locale.CHINA.getLanguage(),
Locale.CHINA.getCountry());
+ Assert.assertEquals(new Locale(Locale.CHINA.getLanguage()),
provider.getParentLocale(locale));
+
+ // Locale with language only
+ locale = new Locale(Locale.CHINA.getLanguage());
+ Assert.assertEquals(provider.getDefaultLocale(),
provider.getParentLocale(locale));
Review Comment:
To complete the coverage of the important (non-exception handling) branches
I would add one additional validation.
```suggestion
Assert.assertEquals(provider.getDefaultLocale(),
provider.getParentLocale(locale));
// The parent of the default locale is null
Assert.assertNull(provider.getParentLocale(provider.getDefaultLocale()));
```
##########
src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java:
##########
@@ -565,15 +573,37 @@ private JcrResourceBundle createResourceBundle(
* returned.</li>
* </ol>
*/
- private Locale getParentLocale(Locale locale) {
- if (locale.getVariant().length() != 0) {
+ protected Locale getParentLocale(Locale locale) {
+ if (!locale.getScript().isEmpty() && !locale.getVariant().isEmpty()) {
+ try {
+ return new Locale.Builder()
+ .setLanguage(locale.getLanguage())
+ .setRegion(locale.getCountry())
+ .setScript(locale.getScript())
+ .build();
+ } catch (IllformedLocaleException e) {
+ // should never happen, as all elements already come from a
valid locale object
+ return new Locale(locale.getLanguage(), locale.getCountry());
+ }
+ } else if (!locale.getScript().isEmpty() &&
!locale.getCountry().isEmpty()) {
+ try {
+ return new Locale.Builder()
+ .setLanguage(locale.getLanguage())
+ .setScript(locale.getScript())
+ .build();
+ } catch (IllformedLocaleException e) {
+ // should never happen, as all elements already come from a
valid locale object
+ return new Locale(locale.getLanguage());
+ }
+ } else if (!locale.getScript().isEmpty()) {
+ return new Locale(locale.getLanguage());
+ } else if (!locale.getVariant().isEmpty()) {
return new Locale(locale.getLanguage(), locale.getCountry());
- } else if (locale.getCountry().length() != 0) {
+ } else if (!locale.getCountry().isEmpty()) {
return new Locale(locale.getLanguage());
} else if (!locale.getLanguage().equals(defaultLocale.getLanguage())) {
return defaultLocale;
}
-
// no more parents
Review Comment:
```suggestion
// the default locale has no parent locale
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]