aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/configuration/Locale.java
blob: 6cb396394cdb43282419a25c7be443f49f5d7b28 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.adt.internal.editors.layout.configuration;

import static com.android.ide.common.resources.configuration.LocaleQualifier.FAKE_VALUE;

import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.android.ide.common.resources.configuration.FolderConfiguration;
import com.android.ide.common.resources.configuration.LocaleQualifier;

import org.eclipse.swt.graphics.Image;

/**
 * A language,region pair
 */
public class Locale {
    /**
     * A special marker region qualifier representing any region
     */
    public static final LocaleQualifier ANY_QUALIFIER = new LocaleQualifier(FAKE_VALUE);

    /**
     * A locale which matches any language and region
     */
    public static final Locale ANY = new Locale(ANY_QUALIFIER);

    /**
     * The locale qualifier, or {@link #ANY_QUALIFIER} if this locale matches
     * any locale
     */
    @NonNull
    public final LocaleQualifier qualifier;

    /**
     * Constructs a new {@linkplain Locale} matching a given language in a given
     * locale.
     *
     * @param locale the locale
     */
    private Locale(@NonNull
    LocaleQualifier locale) {
        qualifier = locale;
    }

    /**
     * Constructs a new {@linkplain Locale} matching a given language in a given
     * specific locale.
     *
     * @param locale the locale
     * @return a locale with the given locale
     */
    @NonNull
    public static Locale create(@NonNull
    LocaleQualifier locale) {
        return new Locale(locale);
    }

    /**
     * Constructs a new {@linkplain Locale} for the given folder configuration
     *
     * @param folder the folder configuration
     * @return a locale with the given language and region
     */
    public static Locale create(FolderConfiguration folder) {
        LocaleQualifier locale = folder.getLocaleQualifier();
        if (locale == null) {
            return ANY;
        } else {
            return new Locale(locale);
        }
    }

    /**
     * Constructs a new {@linkplain Locale} for the given locale string, e.g.
     * "zh", "en-rUS", or "b+eng+US".
     *
     * @param localeString the locale description
     * @return the corresponding locale
     */
    @NonNull
    public static Locale create(@NonNull
    String localeString) {
        // Load locale. Note that this can get overwritten by the
        // project-wide settings read below.

        LocaleQualifier qualifier = LocaleQualifier.getQualifier(localeString);
        if (qualifier != null) {
            return new Locale(qualifier);
        } else {
            return ANY;
        }
    }

    /**
     * Returns a flag image to use for this locale
     *
     * @return a flag image, or a default globe icon
     */
    @NonNull
    public Image getFlagImage() {
        String languageCode = qualifier.hasLanguage() ? qualifier.getLanguage() : null;
        if (languageCode == null) {
            return FlagManager.getGlobeIcon();
        }
        String regionCode = hasRegion() ? qualifier.getRegion() : null;
        FlagManager icons = FlagManager.get();
        Image image = icons.getFlag(languageCode, regionCode);
        if (image != null) {
            return image;
        } else {
            return FlagManager.getGlobeIcon();
        }
    }

    /**
     * Returns true if this locale specifies a specific language. This is true
     * for all locales except {@link #ANY}.
     *
     * @return true if this locale specifies a specific language
     */
    public boolean hasLanguage() {
        return !qualifier.hasFakeValue();
    }

    /**
     * Returns true if this locale specifies a specific region
     *
     * @return true if this locale specifies a region
     */
    public boolean hasRegion() {
        return qualifier.getRegion() != null && !FAKE_VALUE.equals(qualifier.getRegion());
    }

    /**
     * Returns the locale formatted as language-region. If region is not set,
     * language is returned. If language is not set, empty string is returned.
     */
    public String toLocaleId() {
        return qualifier == ANY_QUALIFIER ? "" : qualifier.getTag();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + qualifier.hashCode();
        return result;
    }

    @Override
    public boolean equals(@Nullable
    Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Locale other = (Locale) obj;
        if (!qualifier.equals(other.qualifier))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return qualifier.getTag();
    }
}