Skip to content

Commit

Permalink
Ported from branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
mihxil committed Mar 5, 2024
1 parent 1b5b03a commit 9abd757
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/main/java/org/meeuw/i18n/languages/ISO_639.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,19 @@ public static Stream<ISO_639_Code> stream() {

private static final ThreadLocal<Map<String, ISO_639_Code>> FALLBACKS = ThreadLocal.withInitial(HashMap::new);

static final ThreadLocal<Map<String, LanguageCode>> 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<String, ISO_639_Code> exemptions) {
FALLBACKS.set(Collections.unmodifiableMap(exemptions));
}


public static Map<String, ISO_639_Code> getFallBacks() {
return FALLBACKS.get();
}
Expand Down
36 changes: 31 additions & 5 deletions src/main/java/org/meeuw/i18n/languages/LanguageCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*<p>
Expand Down Expand Up @@ -73,9 +75,26 @@ public interface LanguageCode extends ISO_639_Code {



static void setFallback(Map<String, LanguageCode> exemptions) {
LC_FALLBACKS.set(Collections.unmodifiableMap(exemptions));
}


static void registerFallback(String code, LanguageCode exemption) {
LC_FALLBACKS.get().put(code, exemption);
}

static Map<String, LanguageCode> 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.
Expand All @@ -86,23 +105,30 @@ public interface LanguageCode extends ISO_639_Code {
* @since 0.2
*/
static Optional<LanguageCode> get(String code, boolean matchRetired) {
Optional<LanguageCode> optional;
if (code.length() == 2) {
return getByPart1(code);
optional = getByPart1(code);
} else {
Optional<LanguageCode> byPart3 = ISO_639.getByPart3(code, matchRetired);
if (byPart3.isPresent()) {
return byPart3;
optional = byPart3;
} else {
Optional<LanguageCode> 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<LanguageCode> get(String code) {
return get(code, true);
}
Expand Down

0 comments on commit 9abd757

Please sign in to comment.