Repository navigation
Expand file tree
/
Copy pathIlibLocale.java
More file actions
227 lines (199 loc) · 5.26 KB
/
Copy pathIlibLocale.java
File metadata and controls
227 lines (199 loc) · 5.26 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
*
* Copyright © 2013, JEDLSoft
*
* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
*
* 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.ilib;
import java.util.Locale;
/**
* IlibLocale
*
* @author edwin
*/
public class IlibLocale
{
protected String language = "";
protected String region = "";
protected String script = "";
protected String variant = "";
protected boolean isLanguageCode(String str)
{
if ( str.length() < 2 || str.length() > 3 ) {
return false;
}
for ( int i = 0; i < str.length(); i++ ) {
if ( !Character.isLowerCase(str.charAt(i)) ) {
return false;
}
}
return true;
}
protected boolean isRegionCode(String str)
{
if ( str.length() != 2 ) {
return false;
}
return Character.isUpperCase(str.charAt(0)) && Character.isUpperCase(str.charAt(1));
}
protected boolean isScriptCode(String str)
{
if ( str.length() != 4 || !Character.isUpperCase(str.charAt(0)) ) {
return false;
}
for ( int i = 1; i < str.length(); i++ ) {
if ( !Character.isLowerCase(str.charAt(i)) ) {
return false;
}
}
return true;
}
/**
* @param spec
*/
public IlibLocale(String spec)
{
if ( spec != null ) {
String[] parts = spec.split("-");
for ( int i = 0; i < parts.length; i++ ) {
if ( isLanguageCode(parts[i]) ) {
language = parts[i];
} else if ( isRegionCode(parts[i]) ) {
region = parts[i];
} else if ( isScriptCode(parts[i]) ) {
script = parts[i];
} else {
variant = parts[i];
}
}
}
}
/**
* @param language
* @param region
* @param script
* @param variant
*/
public IlibLocale(String language, String region, String script, String variant)
{
this.language = language != null ? language : "";
this.region = region != null ? region : "";
this.script = script != null ? script : "";
this.variant = variant != null ? variant : "";
}
/**
* @return the locale specifier
*/
public String getSpec()
{
return toString();
}
public String toString()
{
StringBuilder ret = new StringBuilder();
if ( language != null && language.length() > 0 ) {
ret.append(language);
}
if ( script != null && script.length() > 0 ) {
if ( ret.length() > 0 ) {
ret.append("-");
}
ret.append(script);
}
if ( region != null && region.length() > 0 ) {
if ( ret.length() > 0 ) {
ret.append("-");
}
ret.append(region);
}
if ( variant != null && variant.length() > 0 ) {
if ( ret.length() > 0 ) {
ret.append("-");
}
ret.append(variant);
}
return ret.toString();
}
public String getLanguage()
{
return language;
}
public String getRegion()
{
return region;
}
public String getScript()
{
return script;
}
public String getVariant()
{
return variant;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if ( obj instanceof IlibLocale ) {
IlibLocale other = (IlibLocale) obj;
return (this.language.equals(other.language) &&
this.region.equals(other.region) &&
this.script.equals(other.script) &&
this.variant.equals(other.variant));
}
return super.equals(obj);
}
/**
* @param spec
* @return whether this locale represents the same locale as the given spec
*/
public boolean equals(String spec)
{
if ( this.getSpec().equals(spec) ) {
return true;
}
return false;
}
/**
* Return whether or not the current locale is compatible
* with the given locale. A locale is compatible with another locale if
* the other locale is one of its ancestors. That is, if the current
* locale inherits from the given locale eventually, then they are
* compatible.
*
* @param other the locale to check against
* @return true if the the current locale is compatible with the
* other locale
*/
public boolean isCompatibleWith(IlibLocale other)
{
// TODO: fill in IlibLocale.isCompatibleWith
return true;
}
/**
* @return true if this is the pseudolocalization locale
*/
public boolean isPseudo()
{
return language.equals("zxx");
}
/**
* Return the equivalent Java locale for this ilib locale.
* @return the equivalent Java locale
*/
public Locale getJavaLocale() {
return Locale.forLanguageTag(getSpec());
}
}