Skip to content

Commit 3855599

Browse files
committed
docs: add fluent readme.
1 parent 2b32ac0 commit 3855599

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

serde_valid/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,35 @@ assert_eq!(
158158
);
159159
```
160160

161+
### Fluent localization
162+
163+
You can also use [fluent](https://projectfluent.org/) localization by using `fluent` feature.
164+
165+
```rust
166+
use unic_langid::LanguageIdentifier;
167+
use serde_json::json;
168+
use serde_valid::{fluent::Localize, Validate};
169+
170+
#
171+
172+
#[derive(Validate)]
173+
struct Data (
174+
#[validate(min_length = 3, fluent("name-min-length", min_length = 3))]
175+
String,
176+
);
177+
178+
assert_eq!(
179+
Data("田中".to_string()).validate()
180+
.unwrap_err()
181+
.localize(&get_bundle("name-min-length = 名前の長さは { $min_length } 文字以上でないといけません。"))
182+
.to_string(),
183+
json!({
184+
"errors": ["名前の長さは \u{2068}3\u{2069} 文字以上でないといけません。"]
185+
})
186+
.to_string()
187+
);
188+
```
189+
161190
## Custom method
162191

163192
You can use your custom validation using by `#[validate(custom)]`.

serde_valid/src/lib.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,45 @@
158158
//! );
159159
//! ```
160160
//!
161+
//! ### Fluent localization
162+
//!
163+
//! You can also use [fluent](https://projectfluent.org/) localization by using `fluent` feature.
164+
//!
165+
//! ```rust
166+
//! # #[cfg(feature = "fluent")] {
167+
//! # use fluent::{FluentBundle, FluentResource};
168+
//! use unic_langid::LanguageIdentifier;
169+
//! use serde_json::json;
170+
//! use serde_valid::{fluent::Localize, Validate};
171+
//!
172+
//! # fn get_bundle(source: impl Into<String>) -> FluentBundle<FluentResource> {
173+
//! # let res = FluentResource::try_new(source.into()).expect("Failed to parse an FTL string.");
174+
//! # let langid_en: LanguageIdentifier = "ja_JP".parse().expect("Parsing failed");
175+
//! # let mut bundle = FluentBundle::new(vec![langid_en]);
176+
//! # bundle.add_resource(res).unwrap();
177+
//! # bundle
178+
//! # }
179+
//! #
180+
//!
181+
//! #[derive(Validate)]
182+
//! struct Data (
183+
//! #[validate(min_length = 3, fluent("name-min-length", min_length = 3))]
184+
//! String,
185+
//! );
186+
//!
187+
//! assert_eq!(
188+
//! Data("田中".to_string()).validate()
189+
//! .unwrap_err()
190+
//! .localize(&get_bundle("name-min-length = 名前の長さは { $min_length } 文字以上でないといけません。"))
191+
//! .to_string(),
192+
//! json!({
193+
//! "errors": ["名前の長さは \u{2068}3\u{2069} 文字以上でないといけません。"]
194+
//! })
195+
//! .to_string()
196+
//! );
197+
//! # }
198+
//! ```
199+
//!
161200
//! ## Custom method
162201
//!
163202
//! You can use your custom validation using by `#[validate(custom)]`.

serde_valid/tests/fluent_test.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ mod tests {
66
use serde_valid::{fluent::Localize, Validate};
77
use unic_langid::LanguageIdentifier;
88

9-
fn get_bundle() -> FluentBundle<FluentResource> {
10-
let ftl_string = ["hello-world = Hello, world!", "intro = Welcome, { $name }."]
11-
.join("\n")
12-
.to_string();
13-
let res = FluentResource::try_new(ftl_string).expect("Failed to parse an FTL string.");
9+
fn get_bundle(source: impl Into<String>) -> FluentBundle<FluentResource> {
10+
let res = FluentResource::try_new(source.into()).expect("Failed to parse an FTL string.");
1411

1512
let langid_en: LanguageIdentifier = "en-US".parse().expect("Parsing failed");
1613
let mut bundle = FluentBundle::new(vec![langid_en]);
@@ -30,7 +27,10 @@ mod tests {
3027
}
3128

3229
let test = Test { a: 1, b: 11 };
33-
let a = test.validate().unwrap_err().localize(&get_bundle());
30+
let a = test.validate().unwrap_err().localize(&get_bundle(
31+
["hello-world = Hello, world!", "intro = Welcome, { $name }."].join("\n"),
32+
));
33+
3434
assert_eq!(
3535
a.to_string(),
3636
json!({

0 commit comments

Comments
 (0)