✨ Añade soporte para localización y traducción
- Incluye recursos Fluent básicos y pruebas asociadas. - Nueva variable de configuración global para definir el idioma predeterminado.
This commit is contained in:
parent
efc4839613
commit
208ad83bea
13 changed files with 780 additions and 3 deletions
58
tests/locale.rs
Normal file
58
tests/locale.rs
Normal file
|
@ -0,0 +1,58 @@
|
|||
use pagetop::prelude::*;
|
||||
|
||||
#[pagetop::test]
|
||||
async fn literal_text() {
|
||||
let _app = service::test::init_service(Application::new().test()).await;
|
||||
|
||||
let l10n = L10n::n("© 2025 PageTop");
|
||||
assert_eq!(l10n.get(), Some("© 2025 PageTop".to_string()));
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn translation_without_args() {
|
||||
let _app = service::test::init_service(Application::new().test()).await;
|
||||
|
||||
let l10n = L10n::l("test-hello-world");
|
||||
let translation = l10n.using(LangMatch::langid_or_fallback("es-ES"));
|
||||
assert_eq!(translation, Some("¡Hola mundo!".to_string()));
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn translation_with_args() {
|
||||
let _app = service::test::init_service(Application::new().test()).await;
|
||||
|
||||
let l10n = L10n::l("test-hello-user").with_arg("userName", "Manuel");
|
||||
let translation = l10n.using(LangMatch::langid_or_fallback("es-ES"));
|
||||
assert_eq!(translation, Some("¡Hola, Manuel!".to_string()));
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn translation_with_plural_and_select() {
|
||||
let _app = service::test::init_service(Application::new().test()).await;
|
||||
|
||||
let l10n = L10n::l("test-shared-photos").with_args(vec![
|
||||
("userName", "Roberto"),
|
||||
("photoCount", "3"),
|
||||
("userGender", "male"),
|
||||
]);
|
||||
let translation = l10n.using(LangMatch::langid_or_fallback("es-ES")).unwrap();
|
||||
assert!(translation.contains("añadido 3 nuevas fotos de él"));
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn check_fallback_language() {
|
||||
let _app = service::test::init_service(Application::new().test()).await;
|
||||
|
||||
let l10n = L10n::l("test-hello-world");
|
||||
let translation = l10n.using(LangMatch::langid_or_fallback("xx-YY")); // Fallback a "en-US".
|
||||
assert_eq!(translation, Some("Hello world!".to_string()));
|
||||
}
|
||||
|
||||
#[pagetop::test]
|
||||
async fn check_unknown_key() {
|
||||
let _app = service::test::init_service(Application::new().test()).await;
|
||||
|
||||
let l10n = L10n::l("non-existent-key");
|
||||
let translation = l10n.using(LangMatch::langid_or_fallback("en-US"));
|
||||
assert_eq!(translation, None);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue