pub struct Lang;Expand description
The main entry point for configuring and querying the localization system.
Lang manages process-global state behind a read/write lock. Configure it
once during startup, load the locales your application needs, and then use
t! or Lang::translate wherever translated text is needed.
Implementations§
Source§impl Lang
impl Lang
Sourcepub fn set_path(path: impl Into<String>)
pub fn set_path(path: impl Into<String>)
Sets the directory where language files are looked up.
Defaults to "locales". Call this before the first Lang::load if
your project stores files elsewhere.
§Examples
use lang_lib::Lang;
Lang::set_path("assets/lang");Sourcepub fn path() -> String
pub fn path() -> String
Returns the current language file path.
§Examples
use lang_lib::Lang;
Lang::set_path("assets/locales");
assert_eq!(Lang::path(), "assets/locales");Sourcepub fn set_locale(locale: impl Into<String>)
pub fn set_locale(locale: impl Into<String>)
Sets the active locale used when no locale is specified in t!.
The locale does not need to be loaded before calling this, but translations will be empty until it is.
This method is a good fit for single-user applications, CLIs, and
startup-time configuration. In request-driven servers, prefer passing
an explicit locale to Lang::translate or t! so one
request does not change another request’s active locale.
§Examples
use lang_lib::Lang;
Lang::set_locale("es");Sourcepub fn locale() -> String
pub fn locale() -> String
Returns the currently active locale.
§Examples
use lang_lib::Lang;
Lang::set_locale("fr");
assert_eq!(Lang::locale(), "fr");Sourcepub fn set_fallbacks(chain: Vec<String>)
pub fn set_fallbacks(chain: Vec<String>)
Sets the fallback locale chain.
When a key is not found in the requested locale, each fallback is
checked in order. The last resort is the inline fallback: argument
in t!, and if that is absent, the key itself is returned.
§Examples
use lang_lib::Lang;
Lang::set_fallbacks(vec!["en".to_string()]);Sourcepub fn load(locale: impl Into<String>) -> Result<(), LangError>
pub fn load(locale: impl Into<String>) -> Result<(), LangError>
Loads a locale from disk.
Reads {path}/{locale}.toml and stores all translations in memory.
Calling this a second time for the same locale replaces the existing
translations with a fresh load from disk.
Locale names must be single file stems such as en, en-US, or
pt_BR. Path separators and relative path components are rejected.
§Errors
Returns LangError::Io if the file cannot be read, or
LangError::Parse if the TOML is invalid.
§Examples
use lang_lib::Lang;
Lang::set_path("locales");
Lang::load("en").unwrap();
Lang::load("es").unwrap();Sourcepub fn load_from(locale: impl Into<String>, path: &str) -> Result<(), LangError>
pub fn load_from(locale: impl Into<String>, path: &str) -> Result<(), LangError>
Loads a locale from a specific path, ignoring the global path setting.
Useful when a project stores one locale separately from the others.
Locale names follow the same validation rules as Lang::load.
§Errors
Returns LangError::Io or LangError::Parse on failure.
§Examples
use lang_lib::Lang;
Lang::load_from("en", "tests/fixtures/locales").unwrap();Sourcepub fn is_loaded(locale: &str) -> bool
pub fn is_loaded(locale: &str) -> bool
Returns true if the locale has been loaded.
§Examples
use lang_lib::Lang;
Lang::load_from("en", "tests/fixtures/locales").unwrap();
assert!(Lang::is_loaded("en"));Sourcepub fn loaded() -> Vec<String>
pub fn loaded() -> Vec<String>
Returns a sorted list of all loaded locale identifiers.
Sorting keeps diagnostics and tests deterministic.
§Examples
use lang_lib::Lang;
Lang::load_from("es", "tests/fixtures/locales").unwrap();
Lang::load_from("en", "tests/fixtures/locales").unwrap();
assert_eq!(Lang::loaded(), vec!["en".to_string(), "es".to_string()]);Sourcepub fn unload(locale: &str)
pub fn unload(locale: &str)
Unloads a locale and frees its memory.
Unloading a locale does not change the active locale or fallback chain. If either of those still references the removed locale, translation will simply skip it.
§Examples
use lang_lib::Lang;
Lang::load_from("en", "tests/fixtures/locales").unwrap();
Lang::unload("en");
assert!(!Lang::is_loaded("en"));Sourcepub fn translator(locale: impl Into<String>) -> Translator
pub fn translator(locale: impl Into<String>) -> Translator
Creates a request-scoped Translator for the provided locale.
This is a convenience wrapper around Translator::new. It is most
useful in server code where locale is resolved per request and passed
through the handler stack.
§Examples
use lang_lib::Lang;
let translator = Lang::translator("es");
assert_eq!(translator.locale(), "es");Sourcepub fn translate(
key: &str,
locale: Option<&str>,
fallback: Option<&str>,
) -> String
pub fn translate( key: &str, locale: Option<&str>, fallback: Option<&str>, ) -> String
Translates a key.
Lookup order:
- The requested locale (or active locale if
None) - Each locale in the fallback chain, in order
- The inline
fallbackstring if provided - The key itself (never returns an empty string)
This is the function called by the t! macro. Prefer
using the macro directly in application code.
In concurrent server code, passing Some(locale) is usually the safest
policy because it avoids mutating the process-wide active locale.
§Examples
use lang_lib::Lang;
Lang::load_from("en", "tests/fixtures/locales").unwrap();
let text = Lang::translate("welcome", Some("en"), Some("Welcome"));
assert_eq!(text, "Welcome");