diff --git a/src/main/java/org/meeuw/i18n/languages/ISO_639.java b/src/main/java/org/meeuw/i18n/languages/ISO_639.java index 09d54bc..d9e1c4d 100644 --- a/src/main/java/org/meeuw/i18n/languages/ISO_639.java +++ b/src/main/java/org/meeuw/i18n/languages/ISO_639.java @@ -123,10 +123,19 @@ public static Stream stream() { private static final ThreadLocal> FALLBACKS = ThreadLocal.withInitial(HashMap::new); + static final ThreadLocal> LC_FALLBACKS = ThreadLocal.withInitial(HashMap::new); + + public static void registerFallback(String code, ISO_639_Code exemption) { FALLBACKS.get().put(code, exemption); } + + static void setFallback(Map exemptions) { + FALLBACKS.set(Collections.unmodifiableMap(exemptions)); + } + + public static Map getFallBacks() { return FALLBACKS.get(); } diff --git a/src/main/java/org/meeuw/i18n/languages/LanguageCode.java b/src/main/java/org/meeuw/i18n/languages/LanguageCode.java index 2b5ef63..8c789c3 100644 --- a/src/main/java/org/meeuw/i18n/languages/LanguageCode.java +++ b/src/main/java/org/meeuw/i18n/languages/LanguageCode.java @@ -12,6 +12,8 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; +import static org.meeuw.i18n.languages.ISO_639.LC_FALLBACKS; + /** * A language with a ISO 639-3 language code (of three letters). Also, aware of the ISO-630-1 2 letter codes if that exist. *

@@ -73,9 +75,26 @@ public interface LanguageCode extends ISO_639_Code { + static void setFallback(Map exemptions) { + LC_FALLBACKS.set(Collections.unmodifiableMap(exemptions)); + } + + + static void registerFallback(String code, LanguageCode exemption) { + LC_FALLBACKS.get().put(code, exemption); + } + + static Map getFallBacks() { + return LC_FALLBACKS.get(); + } + + static void resetFallBacks() { + LC_FALLBACKS.remove(); + } + /** - * Retrieves a {@link ISO_639_3_Code} by on of its three-letter identifiers {@link ISO_639#getByPart3(String)}, {@link ISO_639#getByPart2B(String)}, or {@link ISO_639#getByPart2T(String)} or its two letter identifier {@link #part1()}. + * Retrieves a {@link ISO_639_3_Code} by on of its three-letter identifiers {@link ISO_639#getByPart3(String)}, {@link ISO_639#getByPart2B(String)}, or {@link ISO_639#getByPart2T(String)} or its two letter identifier {@link #part1()}. * * @param code A 2 or 3 letter language code * @return An optional containing the {@link ISO_639_3_Code} if found. @@ -86,23 +105,30 @@ public interface LanguageCode extends ISO_639_Code { * @since 0.2 */ static Optional get(String code, boolean matchRetired) { + Optional optional; if (code.length() == 2) { - return getByPart1(code); + optional = getByPart1(code); } else { Optional byPart3 = ISO_639.getByPart3(code, matchRetired); if (byPart3.isPresent()) { - return byPart3; + optional = byPart3; } else { Optional byPart2B = ISO_639.getByPart2B(code); if (byPart2B.isPresent()) { - return byPart2B; + optional = byPart2B; } else { - return ISO_639.getByPart2T(code); + optional = ISO_639.getByPart2T(code); } } } + if (optional.isPresent()) { + return optional; + } else { + return Optional.ofNullable(LC_FALLBACKS.get().get(code)); + } } + static Optional get(String code) { return get(code, true); }