From 2cd1d1332c310f776ba1ad811bb10436597f20d6 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Tue, 27 Feb 2024 17:45:54 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=90=20Change=20to=20LANGID=5FDEFAULT?= =?UTF-8?q?=20name=20and=20update=20docs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.rs | 4 +-- src/base/package/welcome.rs | 23 +++++++++++-- src/core/component/context.rs | 4 +-- src/locale.rs | 63 ++++++++++++++++------------------- 4 files changed, 54 insertions(+), 40 deletions(-) diff --git a/src/app.rs b/src/app.rs index 9c8b4e98..b85afd39 100644 --- a/src/app.rs +++ b/src/app.rs @@ -45,8 +45,8 @@ impl Application { // Starts logging and event tracing. LazyStatic::force(&trace::TRACING); - // Validates the global language identifier. - LazyStatic::force(&locale::LANGID); + // Validates the default language identifier. + LazyStatic::force(&locale::LANGID_DEFAULT); #[cfg(feature = "database")] // Connects to the database. diff --git a/src/base/package/welcome.rs b/src/base/package/welcome.rs index 2812cc6e..d995c74f 100644 --- a/src/base/package/welcome.rs +++ b/src/base/package/welcome.rs @@ -12,13 +12,32 @@ impl PackageTrait for Welcome { } fn configure_service(&self, scfg: &mut service::web::ServiceConfig) { - scfg.route("/", service::web::get().to(demo)); + scfg.route("/", service::web::get().to(home_page)) + .route("/{lang}", service::web::get().to(home_lang)); } } -async fn demo(request: service::HttpRequest) -> ResultPage { +async fn home_page(request: service::HttpRequest) -> ResultPage { + home(request, &LANGID_DEFAULT) +} + +async fn home_lang( + request: service::HttpRequest, + path: service::web::Path, +) -> ResultPage { + match langid_for(path.into_inner()) { + Ok(lang) => home(request, lang), + _ => Err(ErrorPage::NotFound(request)), + } +} + +fn home( + request: service::HttpRequest, + lang: &'static LanguageIdentifier, +) -> ResultPage { Page::new(request) .with_title(L10n::l("welcome_title")) + .with_context(ContextOp::LangId(lang)) .with_context(ContextOp::AddStyleSheet(StyleSheet::at( "/base/css/welcome.css", ))) diff --git a/src/core/component/context.rs b/src/core/component/context.rs index 8b935a74..ae23dbba 100644 --- a/src/core/component/context.rs +++ b/src/core/component/context.rs @@ -2,7 +2,7 @@ use crate::base::component::add_base_assets; use crate::core::theme::all::{theme_by_single_name, THEME}; use crate::core::theme::ThemeRef; use crate::html::{html, Assets, HeadScript, HeadStyles, JavaScript, Markup, StyleSheet}; -use crate::locale::{LanguageIdentifier, LANGID}; +use crate::locale::{LanguageIdentifier, LANGID_DEFAULT}; use crate::service::HttpRequest; use crate::{concat_string, util}; @@ -46,7 +46,7 @@ impl Context { pub(crate) fn new(request: HttpRequest) -> Self { Context { request, - langid : &LANGID, + langid : &LANGID_DEFAULT, theme : *THEME, stylesheet: Assets::::new(), // Stylesheets. headstyles: Assets::::new(), // Styles in head. diff --git a/src/locale.rs b/src/locale.rs index 25c65613..41d2bb6c 100644 --- a/src/locale.rs +++ b/src/locale.rs @@ -1,22 +1,21 @@ //! Localization (L10n). //! -//! PageTop usa el conjunto de especificaciones [Fluent](https://www.projectfluent.org/) para la -//! localización de aplicaciones. +//! PageTop uses the [Fluent](https://www.projectfluent.org/) set of specifications for application +//! localization. //! -//! # Sintaxis Fluent (FTL) +//! # Fluent Syntax (FTL) //! -//! El formato utilizado para describir los recursos de traducción utilizados por Fluent se llama -//! [FTL](https://www.projectfluent.org/fluent/guide/). FTL está diseñado para ser fácil de leer, -//! pero al mismo tiempo permite representar conceptos complejos del lenguaje natural para tratar -//! género, plurales, conjugaciones, y otros. +//! The format used to describe the translation resources used by Fluent is called +//! [FTL](https://www.projectfluent.org/fluent/guide/). FTL is designed to be easy to read while +//! simultaneously allowing the representation of complex natural language concepts to address +//! gender, plurals, conjugations, and others. //! +//! # Fluent Resources //! -//! # Recursos Fluent -//! -//! PageTop usa [fluent-templates](https://docs.rs/fluent-templates/) para integrar los recursos de -//! localización en el binario de la aplicación. El siguiente ejemplo agrupa archivos y subcarpetas -//! de *src/locale* que tienen un [Identificador de Idioma Unicode](https://docs.rs/unic-langid/) -//! válido y los asigna a su identificador correspondiente: +//! PageTop utilizes [fluent-templates](https://docs.rs/fluent-templates/) to integrate localization +//! resources into the application binary. The following example groups files and subfolders from +//! *src/locale* that have a valid [Unicode Language Identifier](https://docs.rs/unic-langid/) and +//! assigns them to their corresponding identifier: //! //! ```text //! src/locale/ @@ -35,7 +34,7 @@ //! └── main.ftl //! ``` //! -//! Ejemplo de un archivo *src/locale/en-US/main.ftl*: +//! Example of a file *src/locale/en-US/main.ftl*: //! //! ```text //! hello-world = Hello world! @@ -51,7 +50,7 @@ //! }. //! ``` //! -//! Ejemplo del archivo equivalente *src/locale/es-ES/main.ftl*: +//! Example of the equivalent file *src/locale/es-ES/main.ftl*: //! //! ```text //! hello-world = Hola mundo! @@ -67,11 +66,11 @@ //! }. //! ``` //! -//! # Cómo aplicar la localización en tu código +//! # How to apply localization in your code //! -//! Una vez hayas creado tu directorio de recursos FTL usa la macro -//! [`static_locales!`](crate::static_locales) para integrarlos en tu módulo o aplicación. -//! si tus recursos se encuentran en el directorio `"src/locale"` bastará con declarar: +//! Once you have created your FTL resource directory, use the +//! [`static_locales!`](crate::static_locales) macro to integrate them into your module or +//! application. If your resources are located in the `"src/locale"` directory, simply declare: //! //! ``` //! use pagetop::prelude::*; @@ -79,7 +78,7 @@ //! static_locales!(LOCALES_SAMPLE); //! ``` //! -//! Y si están en otro directorio, entonces puedes usar: +//! But if they are in another directory, then you can use: //! //! ``` //! use pagetop::prelude::*; @@ -114,10 +113,10 @@ static LANGUAGES: LazyStatic> = Lazy pub static LANGID_FALLBACK: LazyStatic = LazyStatic::new(|| langid!("en-US")); -/// Almacena el Identificador de Idioma Unicode -/// ([Unicode Language Identifier](https://unicode.org/reports/tr35/tr35.html#Unicode_language_identifier)) -/// global para la aplicación a partir de `SETTINGS.app.language`. -pub static LANGID: LazyStatic<&LanguageIdentifier> = LazyStatic::new(|| { +/// Sets the application's default +/// [Unicode Language Identifier](https://unicode.org/reports/tr35/tr35.html#Unicode_language_identifier) +/// through `SETTINGS.app.language`. +pub static LANGID_DEFAULT: LazyStatic<&LanguageIdentifier> = LazyStatic::new(|| { langid_for(config::SETTINGS.app.language.as_str()).unwrap_or(&LANGID_FALLBACK) }); @@ -130,10 +129,8 @@ pub fn langid_for(language: impl Into) -> Result<&'static LanguageIdenti Ok(&LANGID_FALLBACK) } else { Err(format!( - "{} Unicode Language Identifier \"{}\" is not accepted. {}", - "Failed to set language.", - config::SETTINGS.app.language, - "Using \"en-US\", check the settings file" + "Failed to get langid. Unicode Language Identifier \"{}\" is not accepted.", + language, )) } } @@ -141,7 +138,7 @@ pub fn langid_for(language: impl Into) -> Result<&'static LanguageIdenti } #[macro_export] -/// Define un conjunto de elementos de localización y textos locales de traducción. +/// Defines a set of localization elements and local translation texts. macro_rules! static_locales { ( $LOCALES:ident $(, $core_locales:literal)? ) => { $crate::locale::fluent_templates::static_loader! { @@ -149,8 +146,7 @@ macro_rules! static_locales { locales: "src/locale", $( core_locales: $core_locales, )? fallback_language: "en-US", - - // Elimina las marcas Unicode que delimitan los argumentos. + // Removes unicode isolating marks around arguments. customise: |bundle| bundle.set_use_isolating(false), }; } @@ -161,8 +157,7 @@ macro_rules! static_locales { locales: $dir_locales, $( core_locales: $core_locales, )? fallback_language: "en-US", - - // Elimina las marcas Unicode que delimitan los argumentos. + // Removes unicode isolating marks around arguments. customise: |bundle| bundle.set_use_isolating(false), }; } @@ -253,7 +248,7 @@ impl ToString for L10n { .lookup_with_args( match key.as_str() { LANGUAGE_SET_FAILURE => &LANGID_FALLBACK, - _ => &LANGID, + _ => &LANGID_DEFAULT, }, key, &self