summaryrefslogtreecommitdiff
path: root/propertysheet/src/org/eclipse/wb/internal/core/nls/model/LocaleInfo.java
blob: 04033517d49788f6fb51a7586251bb8bded09afd (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
185
186
187
188
189
190
/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.wb.internal.core.nls.model;

import java.util.Locale;

/**
 * Information about {@link Locale}.
 *
 * We separate {@link LocaleInfo} from {@link BundleInfo} because {@link BundleInfo} is wrapper for
 * single *.properties file and we support more than one source of bundles in one
 * {@link CompilationUnit}.
 *
 * @author scheglov_ke
 * @coverage core.nls
 */
public final class LocaleInfo implements Comparable<LocaleInfo> {
  /**
   * The default {@link LocaleInfo}.
   */
  public static final LocaleInfo DEFAULT = new LocaleInfo(null);
  ////////////////////////////////////////////////////////////////////////////
  //
  // Instance fields
  //
  ////////////////////////////////////////////////////////////////////////////
  private final Locale m_locale;

  ////////////////////////////////////////////////////////////////////////////
  //
  // Constructor
  //
  ////////////////////////////////////////////////////////////////////////////
  public LocaleInfo(Locale locale) {
    m_locale = locale;
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Object
  //
  ////////////////////////////////////////////////////////////////////////////
  @Override
  public String toString() {
    return getTitle();
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    }
    if (obj instanceof LocaleInfo) {
      LocaleInfo localeInfo = (LocaleInfo) obj;
      if (isDefault()) {
        return localeInfo.isDefault();
      }
      return m_locale.equals(localeInfo.m_locale);
    }
    return false;
  }

  @Override
  public int hashCode() {
    if (isDefault()) {
      return 0;
    }
    return m_locale.hashCode();
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Comparable
  //
  ////////////////////////////////////////////////////////////////////////////
  @Override
public int compareTo(LocaleInfo o) {
    if (m_locale == null) {
      if (o.m_locale == null) {
        return 0;
      }
      return -1;
    }
    if (o.m_locale == null) {
      return 1;
    }
    String localeNameA = m_locale.toString();
    String localeNameB = o.m_locale.toString();
    return localeNameA.compareTo(localeNameB);
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Access
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * @return {@link Locale} for this {@link LocaleInfo}.
   */
  public Locale getLocale() {
    return m_locale;
  }

  /**
   * @return <code>true</code> if that {@link Locale} is default.
   */
  public boolean isDefault() {
    return m_locale == null;
  }

  /**
   * @return the title to display in UI.
   */
  public String getTitle() {
    if (isDefault()) {
      return "(default)";
    }
    return m_locale.toString();
  }

  ////////////////////////////////////////////////////////////////////////////
  //
  // Utils
  //
  ////////////////////////////////////////////////////////////////////////////
  /**
   * @return the "parent" {@link LocaleInfo} from given array.<br>
   *         Here "parent" is locale that is more general than current one.<br>
   *         For example "parent" locale for 'ru_RU' is 'ru'.
   *
   *         If there are no parent locale in array, return default locale.<br>
   */
  public LocaleInfo getParent(LocaleInfo locales[]) {
    String localeName = m_locale.toString();
    int lastSeparatorIndex = localeName.lastIndexOf('_');
    if (lastSeparatorIndex != -1) {
      String parentLocaleName = localeName.substring(0, lastSeparatorIndex);
      // try to find locale with parent name
      for (LocaleInfo locale : locales) {
        if (locale.getLocale() != null && locale.getLocale().toString().equals(parentLocaleName)) {
          return locale;
        }
      }
    }
    // use default
    return LocaleInfo.DEFAULT;
  }

  /**
   * @param localeName
   *          the name of locale, such as "en" or "ru_RU".
   * @param localeDescription
   *          the description of {@link Locale}, used in exception.
   * @return the {@link LocaleInfo} which wraps {@link Locale}.
   */
  public static LocaleInfo create(String localeName, String localeDescription) {
    // try to find locale in list of available locales
    Locale[] locales = Locale.getAvailableLocales();
    for (int i = 0; i < locales.length; i++) {
      Locale locale = locales[i];
      if (locale.toString().equals(localeName)) {
        return new LocaleInfo(locale);
      }
    }
    // try to create new, this constructor is since 1.4, so do this in try/catch
    try {
      Locale locale;
      int separatorIndex = localeName.indexOf('_');
      if (separatorIndex != -1) {
        String language = localeName.substring(0, separatorIndex);
        String country = localeName.substring(separatorIndex + 1);
        locale = new Locale(language, country);
      } else {
        locale = new Locale(localeName);
      }
      return new LocaleInfo(locale);
    } catch (Throwable e) {
      String msg = "Locale not found for " + localeDescription;
      throw new IllegalArgumentException(msg);
    }
  }
}