You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorials/date-picker-data.md
+43-57Lines changed: 43 additions & 57 deletions
Original file line number
Diff line number
Diff line change
@@ -11,8 +11,8 @@ Data generation is done using the `icu4x-datagen` tool, which pulls data from [U
11
11
Verify that Rust is installed. If it's not, you can install it in a few seconds from [https://rustup.rs/](https://rustup.rs/).
12
12
13
13
```console
14
-
$ cargo --version
15
-
cargo 1.81.0 (2dbb1af80 2024-08-20)
14
+
cargo --version
15
+
# cargo 1.86.0 (adf9b6ad1 2025-02-28)
16
16
```
17
17
18
18
Now you can run
@@ -26,7 +26,7 @@ cargo install icu4x-datagen
26
26
We're ready to generate the data. We will use the blob format, and create a blob that will contain just Chakma data. At runtime we can then load it as needed.
27
27
28
28
```console
29
-
$ icu4x-datagen --markers all --locales ccp --format blob --out ccp.blob
29
+
icu4x-datagen --markers all --locales ccp --format blob --out ccp.blob
30
30
```
31
31
32
32
This will generate a `ccp.blob` file containing data for Chakma.
@@ -41,7 +41,7 @@ This will generate a `ccp.blob` file containing data for Chakma.
41
41
To use blob data, we will need to add the `icu_provider_blob` crate to our project:
42
42
43
43
```console
44
-
cargo add icu_provider_blob
44
+
cargo add icu_provider_blob --features alloc
45
45
```
46
46
47
47
We also need to enable the `serde` feature on the `icu` crate to enable deserialization support:
@@ -58,29 +58,23 @@ locale is Chakma:
58
58
useicu::locale::locale;
59
59
useicu_provider_blob::BlobDataProvider;
60
60
61
-
// Just below the imports (fill in the path):
62
-
constCCP_BLOB_PATH:&str="<absolute path to ccp.blob>";
63
-
64
-
letdatetime_formatter=iflocale==locale!("ccp") {
61
+
// replace the date_formatter creation
62
+
letdate_formatter=iflocale==locale!("ccp") {
65
63
println!("Using buffer provider");
66
64
67
-
letblob=std::fs::read(CCP_BLOB_PATH)
65
+
letblob=std::fs::read("ccp.blob")
68
66
.expect("blob should read successfully")
69
67
.into();
70
68
71
69
letprovider=
72
70
BlobDataProvider::try_new_from_blob(blob).expect("deserialization should succeed");
@@ -134,67 +125,62 @@ Note: the following steps are currently only possible in Rust. 🤷
134
125
When we ran `icu4x-datagen`, we passed `--markers all`, which make it generate *all* data for the Chakma locale, even though we only need date formatting. We can make `icu4x-datagen` analyze our binary to figure out which markers are needed:
Note: you usually want to build with the `--release` flag, and analyze that binary, but we don't have all day.
132
+
Note: you usually want to build with the `--release` flag, and analyze that binary.
141
133
142
134
This should generate a lot fewer markers!
143
135
144
136
Let's look at the sizes:
145
137
146
138
```console
147
-
$ wc -c *.blob
148
-
656767 ccp.blob
149
-
45471 ccp_smaller.blob
139
+
wc -c *.blob
140
+
# 5448603 ccp.blob
141
+
# 13711 ccp_smaller.blob
150
142
```
151
143
152
144
This is much better! Rerun your app with `ccp_smaller.blob` to make sure it still works!
153
145
154
146
## 5. Slimming the data pack ... again
155
147
156
-
The last datagen invocation still produced a lot of markers, as you saw in its output. This is because we used the `DateFormatter` API, which can format dates for a lot of different calendars. However, if we are only using it with an Gregorian calendar date, so we don't need Coptic, Indian, etc. date formatting data.
148
+
The last datagen invocation still produced a lot of markers, as you saw in its output. This is because we used the `DateTimeFormatter` API, which can format dates for a lot of different calendars (remember `en-u-ca-hebrew`). However, if we were only using it with a Gregorian calendar date, we wouldn't need Coptic, Indian, etc. date formatting data. Now, how do we communicate this to `--markers-for-bin`? Turns out, `icu::datetime` also exposes a `FixedCalendarDateTimeFormatter`, which is generic in a single calendar type. If you use this API instead, `--markers-for-bin` will only include the markers for that one calendar type.
157
149
158
-
We've seen that `DateFormatter` pulls in a lot of data. It would be nice if we could tell it that we'll only ever use it with Gregorian dates. Turns out we can! `icu::datetime` also exposes a `TypedDateFormatter<C>`, which is generic in a single calendar type. If you use this API instead (instantiated as `TypedDateFormatter<Gregorian>`), `--markers-for-bin` will give you exactly the markers we manually selected in the last section. However, now you can be sure that you didn't make a mistake selecting the markers (which would be an awkward runtime error), and that you will never accidentally pass a non-Gregorian date into the formatter (which would an awkward runtime error with `DateFormatter`, but is a compile-time error with `TypeDateFormatter`).
150
+
Replace the `DateTimeFormatter::try_new` calls with `FixedCalendarDateTimeFormatter::try_new`, and change the `format` invocation to convert the input to the Gregorian calendar:
The generic type of `FixedCalendarDateTimeFormatter` will be inferred from the input, which now has type `&Date<Gregorian>` now. Unlike `DateTimeFormatter`, `FixedCalendarDateTimeFormatter` never applies calendar conversions on its input, so it will be a `FixedCalendarDateTimeFormatter<Gregorian, ...>`.
157
+
174
158
Now we can run datagen with `--markers-for-bin` again:
Copy file name to clipboardExpand all lines: tutorials/date-picker.md
+30-41Lines changed: 30 additions & 41 deletions
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ Verify that Rust is installed. If it's not, you can install it in a few seconds
14
14
15
15
```console
16
16
cargo --version
17
-
# cargo 1.81.0 (2dbb1af80 2024-08-20)
17
+
# cargo 1.86.0 (adf9b6ad1 2025-02-28)
18
18
```
19
19
20
20
Create a new Rust binary crate with icu4x as a dependency:
@@ -30,7 +30,7 @@ cargo add icu
30
30
We recommend using [CodePen](https://codepen.io/pen/?editors=1011) to follow along. To load ICU4X into CodePen, you can use this snippet in the JavaScript editor:
This loads the full development ICU4X WebAssembly file. Since it may take some time to load on slow connections, we'll create a loading div. In future tutorials you will learn how to build an optimized WebAssembly file, reducing the size of the WASM file by 99% or more. Add this to your HTML:
@@ -86,8 +86,8 @@ let locale = match locale_str.trim().parse::<Locale>() {
86
86
Try inputting locales in non-canonical syntax and see them normalized!
87
87
88
88
```bash
89
-
$ cargo run
90
-
Enter your locale: DE_CH
89
+
cargo run
90
+
Enter your locale: DE-CH
91
91
You entered: de-CH
92
92
```
93
93
@@ -108,10 +108,13 @@ And in JavaScript:
108
108
functionupdate() {
109
109
try {
110
110
let localeStr =document.getElementById("localeinput").value;
111
-
let locale =ICU4XLocale.create_from_string(localeStr);
Try this in several locales, like `en` (English), `en-GB` (British English), and `th` (Thai). Observe how differently dates are represented in locales around the world! You can explicitly specify arbitrary calendar systems using the `u-ca` Unicode extension keyword in the locale. Try `en-u-ca-hebrew`!
@@ -223,22 +212,22 @@ Now we would also like to format the current time.
223
212
224
213
### Rust Part 4
225
214
226
-
Use the API documentation for [`icu::time::DateTime`](https://docs.rs/icu/latest/icu/timezone/struct.DateTime.html) and [`icu::datetime::DateTimeFormatter`](https://docs.rs/icu/latest/icu/datetime/struct.DateTimeFormatter.html) to expand your app to format both date and time.
227
-
228
-
Hint: You can use `Default::default()` for the `DateTimeFormatterOptions` argument.
215
+
Use the API documentation for [`icu::time::DateTime`](https://docs.rs/icu/latest/icu/time/struct.DateTime.html) and [`icu::datetime::fieldsets`](https://docs.rs/icu/latest/icu/datetime/fieldsets/index.html) to expand your app to format both date and time.
229
216
230
217
### JavaScript Part 4
231
218
232
-
Use the API documentation for [`ICU4XDateTime`](https://unicode-org.github.io/icu4x/tsdoc/classes/ICU4XDateTime.html) and [`ICU4XDateTimeFormatter`](https://unicode-org.github.io/icu4x/tsdoc/classes/ICU4XDateTimeFormatter.html) to expand your app to format both a date and a time.
219
+
Use the API documentation for [`Time`](https://icu4x.unicode.org/2_0/tsdoc/classes/Time.html) and [`DateTimeFormatter`](https://icu4x.unicode.org/2_0/tsdoc/classes/DateTimeFormatter.html) to expand your app to format both a date and a time.
0 commit comments