From 803b4f6cce0ba55eb050e454d6359e8cf8a962c3 Mon Sep 17 00:00:00 2001 From: arctic_hen7 Date: Thu, 9 Sep 2021 18:49:54 +1000 Subject: [PATCH] =?UTF-8?q?feat(i18n):=20=E2=9C=A8=20added=20dummy=20trans?= =?UTF-8?q?lator=20to=20support=20not=20using=20i18n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/cli/src/lib.rs | 4 +-- packages/perseus/src/macros.rs | 20 ++++++++++++--- packages/perseus/src/translations_manager.rs | 26 +++++++++++++++++++- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/examples/cli/src/lib.rs b/examples/cli/src/lib.rs index ad4023d324..3effba32ba 100644 --- a/examples/cli/src/lib.rs +++ b/examples/cli/src/lib.rs @@ -34,7 +34,7 @@ define_app! { ], locales: { default: "en-US", - other: [] + other: [], + no_i18n: true } - // config_manager: perseus::FsConfigManager::new() } diff --git a/packages/perseus/src/macros.rs b/packages/perseus/src/macros.rs index f667761cc2..d5d51613f2 100644 --- a/packages/perseus/src/macros.rs +++ b/packages/perseus/src/macros.rs @@ -36,8 +36,19 @@ macro_rules! define_get_translations_manager { .await } }; + ($locales:expr, $no_i18n:literal) => { + pub async fn get_translations_manager() -> impl $crate::TranslationsManager { + $crate::translations_manager::DummyTranslationsManager::new() + } + }; ($locales:expr, $translations_manager:expr) => { - pub fn get_translations_manager() -> impl $crate::TranslationsManager { + pub async fn get_translations_manager() -> impl $crate::TranslationsManager { + $translations_manager + } + }; + // If the user doesn't want i18n but also sets their own transations manager, the latter takes priority + ($locales:expr, $no_i18n:literal, $translations_manager:expr) => { + pub async fn get_translations_manager() -> impl $crate::TranslationsManager { $translations_manager } }; @@ -45,7 +56,9 @@ macro_rules! define_get_translations_manager { /// Defines the components to create an entrypoint for the app. The actual entrypoint is created in the `.perseus/` crate (where we can /// get all the dependencies without driving the user's `Cargo.toml` nuts). This also defines the template map. This is intended to make -/// compatibility with the Perseus CLI significantly easier. +/// compatibility with the Perseus CLI significantly easier. Perseus makes i18n opt-out, so if you don't intend to use it, set `no_i18n` +/// to `true` in `locales`. Note that you must still specify a default locale for verbosity and correctness. If you specify `no_i18n` and +/// a custom translations manager, the latter will override. #[macro_export] macro_rules! define_app { { @@ -71,6 +84,7 @@ macro_rules! define_app { default: $default_locale:literal, // The user doesn't have to define any other locales other: [$($other_locale:literal),*] + $(,no_i18n: $no_i18n:literal)? } $(,config_manager: $config_manager:expr)? $(,translations_manager: $translations_manager:expr)? @@ -87,7 +101,7 @@ macro_rules! define_app { /// Gets the translations manager to use. This allows the user to conveniently test production managers in development. If /// nothing is given, the filesystem will be used. - $crate::define_get_translations_manager!(get_locales() $(, $translations_manager)?); + $crate::define_get_translations_manager!(get_locales() $(, $no_i18n)? $(, $translations_manager)?); /// Defines the locales the app should build for, specifying defaults and common locales (which will be built at build-time /// rather than on-demand). diff --git a/packages/perseus/src/translations_manager.rs b/packages/perseus/src/translations_manager.rs index 7d81d94b20..4e5fd6edbf 100644 --- a/packages/perseus/src/translations_manager.rs +++ b/packages/perseus/src/translations_manager.rs @@ -3,7 +3,8 @@ // This has its own error management logic because the user may implement it separately use crate::Translator; -use error_chain::{bail, error_chain}; +pub use error_chain::bail; +use error_chain::error_chain; use futures::future::join_all; use std::collections::HashMap; use std::fs; @@ -136,3 +137,26 @@ impl TranslationsManager for FsTranslationsManager { Ok(translator) } } + +/// A dummy translations manager for use if you don't want i18n. This avoids errors of not being able to find translations. If you set +/// `no_i18n: true` in the `locales` section of `define_app!`, this will be used by default. If you intend to use i18n, do not use this! +#[derive(Clone, Default)] +pub struct DummyTranslationsManager; +impl DummyTranslationsManager { + /// Creates a new dummy translations manager. + pub fn new() -> Self { + Self::default() + } +} +#[async_trait::async_trait] +impl TranslationsManager for DummyTranslationsManager { + async fn get_translations_str_for_locale(&self, _locale: String) -> Result { + Ok(String::new()) + } + async fn get_translator_for_locale(&self, locale: String) -> Result { + let translator = Translator::new(locale.clone(), String::new()) + .map_err(|err| ErrorKind::SerializationFailed(locale.clone(), err.to_string()))?; + + Ok(translator) + } +}