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 startupImplementations§
Source§impl LocalizationManager
impl LocalizationManager
Sourcepub fn new() -> Result<Self, LocalizationError>
pub fn new() -> Result<Self, LocalizationError>
Creates a new LocalizationManager with automatic language detection and translation loading.
This constructor performs several initialization steps:
- Loads all available translation files
- Detects system locale or loads saved language preference
- 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());Sourcepub fn get_text(&self, key: &str) -> String
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:
- Try current language translation
- Fall back to English if key is missing in current language
- 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");Sourcepub fn set_language(
&mut self,
language: Language,
) -> Result<(), LocalizationError>
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 diskSee also set_language_with_persistence for persistent language changes,
SystemLocaleDetector::detect for automatic detection, and SettingsManager
for settings management.
Sourcepub fn set_language_with_persistence(
&mut self,
language: Language,
) -> Result<(), LocalizationError>
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 startupSourcepub fn get_current_language(&self) -> Language
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);Sourcepub fn get_available_languages(&self) -> Vec<Language>
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());
}Sourcepub fn load_translations(
&mut self,
language: Language,
translations: TranslationMap,
)
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 fortranslations- 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
impl Default for LocalizationManager
Source§impl LanguageProvider for LocalizationManager
Implementation of LanguageProvider for LocalizationManager.
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.
Auto Trait Implementations§
impl Freeze for LocalizationManager
impl RefUnwindSafe for LocalizationManager
impl Send for LocalizationManager
impl Sync for LocalizationManager
impl Unpin for LocalizationManager
impl UnwindSafe for LocalizationManager
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().