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:
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    isValid(String languageCode)
    Checks whether the given language code is a valid ISO 639 code.
    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").
    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").
    static void
    Validates the given language code and throws an IllegalArgumentException if it is not a recognized ISO 639 language code.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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.