mintrt 0.1.1

Security-featured runtime for lua.
//! 国际化使用示例
//! 
//! 此文件展示如何在 Mint Runtime 中使用 i10n 国际化功能

#[allow(dead_code)]
mod examples {
    use crate::i10n;

    /// 示例 1: 基础翻译使用
    pub fn example_basic_translation() {
        // 初始化国际化系统(通常在 main 函数中调用一次即可)
        i10n::init_i18n();
        
        // 获取翻译
        let runtime_init_text = i10n::t("runtime_init");
        println!("{}", runtime_init_text);  // 输出:运行时初始化
        
        let security_check_text = i10n::t("security_check");
        println!("{}", security_check_text);  // 输出:安全检查
    }

    /// 示例 2: 在日志中使用国际化
    pub fn example_logging_with_i18n() {
        i10n::init_i18n();
        
        // 使用 info_i18n! 宏
        info_i18n!("runtime_init");
        
        // 带参数的日志
        let module_name = "my_module";
        info_i18n!("module_loaded", ": {}", module_name);
        
        // 错误日志
        error_i18n!("lua_error", ": 无效的语法");
        
        // 警告日志
        warn_i18n!("permission_denied", ": 访问受限资源");
    }

    /// 示例 3: 自定义翻译键
    pub fn example_custom_translation_key() {
        // 在实际使用中,你可以在 init_i18n() 中添加自己的翻译键
        // 然后像这样使用:
        let custom_message = i10n::t("task_complete");
        println!("任务状态:{}", custom_message);
    }

    /// 示例 4: 处理不存在的翻译键
    pub fn example_missing_translation() {
        // 如果翻译键不存在,会返回键本身
        let missing = i10n::t("nonexistent_key");
        assert_eq!(missing, "nonexistent_key");
        println!("未找到翻译时返回键名:{}", missing);
    }

    /// 示例 5: 组合使用多个翻译
    pub fn example_multiple_translations() {
        i10n::init_i18n();
        
        println!("=== 系统启动 ===");
        log::info!("{}: 开始", i10n::t("runtime_init"));
        log::info!("{}: 配置加载完成", i10n::t("config_load"));
        log::info!("{}: 通过", i10n::t("bytecode_verify"));
        log::info!("{}: 完成", i10n::t("task_complete"));
    }

    /// 示例 6: 在错误处理中使用
    pub fn example_error_handling() {
        i10n::init_i18n();
        
        let result: Result<(), &str> = Err("some error");
        
        match result {
            Ok(_) => info_i18n!("task_complete"),
            Err(e) => {
                error_i18n!("lua_error", ": {}", e);
                // 或者使用标准日志 + 翻译
                log::error!("{}: {}", i10n::t("permission_denied"), e);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_all_examples_compile() {
        // 这些示例仅用于演示,实际运行时需要在 main 中初始化
        i10n::init_i18n();
        
        // 测试基础翻译
        assert_eq!(i10n::t("runtime_init"), "运行时初始化");
        assert_eq!(i10n::t("security_check"), "安全检查");
        
        // 测试不存在的键
        assert_eq!(i10n::t("unknown"), "unknown");
    }
}

// ============================================
// 通义添加说明:
// 
// 本文件展示了如何使用 i10n 国际化模块的多种方式:
// 1. 基础翻译函数调用
// 2. 日志宏的使用
// 3. 自定义翻译键
// 4. 错误处理场景
// 5. 组合使用多个翻译
//
// 实际使用时,建议在程序启动时调用一次 init_i18n()
// ============================================