Skip to main content

Locale

Struct Locale 

Source
pub struct Locale {
Show 16 fields pub locale: String, pub dates: DateTerms, pub roles: HashMap<ContributorRole, ContributorTerm>, pub locators: HashMap<LocatorType, LocatorTerm>, pub terms: Terms, pub punctuation_in_quote: bool, pub sort_articles: Vec<String>, pub locale_schema_version: Option<String>, pub evaluation: EvaluationConfig, pub messages: HashMap<String, String>, pub date_formats: HashMap<String, String>, pub number_formats: NumberFormats, pub grammar_options: GrammarOptions, pub legacy_term_aliases: HashMap<String, String>, pub vocab: VocabMap, pub evaluator: Arc<dyn MessageEvaluator>,
}
Expand description

A locale definition containing language-specific terms and formatting rules.

The evaluator field holds the message evaluation engine, selected based on evaluation.message_syntax. This allows for trait-based swapping to ICU4X implementations in the future without changing call sites.

Fields§

§locale: String

The locale identifier (e.g., “en-US”, “de-DE”).

§dates: DateTerms

Date-related terms (months, seasons).

§roles: HashMap<ContributorRole, ContributorTerm>

Contributor role terms (editor, translator, etc.).

§locators: HashMap<LocatorType, LocatorTerm>

Locator terms (page, chapter, etc.).

§terms: Terms

General terms (and, et al., etc.).

§punctuation_in_quote: bool

Whether to place periods/commas inside quotation marks. true = American style (“text.”), false = British style (“text”.)

§sort_articles: Vec<String>

Articles to strip from titles when sorting (e.g., “the”, “a”, “an” for English). These should be lowercase and will be matched case-insensitively.

§locale_schema_version: Option<String>

Schema version from the source locale file (None = legacy v1).

§evaluation: EvaluationConfig

Runtime evaluation configuration.

§messages: HashMap<String, String>

ICU MF1 messages keyed by message ID (populated for v2 locales).

§date_formats: HashMap<String, String>

Named date format presets: symbolic name → CLDR pattern.

§number_formats: NumberFormats

Number formatting options.

§grammar_options: GrammarOptions

Grammar options.

§legacy_term_aliases: HashMap<String, String>

Backwards-compatibility aliases: old term key → new message ID.

§vocab: VocabMap

Vocabulary maps for genre and medium display text.

§evaluator: Arc<dyn MessageEvaluator>

Message evaluator implementation (not serialized; set during load).

Implementations§

Source§

impl Locale

Source

pub fn from_yaml_str(yaml: &str) -> Result<Locale, String>

Load a locale from a YAML string.

§Errors

Returns an error when the YAML cannot be parsed into a locale.

Source

pub fn load(locale_id: &str, locales_dir: &Path) -> Locale

Load a locale by ID (e.g., “en-US”, “de-DE”) from a locales directory. Falls back to en-US if the locale file is not found.

Source

pub fn from_file(path: &Path) -> Result<Locale, String>

Load locale from a file path directly (detects format).

§Errors

Returns an error when the file cannot be read or its contents cannot be parsed as a supported locale format.

Source

pub fn parse_general_term(name: &str) -> Option<GeneralTerm>

Parse a locale term key into a structured general-term identifier.

Source

pub fn apply_override(&mut self, ov: &LocaleOverride)

Apply a partial override, merging its fields into this locale.

Performs key-by-key insertion or replacement for:

  • messages: new or updated message IDs
  • grammar_options: if Some, replaces the entire block and syncs punctuation_in_quote field
  • legacy_term_aliases: new or updated term aliases
Source§

impl Locale

Source

pub fn en_us() -> Locale

Create a new English (US) locale with default terms.

Source

pub fn strip_sort_articles<'a>(&self, s: &'a str) -> &'a str

Strip leading articles from a string for sorting.

Uses locale-specific articles (e.g., “the”, “a”, “an” for English; “der”, “die”, “das” for German). Falls back to English articles if no locale-specific articles are defined.

Source

pub fn lookup_genre(&self, key: &str) -> String

Look up display text for a genre canonical key.

Falls back to a readable form of the key if no translation found.

Source

pub fn lookup_medium(&self, key: &str) -> String

Look up display text for a medium canonical key.

Falls back to a readable form of the key if no translation found.

Source

pub fn role_term( &self, role: &ContributorRole, plural: bool, form: &TermForm, requested_gender: Option<GrammaticalGender>, ) -> Option<&str>

Get a contributor role term.

Source

pub fn role_term_neutral( &self, role: &ContributorRole, plural: bool, form: &TermForm, ) -> Option<&str>

Resolve a contributor role term using only neutral/common values.

Source

pub fn resolved_role_term( &self, role: &ContributorRole, plural: bool, form: &TermForm, requested_gender: Option<GrammaticalGender>, ) -> Option<String>

Resolve a contributor role term, evaluating MF2 messages when configured.

Source

pub fn resolved_role_term_neutral( &self, role: &ContributorRole, plural: bool, form: &TermForm, ) -> Option<String>

Resolve a contributor role term using only neutral/common values.

Source

pub fn locator_term( &self, locator: &LocatorType, plural: bool, form: &TermForm, requested_gender: Option<GrammaticalGender>, ) -> Option<&str>

Get a locator term.

Source

pub fn resolved_locator_term( &self, locator: &LocatorType, plural: bool, form: &TermForm, requested_gender: Option<GrammaticalGender>, ) -> Option<String>

Resolve a locator term, evaluating MF2 messages when configured.

Source

pub fn general_term( &self, term: &GeneralTerm, form: &TermForm, requested_gender: Option<GrammaticalGender>, ) -> Option<&str>

Resolve a general term to a borrowed string.

Source

pub fn resolved_general_term( &self, term: &GeneralTerm, form: &TermForm, requested_gender: Option<GrammaticalGender>, ) -> Option<String>

Resolve a general term, evaluating MF2 messages when configured.

Source

pub fn resolved_archive_term( &self, field: ArchiveHierarchyField, ) -> Option<String>

Resolve an archive hierarchy label, using MF2 messages. Returns singular form (count=1) by default.

Source

pub fn and_term(&self, use_symbol: bool) -> &str

Get the “and” term based on style preference.

Source

pub fn et_al(&self) -> &str

Get the “et al.” term.

Source

pub fn month_name(&self, month: u8, short: bool) -> &str

Get a month name.

Source

pub fn resolve_date_pattern( &self, message_id: &str, year: Option<&str>, month: Option<&str>, day: Option<u32>, ) -> Option<String>

Resolve a pattern.date-* message with locale-specific year/month/day components.

Returns Some(rendered) only when the locale carries an MF2 message at message_id and the evaluator produces output. Callers fall back to the engine’s hardcoded English assembly on None.

A component is forwarded to the evaluator only when non-empty; an authored pattern that references {$day} therefore yields None if the input date carries no day, letting the caller pick a shorter form.

The day argument is taken as Option<u32> rather than a pre-formatted string so the digit-to-string allocation is deferred until after the message lookup succeeds — the common case for legacy locales (en-US, every v1 file) is the lookup miss, which now incurs zero allocation.

Trait Implementations§

Source§

impl Clone for Locale

Source§

fn clone(&self) -> Locale

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 Debug for Locale

Source§

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

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

impl Default for Locale

Source§

fn default() -> Locale

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

impl<'de> Deserialize<'de> for Locale

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Locale, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Locale

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

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<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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

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> Same for T

Source§

type Output = T

Should always be Self
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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.