Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 4e8c67a

Browse files
technohippymhevery
authored andcommitted
fix(filters): handle the current locale correctly
The NumberFormat uses the locale which is set when the formatter is initialized. This means a NumberFormat should be cached for each locale. Closes #865
1 parent b4c92ec commit 4e8c67a

File tree

5 files changed

+28
-23
lines changed

5 files changed

+28
-23
lines changed

lib/filter/currency.dart

+9-5
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,8 @@ part of angular.filter;
1212
*/
1313
@NgFilter(name:'currency')
1414
class Currency implements Function {
15-
NumberFormat nf = new NumberFormat();
1615

17-
Currency() {
18-
nf.minimumFractionDigits = 2;
19-
nf.maximumFractionDigits = 2;
20-
}
16+
var _nfs = new Map<String, NumberFormat>();
2117

2218
/**
2319
* [value]: the value to format
@@ -30,6 +26,14 @@ class Currency implements Function {
3026
if (value is String) value = double.parse(value);
3127
if (value is! num) return value;
3228
if (value.isNaN) return '';
29+
var verifiedLocale = Intl.verifiedLocale(Intl.getCurrentLocale(), NumberFormat.localeExists);
30+
var nf = _nfs[verifiedLocale];
31+
if (nf == null) {
32+
nf = new NumberFormat();
33+
nf.minimumFractionDigits = 2;
34+
nf.maximumFractionDigits = 2;
35+
_nfs[verifiedLocale] = nf;
36+
}
3337
var neg = value < 0;
3438
if (neg) value = -value;
3539
var before = neg ? '(' : '';

lib/filter/number.dart

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ part of angular.filter;
1414
@NgFilter(name:'number')
1515
class Number {
1616

17-
Map<num, NumberFormat> nfs = new Map<num, NumberFormat>();
17+
var _nfs = new Map<String, Map<num, NumberFormat>>();
1818

1919
/**
2020
* [value]: the value to format
@@ -28,14 +28,16 @@ class Number {
2828
if (value is String) value = double.parse(value);
2929
if (!(value is num)) return value;
3030
if (value.isNaN) return '';
31-
var nf = nfs[fractionSize];
31+
var verifiedLocale = Intl.verifiedLocale(Intl.getCurrentLocale(), NumberFormat.localeExists);
32+
_nfs.putIfAbsent(verifiedLocale, () => new Map<num, NumberFormat>());
33+
var nf = _nfs[verifiedLocale][fractionSize];
3234
if (nf == null) {
3335
nf = new NumberFormat()..maximumIntegerDigits = 9;
3436
if (fractionSize != null) {
3537
nf.minimumFractionDigits = fractionSize;
3638
nf.maximumFractionDigits = fractionSize;
3739
}
38-
nfs[fractionSize] = nf;
40+
_nfs[verifiedLocale][fractionSize] = nf;
3941
}
4042
return nf.format(value);
4143
}

test/filter/currency_spec.dart

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
library curerncy_spec;
22

33
import '../_specs.dart';
4+
import 'package:intl/intl.dart';
45

56
void main() {
67
describe('number', () {
@@ -28,5 +29,9 @@ void main() {
2829
expect(currency(0.008)).toEqual(r'$0.01');
2930
expect(currency(0.003)).toEqual(r'$0.00');
3031
});
32+
33+
it('should accept various locales', () {
34+
expect(Intl.withLocale('de', () => currency(0.008, '€', false))).toEqual(r'0,01€');
35+
});
3136
});
3237
}

test/filter/date_spec.dart

+4-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
library date_spec;
22

33
import '../_specs.dart';
4-
import 'package:intl/date_symbol_data_local.dart';
54
import 'package:intl/intl.dart';
65

76
void main() {
@@ -69,20 +68,10 @@ void main() {
6968

7069

7170
it('should accept various locales', async(() {
72-
initializeDateFormatting(null, null).then((_) {
73-
String defaultLocale = Intl.defaultLocale;
74-
try {
75-
Intl.defaultLocale = 'de';
76-
expect(date(noon, "medium")).
77-
toEqual('Sep 3, 2010 12:05:08 nachm.');
78-
79-
Intl.defaultLocale = 'fr';
80-
expect(date(noon, "medium")).
81-
toEqual('sept. 3, 2010 12:05:08 PM');
82-
} finally {
83-
Intl.defaultLocale = defaultLocale;
84-
}
85-
});
71+
expect(Intl.withLocale('de', () => date(noon, "medium"))).
72+
toEqual('Sep 3, 2010 12:05:08 nachm.');
73+
expect(Intl.withLocale('fr', () => date(noon, "medium"))).
74+
toEqual('sept. 3, 2010 12:05:08 PM');
8675
}));
8776
});
8877
}

test/filter/number_spec.dart

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
library number_spec;
22

33
import '../_specs.dart';
4+
import 'package:intl/intl.dart';
45

56
void main() {
67
describe('number', () {
@@ -45,5 +46,9 @@ void main() {
4546
expect(number(-1e-6, 6)).toEqual('-0.000001');
4647
expect(number(-1e-7, 6)).toEqual('-0.000000');
4748
});
49+
50+
it('should accept various locales', () {
51+
expect(Intl.withLocale('de', () => number(1234.567, 2))).toEqual('1.234,57');
52+
});
4853
});
4954
}

0 commit comments

Comments
 (0)