Skip to main content

Module localizer

Module localizer 

Source
Expand description

Localization / wording customization.

The Localizer trait is the central hook for customizing crate-authored wording. It is intentionally designed to be low-boilerplate:

  • Every method has a reasonable English default.
  • You can override only the pieces you care about, while inheriting all other defaults.

This crate may also show external message text coming from dependencies (for example granit-parser scan errors, or validator messages). Where such texts are used, the rendering pipeline should provide a best-effort opportunity to override them via Localizer::override_external_message.

§Example: override a single phrase

use serde_saphyr::{Error, Location};
use serde_saphyr::localizer::{Localizer, DEFAULT_ENGLISH_LOCALIZER};
use std::borrow::Cow;

/// A wrapper that overrides only location suffix wording, delegating everything else.
struct Pirate<'a> {
    base: &'a dyn Localizer,
}

impl Localizer for Pirate<'_> {
    fn attach_location<'b>(&self, base: Cow<'b, str>, loc: Location) -> Cow<'b, str> {
        if loc == Location::UNKNOWN {
            return base;
        }
        // Note: you can also delegate to `self.base.attach_location(...)` if you want.
        Cow::Owned(format!(
            "{base}. Bug lurks on line {}, then {} runes in",
            loc.line(),
            loc.column()
        ))
    }
}

// This snippet shows the customization building blocks; the crate's rendering APIs
// obtain a `Localizer` via the `MessageFormatter`.

Structs§

DefaultEnglishLocalizer
Default English localizer used by the crate.
ExternalMessage
A best-effort description of an external message.

Enums§

ExternalMessageSource
Where an “external” message comes from.

Statics§

DEFAULT_ENGLISH_LOCALIZER
A single shared instance of the default English localizer.

Traits§

Localizer
All crate-authored wording customization points.