1
- import {
2
- DateTimeFormat as commonDateTimeFormat ,
3
- NumberFormat as commonNumberFormat ,
4
- FULL
5
- } from "./nativescript-intl-common" ;
1
+ import { DateTimeFormat as commonDateTimeFormat , NumberFormat as commonNumberFormat , FULL } from "./nativescript-intl-common" ;
6
2
import { NumberFormatOptions } from "./nativescript-intl" ;
3
+ import getSymbolFromCurrency from "currency-symbol-map" ;
7
4
8
5
let localesCache : Map < string , any > = new Map < string , any > ( ) ;
9
6
@@ -20,7 +17,7 @@ function getNativeLocale(locale?: string) {
20
17
if ( firstHypenIndex > - 1 ) {
21
18
lang = locale . substr ( 0 , firstHypenIndex ) ;
22
19
let nextHypenIndex = locale . substr ( firstHypenIndex + 1 ) . indexOf ( "-" ) ;
23
- country = locale . substr ( firstHypenIndex + 1 , ( nextHypenIndex > - 1 ) ? nextHypenIndex : undefined ) ;
20
+ country = locale . substr ( firstHypenIndex + 1 , nextHypenIndex > - 1 ? nextHypenIndex : undefined ) ;
24
21
} else {
25
22
lang = locale ;
26
23
}
@@ -37,7 +34,13 @@ function getNativeLocale(locale?: string) {
37
34
}
38
35
39
36
export class DateTimeFormat extends commonDateTimeFormat {
40
- public getNativePattern ( patternDefinition : { date ?: string , time ?: string } , locale ?: string ) : string {
37
+ public getNativePattern (
38
+ patternDefinition : {
39
+ date ?: string ;
40
+ time ?: string ;
41
+ } ,
42
+ locale ?: string
43
+ ) : string {
41
44
let result = "" ;
42
45
let flag = 0 ;
43
46
let nativeLocale ;
@@ -69,8 +72,7 @@ export class DateTimeFormat extends commonDateTimeFormat {
69
72
break ;
70
73
case 3 :
71
74
// date + locale
72
- dateFormat =
73
- java . text . DateFormat . getDateInstance ( patternDefinition . date === FULL ? 0 : 3 , nativeLocale ) ;
75
+ dateFormat = java . text . DateFormat . getDateInstance ( patternDefinition . date === FULL ? 0 : 3 , nativeLocale ) ;
74
76
break ;
75
77
case 4 :
76
78
// only time we always use long pattern using default locale
@@ -86,8 +88,7 @@ export class DateTimeFormat extends commonDateTimeFormat {
86
88
break ;
87
89
case 7 :
88
90
// locale + date + time
89
- dateFormat =
90
- java . text . DateFormat . getDateTimeInstance ( patternDefinition . date === FULL ? 0 : 3 , 1 , nativeLocale ) ;
91
+ dateFormat = java . text . DateFormat . getDateTimeInstance ( patternDefinition . date === FULL ? 0 : 3 , 1 , nativeLocale ) ;
91
92
break ;
92
93
default :
93
94
break ;
@@ -97,14 +98,11 @@ export class DateTimeFormat extends commonDateTimeFormat {
97
98
}
98
99
99
100
public formatNative ( pattern : string , locale ?: string , date ?: Date ) : string {
100
- let sdf = locale ?
101
- new java . text . SimpleDateFormat ( pattern , getNativeLocale ( locale ) ) :
102
- new java . text . SimpleDateFormat ( pattern ) ;
101
+ let sdf = locale ? new java . text . SimpleDateFormat ( pattern , getNativeLocale ( locale ) ) : new java . text . SimpleDateFormat ( pattern ) ;
103
102
return sdf . format ( date ? new java . util . Date ( date . valueOf ( ) ) : new java . util . Date ( ) ) . toString ( ) ;
104
103
}
105
104
}
106
105
107
-
108
106
// style?: string;
109
107
// currency?: string;
110
108
// currencyDisplay?: string;
@@ -113,69 +111,78 @@ export class DateTimeFormat extends commonDateTimeFormat {
113
111
// minimumFractionDigits?: number;
114
112
// maximumFractionDigits?: number;
115
113
export class NumberFormat extends commonNumberFormat {
116
- public formatNative ( value : number , locale ?: string , options ?: NumberFormatOptions , pattern ?: string ) {
117
- let numberFormat ;
114
+ public numberFormat : java . text . DecimalFormat | java . text . NumberFormat | any ;
115
+ constructor ( locale ?: string , options ?: NumberFormatOptions , pattern ?: string ) {
116
+ super ( locale , options , pattern ) ;
118
117
if ( pattern ) {
119
- numberFormat = new java . text . DecimalFormat ( pattern ) ;
118
+ this . numberFormat = new java . text . DecimalFormat ( pattern ) ;
120
119
} else {
121
- if ( options ) {
120
+ if ( options && options . style ) {
122
121
switch ( options . style . toLowerCase ( ) ) {
123
122
case "decimal" :
124
- numberFormat = java . text . NumberFormat . getNumberInstance ( getNativeLocale ( locale ) ) ;
123
+ this . numberFormat = java . text . NumberFormat . getNumberInstance ( getNativeLocale ( locale ) ) ;
125
124
break ;
126
125
case "percent" :
127
- numberFormat = java . text . NumberFormat . getPercentInstance ( getNativeLocale ( locale ) ) ;
126
+ this . numberFormat = java . text . NumberFormat . getPercentInstance ( getNativeLocale ( locale ) ) ;
128
127
break ;
129
128
case "currency" :
130
- numberFormat = java . text . NumberFormat . getCurrencyInstance ( getNativeLocale ( locale ) ) ;
129
+ this . numberFormat = java . text . NumberFormat . getCurrencyInstance ( getNativeLocale ( locale ) ) ;
131
130
if ( options . currency !== void 0 ) {
132
- numberFormat . setCurrency ( java . util . Currency . getInstance ( options . currency ) ) ;
131
+ this . numberFormat . setCurrency ( java . util . Currency . getInstance ( options . currency ) ) ;
133
132
}
134
133
break ;
135
134
default :
136
- numberFormat = java . text . NumberFormat . getNumberInstance ( getNativeLocale ( locale ) ) ;
135
+ this . numberFormat = java . text . NumberFormat . getNumberInstance ( getNativeLocale ( locale ) ) ;
137
136
break ;
138
137
}
139
138
} else {
140
- numberFormat = java . text . NumberFormat . getNumberInstance ( getNativeLocale ( locale ) ) ;
139
+ this . numberFormat = java . text . NumberFormat . getNumberInstance ( getNativeLocale ( locale ) ) ;
141
140
}
142
141
}
143
142
144
143
if ( options && options . minimumIntegerDigits !== void 0 ) {
145
- numberFormat . setMinimumIntegerDigits ( options . minimumIntegerDigits ) ;
144
+ this . numberFormat . setMinimumIntegerDigits ( options . minimumIntegerDigits ) ;
146
145
}
147
146
148
147
if ( options && options . minimumFractionDigits !== void 0 ) {
149
- numberFormat . setMinimumFractionDigits ( options . minimumFractionDigits ) ;
148
+ this . numberFormat . setMinimumFractionDigits ( options . minimumFractionDigits ) ;
150
149
}
151
150
152
151
if ( options && options . maximumFractionDigits !== void 0 ) {
153
- numberFormat . setMaximumFractionDigits ( options . maximumFractionDigits ) ;
152
+ this . numberFormat . setMaximumFractionDigits ( options . maximumFractionDigits ) ;
154
153
}
155
154
156
155
if ( options && options . useGrouping !== void 0 ) {
157
- numberFormat . setGroupingUsed ( options . useGrouping ) ;
156
+ this . numberFormat . setGroupingUsed ( options . useGrouping ) ;
157
+ }
158
+
159
+ let decimalFormatSymbols = locale
160
+ ? new java . text . DecimalFormatSymbols ( getNativeLocale ( locale ) )
161
+ : new java . text . DecimalFormatSymbols ( ) ;
162
+
163
+ if ( options && options . currency !== void 0 ) {
164
+ decimalFormatSymbols . setCurrency ( java . util . Currency . getInstance ( options . currency ) ) ;
165
+ // Use a narrow format symbol ("$100" rather than "US$100") (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#narrowsymbol)
166
+ if ( options && options . currencyDisplay === "narrowSymbol" ) {
167
+ const currencySymbol = getSymbolFromCurrency ( options . currency ) ;
168
+ if ( currencySymbol ) decimalFormatSymbols . setCurrencySymbol ( currencySymbol ) ;
169
+ }
158
170
}
159
171
160
- let decimalFormatSymbols = locale ?
161
- new java . text . DecimalFormatSymbols ( getNativeLocale ( locale ) ) :
162
- new java . text . DecimalFormatSymbols ( ) ;
163
- numberFormat . setDecimalFormatSymbols ( decimalFormatSymbols ) ;
172
+ this . numberFormat . setDecimalFormatSymbols ( decimalFormatSymbols ) ;
164
173
165
- if ( options && ( options . style . toLowerCase ( ) === "currency" && options . currencyDisplay === "code" ) ) {
174
+ if ( options && options . style . toLowerCase ( ) === "currency" && options . currencyDisplay === "code" ) {
166
175
if ( ! pattern ) {
167
- let currrentPattern = numberFormat . toPattern ( ) ;
176
+ let currrentPattern = this . numberFormat . toPattern ( ) ;
168
177
// this will display currency code instead of currency symbol
169
178
currrentPattern = currrentPattern . replace ( "¤" , "¤¤" ) ;
170
- numberFormat = new java . text . DecimalFormat ( currrentPattern ) ;
171
- numberFormat . setDecimalFormatSymbols ( decimalFormatSymbols ) ;
172
- }
173
-
174
- if ( options . currency !== void 0 ) {
175
- decimalFormatSymbols . setCurrency ( java . util . Currency . getInstance ( options . currency ) ) ;
179
+ this . numberFormat = new java . text . DecimalFormat ( currrentPattern ) ;
180
+ this . numberFormat . setDecimalFormatSymbols ( decimalFormatSymbols ) ;
176
181
}
177
182
}
183
+ }
178
184
179
- return numberFormat . format ( value ) ;
185
+ public formatNative ( value : number ) {
186
+ return this . numberFormat . format ( value ) ;
180
187
}
181
188
}
0 commit comments