blob: 763af73fd502db1e152342ea4118d887fe2657e6 (
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
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* 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 android.util;
import java.util.Locale;
import libcore.icu.ICU;
/**
* Various utilities for Locales
*
* @hide
*/
public class LocaleUtil {
private LocaleUtil() { /* cannot be instantiated */ }
/**
* @hide Do not use. Implementation not finished.
*/
public static final int TEXT_LAYOUT_DIRECTION_LTR_DO_NOT_USE = 0;
/**
* @hide Do not use. Implementation not finished.
*/
public static final int TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE = 1;
private static final char UNDERSCORE_CHAR = '_';
private static String ARAB_SCRIPT_SUBTAG = "Arab";
private static String HEBR_SCRIPT_SUBTAG = "Hebr";
/**
* Return the layout direction for a given Locale
*
* @param locale the Locale for which we want the layout direction. Can be null.
* @return the layout direction. This may be one of:
* {@link #TEXT_LAYOUT_DIRECTION_LTR_DO_NOT_USE} or
* {@link #TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE}.
*
* Be careful: this code will need to be changed when vertical scripts will be supported
*
* @hide
*/
public static int getLayoutDirectionFromLocale(Locale locale) {
if (locale != null && !locale.equals(Locale.ROOT)) {
final String scriptSubtag = ICU.getScript(ICU.addLikelySubtags(locale.toString()));
if (scriptSubtag == null) return getLayoutDirectionFromFirstChar(locale);
if (scriptSubtag.equalsIgnoreCase(ARAB_SCRIPT_SUBTAG) ||
scriptSubtag.equalsIgnoreCase(HEBR_SCRIPT_SUBTAG)) {
return TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE;
}
}
return TEXT_LAYOUT_DIRECTION_LTR_DO_NOT_USE;
}
/**
* Fallback algorithm to detect the locale direction. Rely on the fist char of the
* localized locale name. This will not work if the localized locale name is in English
* (this is the case for ICU 4.4 and "Urdu" script)
*
* @param locale
* @return the layout direction. This may be one of:
* {@link #TEXT_LAYOUT_DIRECTION_LTR_DO_NOT_USE} or
* {@link #TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE}.
*
* Be careful: this code will need to be changed when vertical scripts will be supported
*
* @hide
*/
private static int getLayoutDirectionFromFirstChar(Locale locale) {
switch(Character.getDirectionality(locale.getDisplayName(locale).charAt(0))) {
case Character.DIRECTIONALITY_RIGHT_TO_LEFT:
case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:
return TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE;
case Character.DIRECTIONALITY_LEFT_TO_RIGHT:
default:
return TEXT_LAYOUT_DIRECTION_LTR_DO_NOT_USE;
}
}
}
|