Class LanguageCodeValidator

java.lang.Object
opennlp.tools.util.LanguageCodeValidator

public final class LanguageCodeValidator extends Object
Validates language codes against ISO 639 standards.

Accepts:

  • ISO 639-1 two-letter language codes (e.g., "en", "de")
  • ISO 639-2/3 three-letter language codes (e.g., "eng", "deu")
  • The special code "x-unspecified" used internally by OpenNLP

Valid codes are derived from Locale.availableLocales() plus additional ISO 639-2 bibliographic codes and "und" (undetermined).

Also converts between ISO 639-1 two-letter codes and ISO 639-2/3 three-letter codes, see toIso6391(String) and toIso6393(String).

See Also:
  • Field Details

    • X_UNSPECIFIED

      private static final String X_UNSPECIFIED
      See Also:
    • ISO_639_1_LENGTH

      private static final int ISO_639_1_LENGTH
      See Also:
    • BIBLIOGRAPHIC_TO_TERMINOLOGIC

      private static final Map<String,String> BIBLIOGRAPHIC_TO_TERMINOLOGIC
      ISO 639-2 bibliographic codes that Locale.getISO3Language() never returns, mapped to their ISO 639-2/T (terminologic) equivalent.
    • VALID_CODES

      private static final Set<String> VALID_CODES
    • ISO_639_3_TO_ISO_639_1

      private static final Map<String,String> ISO_639_3_TO_ISO_639_1
      Maps three-letter ISO 639-2/3 codes, terminologic and bibliographic, to their ISO 639-1 two-letter equivalent.
  • Constructor Details

    • LanguageCodeValidator

      private LanguageCodeValidator()
  • Method Details

    • isValid

      public static boolean isValid(String languageCode)
      Checks whether the given language code is a valid ISO 639 code.
      Parameters:
      languageCode - The language code to check. Must not be null.
      Returns:
      true if the code is valid, false otherwise.
      Throws:
      IllegalArgumentException - Thrown if languageCode is null.
    • validateLanguageCode

      public static void validateLanguageCode(String languageCode)
      Validates the given language code and throws an IllegalArgumentException if it is not a recognized ISO 639 language code.
      Parameters:
      languageCode - The language code to validate. Must not be null.
      Throws:
      IllegalArgumentException - Thrown if the code is not a valid ISO 639 language code or is null.
    • toIso6391

      public static String toIso6391(String languageCode)
      Converts a three-letter ISO 639-2/3 code, terminologic ("nld") or bibliographic ("dut"), to its ISO 639-1 two-letter equivalent ("nl"). The code is lower-cased with StringUtil.toLowerCase(CharSequence) first. Two-letter codes and codes without a two-letter equivalent are returned lower-cased and otherwise unchanged. The code is not validated; use validateLanguageCode(String) for that.
      Parameters:
      languageCode - The language code to convert. Must not be null.
      Returns:
      The ISO 639-1 code, or the lower-cased input if there is none.
      Throws:
      IllegalArgumentException - Thrown if languageCode is null.
    • toIso6393

      public static String toIso6393(String languageCode)
      Converts an ISO 639-1 two-letter code ("de") or an ISO 639-2 bibliographic code ("ger") to its ISO 639-2/3 three-letter equivalent ("deu"). The code is lower-cased with StringUtil.toLowerCase(CharSequence) first. Other codes, and two-letter codes without a three-letter equivalent, are returned lower-cased and otherwise unchanged. The code is not validated; use validateLanguageCode(String) for that.
      Parameters:
      languageCode - The language code to convert. Must not be null.
      Returns:
      The ISO 639-2/3 code, or the lower-cased input if there is none.
      Throws:
      IllegalArgumentException - Thrown if languageCode is null.