Skip to content

Commit 803b4f6

Browse files
committed
feat(i18n): ✨ added dummy translator to support not using i18n
1 parent a123f0d commit 803b4f6

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

examples/cli/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ define_app! {
3434
],
3535
locales: {
3636
default: "en-US",
37-
other: []
37+
other: [],
38+
no_i18n: true
3839
}
39-
// config_manager: perseus::FsConfigManager::new()
4040
}

packages/perseus/src/macros.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,29 @@ macro_rules! define_get_translations_manager {
3636
.await
3737
}
3838
};
39+
($locales:expr, $no_i18n:literal) => {
40+
pub async fn get_translations_manager() -> impl $crate::TranslationsManager {
41+
$crate::translations_manager::DummyTranslationsManager::new()
42+
}
43+
};
3944
($locales:expr, $translations_manager:expr) => {
40-
pub fn get_translations_manager() -> impl $crate::TranslationsManager {
45+
pub async fn get_translations_manager() -> impl $crate::TranslationsManager {
46+
$translations_manager
47+
}
48+
};
49+
// If the user doesn't want i18n but also sets their own transations manager, the latter takes priority
50+
($locales:expr, $no_i18n:literal, $translations_manager:expr) => {
51+
pub async fn get_translations_manager() -> impl $crate::TranslationsManager {
4152
$translations_manager
4253
}
4354
};
4455
}
4556

4657
/// Defines the components to create an entrypoint for the app. The actual entrypoint is created in the `.perseus/` crate (where we can
4758
/// get all the dependencies without driving the user's `Cargo.toml` nuts). This also defines the template map. This is intended to make
48-
/// compatibility with the Perseus CLI significantly easier.
59+
/// compatibility with the Perseus CLI significantly easier. Perseus makes i18n opt-out, so if you don't intend to use it, set `no_i18n`
60+
/// to `true` in `locales`. Note that you must still specify a default locale for verbosity and correctness. If you specify `no_i18n` and
61+
/// a custom translations manager, the latter will override.
4962
#[macro_export]
5063
macro_rules! define_app {
5164
{
@@ -71,6 +84,7 @@ macro_rules! define_app {
7184
default: $default_locale:literal,
7285
// The user doesn't have to define any other locales
7386
other: [$($other_locale:literal),*]
87+
$(,no_i18n: $no_i18n:literal)?
7488
}
7589
$(,config_manager: $config_manager:expr)?
7690
$(,translations_manager: $translations_manager:expr)?
@@ -87,7 +101,7 @@ macro_rules! define_app {
87101

88102
/// Gets the translations manager to use. This allows the user to conveniently test production managers in development. If
89103
/// nothing is given, the filesystem will be used.
90-
$crate::define_get_translations_manager!(get_locales() $(, $translations_manager)?);
104+
$crate::define_get_translations_manager!(get_locales() $(, $no_i18n)? $(, $translations_manager)?);
91105

92106
/// Defines the locales the app should build for, specifying defaults and common locales (which will be built at build-time
93107
/// rather than on-demand).

packages/perseus/src/translations_manager.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
// This has its own error management logic because the user may implement it separately
44

55
use crate::Translator;
6-
use error_chain::{bail, error_chain};
6+
pub use error_chain::bail;
7+
use error_chain::error_chain;
78
use futures::future::join_all;
89
use std::collections::HashMap;
910
use std::fs;
@@ -136,3 +137,26 @@ impl TranslationsManager for FsTranslationsManager {
136137
Ok(translator)
137138
}
138139
}
140+
141+
/// 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
142+
/// `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!
143+
#[derive(Clone, Default)]
144+
pub struct DummyTranslationsManager;
145+
impl DummyTranslationsManager {
146+
/// Creates a new dummy translations manager.
147+
pub fn new() -> Self {
148+
Self::default()
149+
}
150+
}
151+
#[async_trait::async_trait]
152+
impl TranslationsManager for DummyTranslationsManager {
153+
async fn get_translations_str_for_locale(&self, _locale: String) -> Result<String> {
154+
Ok(String::new())
155+
}
156+
async fn get_translator_for_locale(&self, locale: String) -> Result<Translator> {
157+
let translator = Translator::new(locale.clone(), String::new())
158+
.map_err(|err| ErrorKind::SerializationFailed(locale.clone(), err.to_string()))?;
159+
160+
Ok(translator)
161+
}
162+
}

0 commit comments

Comments
 (0)