Skip to main content

Locale

Struct Locale 

Source
pub struct Locale { /* private fields */ }
Expand description

零堆、16 字节 Copy BCP-47 语言区域标签(支持 language[-Script][-REGION],兼容 #![no_std] 常量表)。

示例:Locale::EN_US(en-US)、Locale::ZH_CN(zh-CN)、Locale::ZH_TW(zh-TW)、Locale::JA_JP(ja-JP)、Locale::AR_SA(ar-SA)。

Implementations§

Source§

impl Locale

Source

pub const EN_US: Self

英语(美国)en-US。

Source

pub const ZH_CN: Self

简体中文(中国大陆)zh-CN。

Source

pub const ZH_TW: Self

繁体中文(中国台湾)zh-TW。

Source

pub const JA_JP: Self

日语(日本)ja-JP。

Source

pub const KO_KR: Self

韩语(韩国)ko-KR。

Source

pub const DE_DE: Self

德语(德国)de-DE。

Source

pub const FR_FR: Self

法语(法国)fr-FR。

Source

pub const ES_ES: Self

西班牙语(西班牙)es-ES。

Source

pub const RU_RU: Self

俄语(俄罗斯)ru-RU。

Source

pub const AR_SA: Self

阿拉伯语(沙特阿拉伯,RTL)ar-SA。

Source

pub const HE_IL: Self

希伯来语(以色列,RTL)he-IL。

Source

pub const fn from_lang(lang: &str) -> Self

编译期构造仅含语言码的 Locale(如 "zh"、"en"、"ar")。

Source

pub const fn from_lang_region(lang: &str, region: &str) -> Self

编译期构造 language-REGION 形式的 Locale(如 "zh", "CN" -> zh-CN)。

Source

pub const fn from_parts(lang: &str, script: &str, region: &str) -> Self

编译期构造完整 language-Script-REGION 形式的 Locale(如 "zh", "Hans", "CN")。

Source

pub fn parse(raw: &str) -> Option<Self>

解析 BCP-47 或 POSIX 风格语言标签(兼容 "zh_CN.UTF-8"、"zh-Hans-CN"、"en-us"、"ja")。

若输入为空或不包含有效 ASCII 字母语言子标签,返回 None。

Source

pub fn language(&self) -> &str

返回归一化小写语言子标签(如 "zh"、"en"、"ja")。

Source

pub fn script(&self) -> Option<&str>

返回可选的文字脚本子标签(如 Some("Hans") / Some("Hant"))。

Source

pub fn region(&self) -> Option<&str>

返回可选的大写地区子标签(如 Some("CN")、Some("US")、Some("TW"))。

Source

pub const fn without_script(self) -> Self

去除脚本子标签(如 zh-Hans-CN -> zh-CN)。

Source

pub const fn language_only(self) -> Self

退化为纯语言码(如 zh-CN -> zh),用于 BCP-47 层级回退查找。

Source

pub const fn same_language(self, other: Self) -> bool

判断两个 Locale 的主语言码是否一致(忽略地区与脚本差异)。

Source

pub fn text_direction(self) -> TextDirection

根据主语言码返回默认书写方向(ar / he / iw / fa / ur / ps / sd / yi -> TextDirection::Rtl)。

Source

pub fn is_rtl(self) -> bool

当前语言是否为从右到左(RTL)书写系统。

Source

pub fn format_tag<'b>(&self, buf: &'b mut [u8; 16]) -> &'b str

将规范化 BCP-47 标签零堆写入调用方提供的 16 字节栈缓冲区并返回切片(如 "zh-CN")。

Source

pub fn native_short_name(self) -> &'static str

返回当前语言友好的原生短标签(供工具栏语言切换按钮显示)。

Source

pub fn plural_category(self, count: i64) -> PluralCategory

按 Unicode CLDR 整数复数规则计算 count 所属的 PluralCategory。

Trait Implementations§

Source§

impl Clone for Locale

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Locale

Source§

impl Debug for Locale

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Locale

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Locale

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Locale

Source§

impl Hash for Locale

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Locale

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Locale

Auto Trait Implementations§

§

impl Forget for Locale

§

impl Freeze for Locale

§

impl Move for Locale

§

impl RefUnwindSafe for Locale

§

impl Send for Locale

§

impl Sync for Locale

§

impl Unpin for Locale

§

impl UnsafeUnpin for Locale

§

impl UnwindSafe for Locale

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> FluentBuilder for T

Source§

fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self

当 condition 为 true 时执行闭包修饰自身,否则原样返回。
Source§

fn when_else( self, condition: bool, then: impl FnOnce(Self) -> Self, else_fn: impl FnOnce(Self) -> Self, ) -> Self

根据 condition 在两个修饰分支之间二选一。
Source§

fn when_some<O>( self, option: Option<O>, then: impl FnOnce(Self, O) -> Self, ) -> Self

当 option 为 Some(value) 时执行闭包修饰自身,否则原样返回。
Source§

fn when_none<O>( self, option: &Option<O>, then: impl FnOnce(Self) -> Self, ) -> Self

当 option 为 None 时执行闭包修饰自身,否则原样返回。
Source§

fn apply<U>(self, f: impl FnOnce(Self) -> U) -> U

通用管道映射变换(避免与 Iterator::map / Option::map 重名)。
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,