-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnativescript-intl.android.ts
182 lines (165 loc) · 6.84 KB
/
nativescript-intl.android.ts
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
import {
DateTimeFormat as commonDateTimeFormat,
NumberFormat as commonNumberFormat,
FULL
} from "./nativescript-intl-common";
import { NumberFormatOptions } from "./nativescript-intl";
let localesCache: Map<string, any> = new Map<string, any>();
function getNativeLocale(locale?: string) {
if (localesCache.has(locale)) {
return localesCache.get(locale);
}
let result;
if (locale) {
locale = locale.replace(/_/g, "-");
let firstHypenIndex = locale.indexOf("-");
let lang = "";
let country = "";
if (firstHypenIndex > -1) {
lang = locale.substr(0, firstHypenIndex);
let nextHypenIndex = locale.substr(firstHypenIndex + 1).indexOf("-");
country = locale.substr(firstHypenIndex + 1, (nextHypenIndex > -1) ? nextHypenIndex : undefined);
} else {
lang = locale;
}
if (country !== "") {
result = new java.util.Locale(lang, country);
} else {
result = new java.util.Locale(lang);
}
} else {
result = new java.util.Locale("en", "US");
}
localesCache.set(locale, result);
return result;
}
export class DateTimeFormat extends commonDateTimeFormat {
public getNativePattern(patternDefinition: {date?: string, time?: string}, locale?: string): string {
let result = "";
let flag = 0;
let nativeLocale;
if (locale) {
nativeLocale = getNativeLocale(locale);
flag++;
}
if (patternDefinition.date) {
flag = flag + 2;
}
if (patternDefinition.time) {
flag = flag + 4;
}
let dateFormat;
switch (flag) {
case 0:
// no locale no date no time
dateFormat = java.text.DateFormat.getDateTimeInstance();
break;
case 1:
// only locale
dateFormat = java.text.DateFormat.getDateTimeInstance(0, 0, nativeLocale);
break;
case 2:
// only date 0 for full, 3 for Short date format using default locale
dateFormat = java.text.DateFormat.getDateInstance(patternDefinition.date === FULL ? 0 : 3);
break;
case 3:
// date + locale
dateFormat =
java.text.DateFormat.getDateInstance(patternDefinition.date === FULL ? 0 : 3, nativeLocale);
break;
case 4:
// only time we always use long pattern using default locale
dateFormat = java.text.DateFormat.getTimeInstance(1);
break;
case 5:
// time + locale
dateFormat = java.text.DateFormat.getTimeInstance(1, nativeLocale);
break;
case 6:
// time + date using default locale
dateFormat = java.text.DateFormat.getDateTimeInstance(patternDefinition.date === FULL ? 0 : 3, 1);
break;
case 7:
// locale + date + time
dateFormat =
java.text.DateFormat.getDateTimeInstance(patternDefinition.date === FULL ? 0 : 3, 1, nativeLocale);
break;
default:
break;
}
result = dateFormat.toPattern();
return result;
}
public formatNative(pattern: string, locale?: string, date?: Date): string {
let sdf = locale ?
new java.text.SimpleDateFormat(pattern, getNativeLocale(locale)) :
new java.text.SimpleDateFormat(pattern);
return sdf.format(date ? new java.util.Date(date.valueOf()) : new java.util.Date()).toString();
}
}
// style?: string;
// currency?: string;
// currencyDisplay?: string;
// useGrouping?: boolean;
// minimumIntegerDigits?: number;
// minimumFractionDigits?: number;
// maximumFractionDigits?: number;
export class NumberFormat extends commonNumberFormat {
public formatNative(value: number, locale?: string, options?: NumberFormatOptions, pattern?: string) {
let numberFormat;
if (pattern) {
numberFormat = new java.text.DecimalFormat(pattern);
} else {
if (options) {
switch (options.style.toLowerCase()) {
case "decimal":
numberFormat = java.text.NumberFormat.getNumberInstance(getNativeLocale(locale));
break;
case "percent":
numberFormat = java.text.NumberFormat.getPercentInstance(getNativeLocale(locale));
break;
case "currency":
numberFormat = java.text.NumberFormat.getCurrencyInstance(getNativeLocale(locale));
if (options.currency !== void 0) {
numberFormat.setCurrency(java.util.Currency.getInstance(options.currency));
}
break;
default:
numberFormat = java.text.NumberFormat.getNumberInstance(getNativeLocale(locale));
break;
}
} else {
numberFormat = java.text.NumberFormat.getNumberInstance(getNativeLocale(locale));
}
}
if (options && options.minimumIntegerDigits !== void 0) {
numberFormat.setMinimumIntegerDigits(options.minimumIntegerDigits);
}
if (options && options.minimumFractionDigits !== void 0) {
numberFormat.setMinimumFractionDigits(options.minimumFractionDigits);
}
if (options && options.maximumFractionDigits !== void 0) {
numberFormat.setMaximumFractionDigits(options.maximumFractionDigits);
}
if (options && options.useGrouping !== void 0) {
numberFormat.setGroupingUsed(options.useGrouping);
}
let decimalFormatSymbols = locale ?
new java.text.DecimalFormatSymbols(getNativeLocale(locale)) :
new java.text.DecimalFormatSymbols();
if (options && options.currency !== void 0) {
decimalFormatSymbols.setCurrency(java.util.Currency.getInstance(options.currency));
}
numberFormat.setDecimalFormatSymbols(decimalFormatSymbols);
if (options && (options.style.toLowerCase() === "currency" && options.currencyDisplay === "code")) {
if (!pattern) {
let currrentPattern = numberFormat.toPattern();
// this will display currency code instead of currency symbol
currrentPattern = currrentPattern.replace("¤", "¤¤");
numberFormat = new java.text.DecimalFormat(currrentPattern);
numberFormat.setDecimalFormatSymbols(decimalFormatSymbols);
}
}
return numberFormat.format(value);
}
}