Skip to content

Commit 501e97b

Browse files
authored
feat: add support for ICU (#54)
* feat: add support for ICU * include data
1 parent 3aca672 commit 501e97b

File tree

6 files changed

+40
-0
lines changed

6 files changed

+40
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ license-file = "./LICENSE_MIT"
1414
autoexamples = false
1515
include = [
1616
"src/*.rs",
17+
"src/icudtl.dat",
1718
"Cargo.toml",
1819
]
1920

src/icu.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// This setup is cloned from
2+
// https://github.com/denoland/deno_core_icudata/tree/main
3+
#[repr(C, align(16))]
4+
struct IcuData<T: ?Sized>(T);
5+
6+
static ICU_DATA_RAW: &IcuData<[u8]> = &IcuData(*include_bytes!("icudtl.dat"));
7+
8+
/// Raw ICU data.
9+
pub static ICU_DATA: &[u8] = &ICU_DATA_RAW.0;

src/icudtl.dat

9.98 MB
Binary file not shown.

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
//! .body(result)
144144
//! }
145145
//!```
146+
mod icu;
146147
mod ssr;
147148

148149
pub use ssr::{Ssr, SsrError};

src/ssr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ where
5252
let platform = v8::new_default_platform(0, false).make_shared();
5353
v8::V8::initialize_platform(platform);
5454
v8::V8::initialize();
55+
56+
v8::icu::set_common_data_74(crate::icu::ICU_DATA).expect("Failed to set ICU data");
5557
}
5658

5759
/// It creates a new SSR instance (multiple instances are allowed).

tests/intl.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use ssr_rs::Ssr;
2+
use std::sync::Once;
3+
4+
static INIT: Once = Once::new();
5+
6+
fn prepare() {
7+
INIT.call_once(|| {
8+
Ssr::create_platform();
9+
})
10+
}
11+
12+
#[test]
13+
fn it_supports_intl_namespace() {
14+
prepare();
15+
let mut js = Ssr::from(
16+
r##"var ENTRY = {x: () => {
17+
return Intl.DateTimeFormat().resolvedOptions().locale;
18+
}};"##
19+
.to_string(),
20+
"ENTRY",
21+
)
22+
.unwrap();
23+
24+
let html = js.render_to_string(None).unwrap();
25+
26+
assert_eq!(html, "en-US");
27+
}

0 commit comments

Comments
 (0)