#[macro_use] extern crate uebersetzt; use uebersetzt::{Language, lang_from_json, load_lang, TranslationResult, Config, Uebersetzt, strfmt}; use std::collections::HashMap; use std::path::Path; #[test] fn language_find() { let mut lang = Language::new("German".to_string(), "de".to_string()); lang.insert("add".to_string(), "hinzufügen".to_string()); lang.insert("remove".to_string(), "entfernen".to_string()); assert_eq!("hinzufügen", lang.translate("add").unwrap()); assert_eq!(TranslationResult::KeyNotFound, lang.translate("wrong"), "Could find non existing word"); } #[test] fn parse_lang_json() { let json = r#" { "name": "German", "id": "de", "data": { "add": "hinzufügen", "remove": "entfernen" } } "#; let lang = lang_from_json(json).unwrap(); assert_eq!("German", lang.name()); assert_eq!("de", lang.id()); assert_eq!("hinzufügen", lang.translate("add").unwrap()); assert_eq!("entfernen", lang.translate("remove").unwrap()); } #[test] fn load_language() { let root = Path::new("./test-res"); let id = "de"; let lang = load_lang(root, id).unwrap(); assert_eq!("German", lang.name()); assert_eq!("de", lang.id()); assert_eq!("hinzufügen", lang.translate("add").unwrap()); assert_eq!("entfernen", lang.translate("remove").unwrap()); } #[test] fn new_uebersetzt() { let config = Config::new("./test-res".to_string(), vec!["de".to_string()]); let uebersetzt = Uebersetzt::new(config); assert_eq!("hinzufügen", uebersetzt.get("de", "add").unwrap()); assert_eq!(TranslationResult::LanguageNotFound, uebersetzt.get("en", "add")); } #[test] fn i18n_macro() { let config = Config::new("./test-res".to_string(), vec!["de".to_string()]); let u = Uebersetzt::new(config); let s: String = i18n!(u, "de", "format_test", ("val", "testval")); assert_eq!("value = testval bleh", s); }