/
LocaleModule.java
138 lines (118 loc) Β· 3.5 KB
/
LocaleModule.java
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
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2010-2012 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package ti.modules.titanium.locale;
import java.util.Locale;
import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiContext;
import org.appcelerator.titanium.util.TiPlatformHelper;
import org.appcelerator.titanium.util.TiRHelper;
import android.content.Context;
import android.content.res.Configuration;
import android.telephony.PhoneNumberUtils;
@Kroll.module
public class LocaleModule extends KrollModule
{
private static final String TAG = "LocaleModule";
public LocaleModule()
{
super();
}
public LocaleModule(TiContext tiContext)
{
this();
}
@Kroll.method @Kroll.getProperty
public String getCurrentLanguage()
{
return Locale.getDefault().getLanguage();
}
@Kroll.method @Kroll.getProperty
public String getCurrentCountry()
{
return Locale.getDefault().getCountry();
}
@Kroll.method @Kroll.getProperty
public String getCurrentLocale()
{
return TiPlatformHelper.getInstance().getLocale();
}
@Kroll.method
public String getCurrencyCode(String localeString)
{
if (localeString == null) {
return null;
}
Locale locale = TiPlatformHelper.getInstance().getLocale(localeString);
return TiPlatformHelper.getInstance().getCurrencyCode(locale);
}
@Kroll.method
public String getCurrencySymbol(String currencyCode)
{
return TiPlatformHelper.getInstance().getCurrencySymbol(currencyCode);
}
@Kroll.method
public String getLocaleCurrencySymbol(String localeString)
{
if (localeString == null) {
return null;
}
Locale locale = TiPlatformHelper.getInstance().getLocale(localeString);
return TiPlatformHelper.getInstance().getCurrencySymbol(locale);
}
@SuppressWarnings("deprecation")
@Kroll.method
public String formatTelephoneNumber(String telephoneNumber)
{
return PhoneNumberUtils.formatNumber(telephoneNumber);
}
@Kroll.method @Kroll.setProperty
public void setLanguage(String language)
{
try {
String[] parts = language.split("-");
Locale locale = null;
if (parts.length > 1) {
locale = new Locale(parts[0], parts[1]);
} else {
locale = new Locale(parts[0]);
}
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
Context ctx = TiApplication.getInstance().getBaseContext();
ctx.getResources().updateConfiguration(config, ctx.getResources().getDisplayMetrics());
} catch (Exception e) {
Log.e(TAG, "Error trying to set language '" + language + "':", e);
}
}
@Kroll.method @Kroll.topLevel("L")
public String getString(String key, @Kroll.argument(optional=true) String defaultValue)
{
try {
int resid = TiRHelper.getResource("string." + key.replace(".","_"));
if (resid != 0) {
return TiApplication.getInstance().getString(resid);
} else {
return defaultValue;
}
} catch (TiRHelper.ResourceNotFoundException e) {
Log.d(TAG, "Resource string with key '" + key + "' not found. Returning default value.", Log.DEBUG_MODE);
return defaultValue;
} catch (Exception e) {
Log.e(TAG, "Error trying to get resource string with key '" + key + "':", e);
return defaultValue;
}
}
@Override
public String getApiName()
{
return "Ti.Locale";
}
}