aboutsummaryrefslogtreecommitdiffstats
path: root/linguistics/src/main/java/com/yahoo/language/LocaleFactory.java
blob: 37213b7d9344636b7f3a058332b12de6500e1f0e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.language;

import java.util.Locale;
import java.util.Objects;

/**
 * @author Simon Thoresen Hult
 */
public final class LocaleFactory {

    private static final Locale UNKNOWN = new Locale("", "", "");

    private LocaleFactory() {}

    /**
     * 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
     */
    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);
    }

}