#[diplomat::opaque] /// An ICU4X Locale, capable of representing strings like `"en-US"`. #[diplomat::rust_link(icu::locale::Locale, Struct)] #[diplomat::rust_link(icu::locale::DataLocale, Struct, hidden)] #[diplomat::rust_link(icu::locale::DataLocale::into_locale, FnInStruct, hidden)] pubstruct Locale(pub icu_locale_core::Locale);
impl Locale { /// Construct an [`Locale`] from an locale identifier. /// /// This will run the complete locale parsing algorithm. If code size and /// performance are critical and the locale is of a known shape (such as /// `aa-BB`) use `create_und`, `set_language`, `set_script`, and `set_region`. #[diplomat::rust_link(icu::locale::Locale::try_from_str, FnInStruct)] #[diplomat::rust_link(icu::locale::Locale::try_from_utf8, FnInStruct, hidden)] #[diplomat::rust_link(icu::locale::Locale::from_str, FnInStruct, hidden)] #[diplomat::rust_link(icu::locale::DataLocale::from_str, FnInStruct, hidden)] #[diplomat::rust_link(icu::locale::DataLocale::try_from_str, FnInStruct, hidden)] #[diplomat::rust_link(icu::locale::DataLocale::try_from_utf8, FnInStruct, hidden)] #[diplomat::attr(all(supports = fallible_constructors, supports = named_constructors), named_constructor)] #[diplomat::demo(default_constructor)] pubfn from_string(name: &DiplomatStr) -> Result<Box<Locale>, LocaleParseError> {
Ok(Box::new(Locale(icu_locale_core::Locale::try_from_utf8(
name,
)?)))
}
/// Returns a string representation of the `LanguageIdentifier` part of /// [`Locale`]. #[diplomat::rust_link(icu::locale::Locale::id, StructField)] #[diplomat::attr(auto, getter)] pubfn basename(&self, write: &mut diplomat_runtime::DiplomatWrite) { let _infallible = self.0.id.write_to(write);
}
/// Returns a string representation of the unicode extension. #[diplomat::rust_link(icu::locale::Locale::extensions, StructField)] pubfn get_unicode_extension(
&self,
s: &DiplomatStr,
write: &mut diplomat_runtime::DiplomatWrite,
) -> Option<()> {
icu_locale_core::extensions::unicode::Key::try_from_utf8(s)
.ok()
.and_then(|k| self.0.extensions.unicode.keywords.get(&k))
.map(|v| { let _infallible = v.write_to(write);
})
}
/// Set a Unicode extension. #[diplomat::rust_link(icu::locale::Locale::extensions, StructField)] pubfn set_unicode_extension(&mutself, k: &DiplomatStr, v: &DiplomatStr) -> Option<()> { let k = icu_locale_core::extensions::unicode::Key::try_from_utf8(k).ok()?; let v = icu_locale_core::extensions::unicode::Value::try_from_utf8(v).ok()?; self.0.extensions.unicode.keywords.set(k, v);
Some(())
}
/// Returns a string representation of [`Locale`] language. #[diplomat::rust_link(icu::locale::Locale::id, StructField)] #[diplomat::attr(auto, getter)] pubfn language(&self, write: &mut diplomat_runtime::DiplomatWrite) { let _infallible = self.0.id.language.write_to(write);
}
/// Set the language part of the [`Locale`]. #[diplomat::rust_link(icu::locale::Locale::try_from_str, FnInStruct)] #[diplomat::rust_link(icu::locale::Locale::try_from_utf8, FnInStruct, hidden)] #[diplomat::attr(auto, setter = "language")] pubfn set_language(&mutself, s: &DiplomatStr) -> Result<(), LocaleParseError> { self.0.id.language = if s.is_empty() {
icu_locale_core::subtags::Language::UNKNOWN
} else {
icu_locale_core::subtags::Language::try_from_utf8(s)?
};
Ok(())
}
/// Returns a string representation of [`Locale`] region. #[diplomat::rust_link(icu::locale::Locale::id, StructField)] #[diplomat::attr(auto, getter)] pubfn region(&self, write: &mut diplomat_runtime::DiplomatWrite) -> Option<()> { self.0.id.region.map(|region| { let _infallible = region.write_to(write);
})
}
/// Set the region part of the [`Locale`]. #[diplomat::rust_link(icu::locale::Locale::try_from_str, FnInStruct)] #[diplomat::rust_link(icu::locale::Locale::try_from_utf8, FnInStruct, hidden)] #[diplomat::attr(all(supports = accessors, not(dart)), setter = "region")] pubfn set_region(&mutself, s: &DiplomatStr) -> Result<(), LocaleParseError> { self.0.id.region = if s.is_empty() {
None
} else {
Some(icu_locale_core::subtags::Region::try_from_utf8(s)?)
};
Ok(())
}
/// Returns a string representation of [`Locale`] script. #[diplomat::rust_link(icu::locale::Locale::id, StructField)] #[diplomat::attr(auto, getter)] pubfn script(&self, write: &mut diplomat_runtime::DiplomatWrite) -> Option<()> { self.0.id.script.map(|script| { let _infallible = script.write_to(write);
})
}
/// Set the script part of the [`Locale`]. Pass an empty string to remove the script. #[diplomat::rust_link(icu::locale::Locale::try_from_str, FnInStruct)] #[diplomat::rust_link(icu::locale::Locale::try_from_utf8, FnInStruct, hidden)] #[diplomat::attr(all(supports = accessors, not(dart)), setter = "script")] pubfn set_script(&mutself, s: &DiplomatStr) -> Result<(), LocaleParseError> { self.0.id.script = if s.is_empty() {
None
} else {
Some(icu_locale_core::subtags::Script::try_from_utf8(s)?)
};
Ok(())
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.