LocalizationManager

Struct LocalizationManager 

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

Central manager for all localization operations in Inspector GGUF.

The LocalizationManager coordinates translation loading, language switching, and text retrieval with automatic fallback mechanisms. It serves as the primary interface for all internationalization needs in the application.

The manager integrates with TranslationLoader for file operations, SystemLocaleDetector for automatic language detection, SettingsManager for persistent preferences, and Language for supported language variants.

§Features

  • Automatic Language Detection: Detects system locale on initialization
  • Fallback System: Falls back to English, then to key names if translations are missing
  • Persistent Settings: Integrates with settings system for user preferences
  • Thread-Safe Design: Can be safely shared across threads when wrapped appropriately
  • Validation: Ensures translation completeness and format correctness

§Examples

§Basic Usage

use inspector_gguf::localization::{LocalizationManager, Language};

// Create manager with automatic language detection
let mut manager = LocalizationManager::new()?;

// Get translated text
let app_title = manager.get_text("app.title");
let load_button = manager.get_text("buttons.load");

// Switch language
manager.set_language(Language::Russian)?;
let russian_title = manager.get_text("app.title");

§With Persistent Settings

use inspector_gguf::localization::{LocalizationManager, Language};

let mut manager = LocalizationManager::new()?;

// Change language and save preference
manager.set_language_with_persistence(Language::PortugueseBrazilian)?;

// Language preference will be restored on next startup

Implementations§

Source§

impl LocalizationManager

Source

pub fn new() -> Result<Self, LocalizationError>

Creates a new LocalizationManager with automatic language detection and translation loading.

This constructor performs several initialization steps:

  1. Loads all available translation files
  2. Detects system locale or loads saved language preference
  3. Sets up fallback mechanisms for missing translations
§Returns

Returns a configured LocalizationManager ready for use, or a LocalizationError if critical translation files (especially English) cannot be loaded.

§Errors

Returns an error if:

  • The English translation file is missing or corrupted (required for fallback)
  • Translation files have invalid JSON format
  • Required translation sections are missing
§Examples
use inspector_gguf::localization::LocalizationManager;

let manager = LocalizationManager::new()?;
println!("Current language: {:?}", manager.get_current_language());
Source

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

Retrieves translated text for the specified key with automatic fallback.

This method implements a three-tier fallback system:

  1. Try current language translation
  2. Fall back to English if key is missing in current language
  3. Return the key itself if no translation is found

Keys use dot notation to access nested translation structures (e.g., “buttons.load” accesses translations["buttons"]["load"]).

§Arguments
  • key - Translation key in dot notation (e.g., “app.title”, “buttons.load”)
§Returns

Returns the translated string, or the key itself if no translation is available. This method never panics and always returns a valid string.

§Examples
use inspector_gguf::localization::{LocalizationManager, Language};

let mut manager = LocalizationManager::new()?;
manager.set_language(Language::English)?;

// Get simple translation
let title = manager.get_text("app.title");
assert_eq!(title, "Inspector GGUF");

// Get nested translation
let load_button = manager.get_text("buttons.load");
assert_eq!(load_button, "Load");

// Non-existent key returns the key itself
let missing = manager.get_text("non.existent.key");
assert_eq!(missing, "non.existent.key");
Source

pub fn set_language( &mut self, language: Language, ) -> Result<(), LocalizationError>

Sets the current language without persisting the preference.

Changes the active language for translation lookups. This change is temporary and will not be saved to user settings. Use set_language_with_persistence if you want to save the language preference.

§Arguments
  • language - The language to switch to
§Returns

Returns Ok(()) on success, or a LocalizationError if the language is not supported or translations are not available.

§Examples
use inspector_gguf::localization::{LocalizationManager, Language};

let mut manager = LocalizationManager::new()?;

// Temporarily switch to Russian
manager.set_language(Language::Russian)?;
let russian_title = manager.get_text("app.title");

// Language preference is not saved to disk

See also set_language_with_persistence for persistent language changes, SystemLocaleDetector::detect for automatic detection, and SettingsManager for settings management.

Source

pub fn set_language_with_persistence( &mut self, language: Language, ) -> Result<(), LocalizationError>

Sets the current language and saves the preference to persistent storage.

This method changes the active language and attempts to save the preference to the user’s settings file. The saved preference will be restored when the application is restarted.

§Arguments
  • language - The language to switch to and save as preference
§Returns

Returns Ok(()) on success. If the language change succeeds but saving the preference fails, the method still returns Ok(()) but logs a warning.

§Errors

Returns an error if the language is not supported or translations are not available. Settings save failures are logged but do not cause the method to fail.

§Examples
use inspector_gguf::localization::{LocalizationManager, Language};

let mut manager = LocalizationManager::new()?;

// Switch to Portuguese and save preference
manager.set_language_with_persistence(Language::PortugueseBrazilian)?;

// Preference will be restored on next application startup
Source

pub fn get_current_language(&self) -> Language

Returns the currently active language.

§Returns

The currently selected language for translation lookups.

§Examples
use inspector_gguf::localization::{LocalizationManager, Language};

let manager = LocalizationManager::new()?;
let current = manager.get_current_language();
println!("Current language: {:?}", current);
Source

pub fn get_available_languages(&self) -> Vec<Language>

Returns a list of all supported languages.

This method returns all languages that the application supports, regardless of whether their translation files are currently loaded.

§Returns

A vector containing all supported language variants.

§Examples
use inspector_gguf::localization::LocalizationManager;

let manager = LocalizationManager::new()?;
let languages = manager.get_available_languages();

for lang in languages {
    println!("Supported: {} ({})", lang.display_name(), lang.to_code());
}
Source

pub fn load_translations( &mut self, language: Language, translations: TranslationMap, )

Loads or replaces translations for a specific language.

This method allows manual loading of translation data, which can be useful for testing, dynamic translation loading, or custom translation sources.

§Arguments
  • language - The language to load translations for
  • translations - The translation data as a nested HashMap structure
§Examples
use inspector_gguf::localization::{LocalizationManager, Language};
use std::collections::HashMap;
use serde_json::json;

let mut manager = LocalizationManager::new()?;

// Create custom translations
let mut custom_translations = HashMap::new();
custom_translations.insert("app".to_string(), json!({"title": "Custom Title"}));

// Load custom translations
manager.load_translations(Language::English, custom_translations);

Trait Implementations§

Source§

impl Default for LocalizationManager

Source§

fn default() -> Self

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

impl LanguageProvider for LocalizationManager

Implementation of LanguageProvider for LocalizationManager.

This implementation delegates to the manager’s internal translation lookup system, providing access to all loaded translations with automatic fallback.

Source§

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

Retrieves translated text for the specified key. Read more
Source§

fn t_with_args(&self, key: &str, args: &[&str]) -> String

Retrieves translated text with parameter substitution. Read more

Auto Trait Implementations§

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
§

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

§

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> NoneValue for T
where T: Default,

Source§

type NoneType = T

Source§

fn null_value() -> T

The none-equivalent value.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

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

Source§

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