Skip to content

Commit 720bcc4

Browse files
committed
Remove Dart locale parser
1 parent 863dc69 commit 720bcc4

File tree

4 files changed

+28
-34
lines changed

4 files changed

+28
-34
lines changed

pkgs/intl4x/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.10.2-wip
2+
3+
- Remove Dart parser of locale strings.
4+
15
## 0.10.1
26

37
- Upgrade to new artifacts.

pkgs/intl4x/lib/src/locale/locale_4x.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
import '../bindings/lib.g.dart' as icu;
66

77
import 'locale.dart';
8+
import 'locale_native.dart';
89

9-
extension Locale4X on Locale {
10+
extension Locale4XTransformer on Locale {
1011
icu.Locale to4X() {
11-
final icu4xLocale = icu.Locale.und()..language = language;
12-
if (region != null) icu4xLocale.region = region!;
13-
if (script != null) icu4xLocale.script = script!;
14-
return icu4xLocale;
12+
if (this is IcuLocale) {
13+
return (this as IcuLocale).locale;
14+
} else {
15+
final icu4xLocale = icu.Locale.und()..language = language;
16+
if (region != null) icu4xLocale.region = region!;
17+
if (script != null) icu4xLocale.script = script!;
18+
return icu4xLocale;
19+
}
1520
}
1621
}

pkgs/intl4x/lib/src/locale/locale_native.dart

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,26 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import '../bindings/lib.g.dart' as icu;
56
import 'locale.dart';
67

78
/// This file should be replaced by references to ICU4X when ready.
89
9-
Locale parseLocaleWithSeparatorPlaceholder(String s, [String separator = '-']) {
10-
final parsed = s.split(separator);
11-
// ignore: unused_local_variable
12-
final subtags = parsed.skipWhile((value) => value != 'u').toList();
13-
final tags = parsed.takeWhile((value) => value != 'u').toList();
14-
final language = tags.first;
15-
final String? script;
16-
final String? region;
17-
if (tags.length == 2) {
18-
if (tags[1].length == 2 && tags[1] == tags[1].toUpperCase()) {
19-
region = tags[1];
20-
script = null;
21-
} else {
22-
region = null;
23-
script = tags[1];
24-
}
25-
} else if (tags.length == 3) {
26-
script = tags[1];
27-
region = tags[2];
28-
} else {
29-
script = null;
30-
region = null;
31-
}
10+
class IcuLocale extends Locale {
11+
final icu.Locale locale;
3212

33-
return Locale(
34-
language: language,
35-
region: region,
36-
script: script,
37-
);
13+
IcuLocale(this.locale)
14+
: super(
15+
language: locale.language,
16+
region: locale.region,
17+
script: locale.script,
18+
);
3819
}
3920

21+
Locale parseLocaleWithSeparatorPlaceholder(String s,
22+
[String separator = '-']) =>
23+
IcuLocale(icu.Locale.fromString(s));
24+
4025
//TODO: Switch to ICU4X!
4126
Locale parseLocale(String s, [String separator = '-']) {
4227
if (s.contains('_')) {

pkgs/intl4x/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: intl4x
22
description: >-
33
A lightweight modular library for internationalization (i18n) functionality.
4-
version: 0.10.1
4+
version: 0.10.2-wip
55
repository: https://github.com/dart-lang/i18n/tree/main/pkgs/intl4x
66
platforms:
77
web:

0 commit comments

Comments
 (0)