public static Locale fromLanguageTag(String tag) {
        Objects.requireNonNull(tag, "tag cannot be null");

        tag = tag.trim();
        if (tag.isEmpty()) return UNKNOWN;

        String language = "";
        String region = "";
        String script = "";
        String[] parts = tag.split("-");
        for (int partIdx = 0; partIdx < parts.length; ++partIdx) {
            String part = parts[partIdx];
            int partLen = part.length();
            if (partIdx == 0) {
                if (partLen == 2 || partLen == 3) {
                    language = part;
                }
            } else if (partIdx == 1 || partIdx == 2) {
                if (partLen == 2 || partLen == 3) {
                    region = part;
                } else if (partLen == 4) {
                    script = part;
                }
            }
        }
        if (language.isEmpty()) return UNKNOWN;
        return new Locale(language, region, script);
    }
Implements a simple parser for RFC5646 language tags. The language tag is parsed into a Locale.

@param tag the language tag to parse
@return the corresponding Locale
linguistics/src/main/java/com/yahoo/language/LocaleFactory.java