Skip to content

Commit

Permalink
feat(i18n): ✨ added dummy translator to support not using i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-hen7 committed Sep 9, 2021
1 parent a123f0d commit 803b4f6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
4 changes: 2 additions & 2 deletions examples/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ define_app! {
],
locales: {
default: "en-US",
other: []
other: [],
no_i18n: true
}
// config_manager: perseus::FsConfigManager::new()
}
20 changes: 17 additions & 3 deletions packages/perseus/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,29 @@ 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
}
};
}

/// 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 {
{
Expand All @@ -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)?
Expand All @@ -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).
Expand Down
26 changes: 25 additions & 1 deletion packages/perseus/src/translations_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> {
Ok(String::new())
}
async fn get_translator_for_locale(&self, locale: String) -> Result<Translator> {
let translator = Translator::new(locale.clone(), String::new())
.map_err(|err| ErrorKind::SerializationFailed(locale.clone(), err.to_string()))?;

Ok(translator)
}
}

0 comments on commit 803b4f6

Please sign in to comment.