Fork i18n + search + filtering- v0.2
at main 82 lines 3.1 kB view raw
1// Example demonstrating Phase 2 i18n template function integration 2use std::str::FromStr; 3use std::sync::Arc; 4 5use minijinja::{Environment, context}; 6use unic_langid::LanguageIdentifier; 7 8use smokesignal::i18n::{create_supported_languages, Locales}; 9use smokesignal::i18n::template_helpers::{register_i18n_functions, I18nTemplateContext}; 10 11fn main() -> Result<(), Box<dyn std::error::Error>> { 12 // Initialize the i18n system 13 let languages = create_supported_languages(); 14 let locales = Arc::new(Locales::new(languages.clone())); 15 16 // Set up template context 17 let current_locale = LanguageIdentifier::from_str("en-US")?; 18 let fallback_locale = LanguageIdentifier::from_str("en-US")?; 19 20 let i18n_context = I18nTemplateContext::new( 21 locales, 22 current_locale, 23 fallback_locale, 24 ); 25 26 // Create MiniJinja environment and register i18n functions 27 let mut env = Environment::new(); 28 register_i18n_functions(&mut env, i18n_context); 29 30 // Example 1: Basic translation function 31 let template = env.compile_expression("t('welcome')")?; 32 let result = template.eval(context!())?; 33 println!("Basic translation: {}", result); 34 35 // Example 2: Translation with arguments 36 let template = env.compile_expression("t('hello-user', name='Alice')")?; 37 let result = template.eval(context!())?; 38 println!("Translation with args: {}", result); 39 40 // Example 3: Translation with explicit locale 41 let template = env.compile_expression("tl('es-ES', 'welcome')")?; 42 let result = template.eval(context!())?; 43 println!("Explicit locale translation: {}", result); 44 45 // Example 4: Current locale 46 let template = env.compile_expression("current_locale()")?; 47 let result = template.eval(context!())?; 48 println!("Current locale: {}", result); 49 50 // Example 5: Check locale availability 51 let template = env.compile_expression("has_locale('en-US')")?; 52 let result = template.eval(context!())?; 53 println!("Has en-US locale: {}", result); 54 55 let template = env.compile_expression("has_locale('fr-FR')")?; 56 let result = template.eval(context!())?; 57 println!("Has fr-FR locale: {}", result); 58 59 // Example 6: Number formatting 60 let template = env.compile_expression("format_number(1234.56)")?; 61 let result = template.eval(context!())?; 62 println!("Formatted number: {}", result); 63 64 // Example 7: Pluralization 65 let template = env.compile_expression("plural(5, 'item-count', item='books')")?; 66 let result = template.eval(context!())?; 67 println!("Plural translation: {}", result); 68 69 // Example 8: Template with multiple i18n functions 70 let template_content = r#" 71 Current locale: {{ current_locale() }} 72 Welcome message: {{ t('welcome') }} 73 Has Spanish: {{ has_locale('es-ES') }} 74 Number: {{ format_number(42) }} 75 "#; 76 77 let template = env.from_str(template_content)?; 78 let result = template.render(context!())?; 79 println!("Complete template example:\n{}", result); 80 81 Ok(()) 82}