Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding optional fallback to root language bundle #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cal10n-api/src/main/java/ch/qos/cal10n/Settings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ch.qos.cal10n;

public class Settings
{
private static boolean rootLanguageFallbackEnabled = false;

public static void setRootLanguageFallbackEnabled(boolean enabled)
{
rootLanguageFallbackEnabled = enabled;
}

public static boolean isRootLanguageFallbackEnabled()
{
return rootLanguageFallbackEnabled ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import java.net.URLConnection;
import java.util.Locale;

import ch.qos.cal10n.Settings;

/**
* @since 0.8.1
*/
public abstract class AbstractCAL10NBundleFinder implements CAL10NBundleFinder {

public CAL10NBundle getBundle(String baseName, Locale locale, String charset) {

// same as the JDK convention
Expand All @@ -25,19 +27,36 @@ public CAL10NBundle getBundle(String baseName, Locale locale, String charset) {
baseName, locale);
String languageOnlyCandidate = computeLanguageOnlyCandidate(baseName,
locale);
String noLanguageCandidate = computeNoLanguageCandidate(baseName);

final boolean rootLanguageFallbackEnabled = Settings.isRootLanguageFallbackEnabled();
CAL10NBundle cprbNoLanguage = null;
if (rootLanguageFallbackEnabled) {
cprbNoLanguage = makePropertyResourceBundle(noLanguageCandidate, charset);
}

CAL10NBundle cprbLanguageOnly = makePropertyResourceBundle(languageOnlyCandidate, charset);
CAL10NBundle cprbLanguageAndCountry = null;

if (languageAndCountryCandidate != null) {
cprbLanguageAndCountry = makePropertyResourceBundle(languageAndCountryCandidate, charset);
}

if (rootLanguageFallbackEnabled && cprbNoLanguage != null && cprbLanguageOnly != null) {
cprbLanguageOnly.setParent(cprbNoLanguage);
}

if (cprbLanguageAndCountry != null) {
cprbLanguageAndCountry.setParent(cprbLanguageOnly);
if (cprbLanguageOnly != null) {
cprbLanguageAndCountry.setParent(cprbLanguageOnly);
} else if (rootLanguageFallbackEnabled && cprbNoLanguage != null) {
cprbLanguageAndCountry.setParent(cprbNoLanguage);
}
return cprbLanguageAndCountry;
} else if (cprbLanguageOnly != null) {
return cprbLanguageOnly;
}
return cprbLanguageOnly;
return cprbNoLanguage;
}

private String computeLanguageAndCountryCandidate(String baseName,
Expand Down Expand Up @@ -81,6 +100,10 @@ private String computeLanguageOnlyCandidate(String baseName,
return baseName + "_" + language + ".properties";
}

private String computeNoLanguageCandidate(String baseName) {
return baseName + ".properties";
}

Reader toReader(InputStream in, String charset) {
if (charset == null || charset.length() == 0)
return new InputStreamReader(in);
Expand Down
12 changes: 11 additions & 1 deletion cal10n-api/src/main/java/ch/qos/cal10n/util/CAL10NBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,22 @@ public void resetCheckTimes() {
nextCheck = 0;
lastModified = 0;
}

protected Map<String, String> getMap() {
if(parent == null) {
return this.map;
} else {
Hashtable<String, String> ht = new Hashtable<String, String>(map);
ht.putAll(parent.getMap());
return ht;
}
}

@Override
public Enumeration<String> getKeys() {
Hashtable<String, String> ht = new Hashtable<String, String>(map);
if(parent != null) {
ht.putAll(parent.map);
ht.putAll(parent.getMap());
}
return ht.keys();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package ch.qos.cal10n.verifier;

import ch.qos.cal10n.Settings;
import ch.qos.cal10n.util.AbstractCAL10NBundleFinder;
import ch.qos.cal10n.util.AnnotationExtractor;
import ch.qos.cal10n.util.CAL10NBundleFinder;
import ch.qos.cal10n.util.MiscUtil;

import static ch.qos.cal10n.verifier.Cal10nError.ErrorType.MISSING_LOCALE_DATA_ANNOTATION;
import static ch.qos.cal10n.verifier.Cal10nError.ErrorType.MISSING_BN_ANNOTATION;



import java.util.*;

/**
Expand Down Expand Up @@ -140,6 +142,12 @@ public List<Cal10nError> verifyAllLocales() {
return errorList;
}
for (String localeName : localeNameArray) {
if ("".equals(localeName)) {
Settings.setRootLanguageFallbackEnabled(true);
} else {
//false is the default, but we might want to 'unset' it if the previous iteration of this loop set it to true.
Settings.setRootLanguageFallbackEnabled(false);
}
Locale locale = MiscUtil.toLocale(localeName);
List<Cal10nError> tmpList = verify(locale);
errorList.addAll(tmpList);
Expand Down