Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for icu_locale #250

Merged
merged 4 commits into from
Sep 22, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions components/locale/examples/langid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Simple app
use icu_locale::{subtags, LanguageIdentifier};

const LANGIDS: &[&str] = &[
"de",
"en-US",
"zh-Hant",
"sr-Cyrl",
"fr-CA",
"es-CL",
"pl",
"en-Latn-US",
"ca-valencia",
"und-Arab",
];

fn main() {
// 1. Parse input strings into LanguageIdentifiers
let langids = LANGIDS
.iter()
.map(|s| s.parse())
.collect::<Result<Vec<LanguageIdentifier>, _>>()
.expect("Failed to parse Language Identifier");

{
// 2. Filter list of identifiers that match a given Language Identifier.
let reference: LanguageIdentifier = "en-US"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use your macro here instead? There should no longer be any reason to hard-code a "...".parse().expect("...") pattern for LanguageIdentifier.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we merge the macros! :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to use the macros in this example. Hopefully the macros will be checked in soon.

.parse()
.expect("Failed to parse Language Identifier");

let _matching = langids.iter().filter(|langid| *langid == &reference);
}

{
// 3. Filter list of identifiers that match a given language subtag.
let reference: subtags::Language = "en".parse().expect("Failed to parse Language");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise: use language! (or icu_language!)


let _matching = langids.iter().filter(|langid| langid.language == reference);
}

// 4. Serialize Language Identifiers to a vector of strings.
let _canonicalized: Vec<_> = langids.iter().map(|langid| langid.to_string()).collect();
}