Skip to content

Commit

Permalink
Some systems like to provide their own standard, so offer to work aru…
Browse files Browse the repository at this point in the history
…nd that.
  • Loading branch information
mihxil committed Mar 5, 2024
1 parent 2cb8e84 commit 8a2f115
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 35 deletions.
43 changes: 29 additions & 14 deletions src/main/java/org/meeuw/i18n/languages/ISO_639.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package org.meeuw.i18n.languages;

import com.fasterxml.jackson.annotation.JsonCreator;
import jakarta.validation.constraints.Size;
import java.util.*;
import java.util.stream.Stream;

import jakarta.validation.constraints.Size;

import com.fasterxml.jackson.annotation.JsonCreator;

import static org.meeuw.i18n.languages.ISO_639_3_Code.KNOWN;

/**
* A utility class for working with ISO 639 language codes.
*
*
* @since 3.1
*/

public class ISO_639 {

private ISO_639() {
}
/**
Expand All @@ -21,7 +25,7 @@ private ISO_639() {
* @param code A 2 letter language code
* @return An optional containing the {@link ISO_639_3_Code} if found.
*/

public static Optional<LanguageCode> getByPart1(String code) {
return ISO_639_3_Code
.getByPart1(code)
Expand Down Expand Up @@ -72,7 +76,7 @@ public static Optional<LanguageCode> getByPart2T(String code) {
* @since 2.2
*/
public static Optional<LanguageCode> getByPart3(@Size(min = 3, max = 3) String code, boolean matchRetired) {

return ISO_639_3_Code.getByPart3(code, matchRetired)
.map(LanguageCode::updateToEnum)
;
Expand All @@ -86,18 +90,18 @@ public static Optional<LanguageCode> getByPart3(@Size(min = 3, max = 3) String c
}

/**
* Retrieves a language family code by its 3 letter code.
*
* @see LanguageFamilyCode#get(String)
* Retrieves a language family code by its 3-letter code.
*
* @see LanguageFamilyCode#get(String)
*/
public static Optional<LanguageFamilyCode> getByPart5(@Size(min = 3, max = 3) String code) {

return LanguageFamilyCode.get(code);
}

/**
* A stream with all known {@link ISO_639_Code language (or language family) codes} .
*
*
*
* @return a stream of all known language codes.
*/
Expand All @@ -116,18 +120,29 @@ public static Stream<ISO_639_Code> stream() {
);
}


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

public static void registerFallback(String code, ISO_639_Code exemption) {
FALLBACKS.put(code, exemption);
}

public static Map<String, ISO_639_Code> getFallBacks() {
return FALLBACKS;
}

/**
* Obtains a language or language family by (one of their) code(s).
*
* @see #get For a version that throws an exception if not found.
*
* @see #get For a version that throws an exception if not found.
*/
public static Optional<ISO_639_Code> get(String code) {
ISO_639_Code lc = LanguageCode.get(code).orElse(null);
if (lc == null) {
try {
return Optional.of(LanguageFamilyCode.valueOf(code));
} catch (IllegalArgumentException iae) {
return Optional.empty();
return Optional.ofNullable(FALLBACKS.get(code));
}
} else {
return Optional.of(lc);
Expand Down
45 changes: 24 additions & 21 deletions src/main/java/org/meeuw/i18n/languages/ISO_639_3_Code.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
package org.meeuw.i18n.languages;

import com.fasterxml.jackson.annotation.JsonValue;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

import com.fasterxml.jackson.annotation.JsonValue;

/**
* Implementation of {@link LanguageCode} that {@link ISO_639#stream() produces} all ISO-639-3 codes.
* <p>
* Normally it makes sense to just use {@link LanguageCode}.
* Normally it makes sense to just use {@link LanguageCode}.
*/
public class ISO_639_3_Code implements LanguageCode {

final static Logger LOGGER = Logger.getLogger(LanguageCode.class.getName());


static final Map<String, ISO_639_3_Code> KNOWN;


static final Map<LanguageCode, List<LanguageCode>> INDIVIDUAL_LANGUAGES;

static final Map<LanguageCode, List<LanguageCode>> MACRO;


Expand Down Expand Up @@ -74,7 +76,7 @@ public class ISO_639_3_Code implements LanguageCode {
throw new ExceptionInInitializerError(e);
}
KNOWN = Collections.unmodifiableMap(temp);

Map<LanguageCode, List<LanguageCode>> tempIndividual = new HashMap<>();
Map<LanguageCode, List<LanguageCode>> tempMacro = new HashMap<>();
try (InputStream inputStream = ISO_639_3_Code.class.getResourceAsStream(DIR + "iso-639-3-macrolanguages.tab");
Expand All @@ -100,34 +102,35 @@ public class ISO_639_3_Code implements LanguageCode {
}
tempIndividual.replaceAll((k, v) -> Collections.unmodifiableList(v));
tempMacro.replaceAll((k, v) -> Collections.unmodifiableList(v));

INDIVIDUAL_LANGUAGES = Collections.unmodifiableMap(tempIndividual);
MACRO = Collections.unmodifiableMap(tempMacro);
}

/**
* A stream with all known {@link ISO_639_Code language codes}.
* If the langauge has a 2 letter part 1 code, it will <em>not</em> be implicitly upgraded
* to an {@link ISO_639_1_Code
*
* @see {@link LanguageCode#stream()} For a version that <em>does</em> upgrade
* @return a stream of all known language codes.
*
* @return a stream of all known language codes.
*
*/
public static Stream<ISO_639_3_Code> stream() {
return KNOWN.values()
.stream()
.sorted(Comparator.comparing(ISO_639_3_Code::code));
}

private static final Map<String, String> RETIRED = new HashMap<>();
static {
RETIRED.put("jw", "jv"); // 'Javanese is rendered as "jw" in table 1, while it is correctly given as "jv" in the other tables
RETIRED.put("iw", "he"); // The identifier for Hebrew was changed from "iw" to "he".
RETIRED.put("in", "id"); // The identifier for Indonesian was changed from "in" to "id".
RETIRED.put("ji", "yi"); // The identifier for Yiddish was changed from "ji" to "yi".
}



static Optional<ISO_639_3_Code> getByPart1(String code) {
if (code == null) {
return Optional.empty();
Expand All @@ -139,8 +142,8 @@ static Optional<ISO_639_3_Code> getByPart1(String code) {
.filter(i -> finalCode.equals(i.part1()))
.findFirst();
}


/**
* Retrieves a {@link ISO_639_3_Code} by its three-letter identifier {@link ISO_639#getByPart3(String, boolean)} ()}
* <p>
Expand Down Expand Up @@ -210,7 +213,7 @@ private ISO_639_3_Code(
this.names = Collections.unmodifiableList(names);
}


/**
* The {@link #part1() ISO-639-1-code} if available, otherwise the {@link #part3() ISO-639-3 code}.
*
Expand All @@ -229,7 +232,7 @@ public String toString() {
}



public String part3() {
return part3;
}
Expand Down Expand Up @@ -283,7 +286,7 @@ public String comment() {
public List<NameRecord> nameRecords() {
return names;
}

@Size
public NameRecord nameRecord(Locale locale) {
if (locale.getLanguage().equals("en")) {
Expand All @@ -306,5 +309,5 @@ public List<LanguageCode> individualLanguages() {
private Object readResolve() {
return LanguageCode.get(part3()).orElse(this);
}

}

0 comments on commit 8a2f115

Please sign in to comment.