Skip to main content

rustbasic_translatable/
lib.rs

1pub mod translator;
2pub mod middleware;
3
4// Re-export the RustBasic middleware for easy integration in parent routers
5pub use middleware::translatable_middleware;
6
7/// Translate a key using the active request-scoped locale automatically
8pub fn trans(key: &str) -> String {
9    let locale = middleware::get_locale();
10    translator::TRANSLATOR.trans(key, &locale)
11}
12
13/// Translate a key with dynamic placeholder variables using the active request-scoped locale
14pub fn trans_with(key: &str, params: &[(&str, &str)]) -> String {
15    let locale = middleware::get_locale();
16    translator::TRANSLATOR.trans_with(key, &locale, params)
17}
18
19/// Standard global translation shortcut function, identical to trans
20pub fn __(key: &str) -> String {
21    trans(key)
22}
23
24/// Initialize and load all JSON translation files from the specified folder
25pub fn init<P: AsRef<std::path::Path>>(lang_dir: P) -> Result<(), String> {
26    translator::TRANSLATOR.init(lang_dir)
27}
28
29/// Set default fallback language locale (e.g. "id")
30pub fn set_default_locale(locale: &str) {
31    translator::TRANSLATOR.set_default_locale(locale);
32}
33
34/// Get current default fallback language locale
35pub fn get_default_locale() -> String {
36    translator::TRANSLATOR.get_default_locale()
37}