Skip to content

Commit

Permalink
feat(i18n): ✨ removed concept of common locales
Browse files Browse the repository at this point in the history
All locales are now built together for simplicity.

BREAKING CHANGE: common locales no longer exist
  • Loading branch information
arctic-hen7 committed Sep 7, 2021
1 parent a4402c0 commit 95b476f
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"conventionalCommits.scopes": ["i18n"]
}
1 change: 0 additions & 1 deletion examples/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ define_app! {
],
locales: {
default: "en-US",
common: [],
other: []
}
// config_manager: perseus::FsConfigManager::new()
Expand Down
3 changes: 1 addition & 2 deletions examples/i18n/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ define_app! {
],
locales: {
default: "en-US",
common: ["en-US", "fr-FR"],
other: ["es-ES"]
other: ["fr-FR", "es-ES"]
}
}
8 changes: 4 additions & 4 deletions packages/perseus/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,18 @@ async fn build_templates_and_translator_for_locale(
Ok(())
}

/// Runs the build process of building many templates for the given locales data, building directly for the default and common locales.
/// Any other locales should be built on demand.
/// Runs the build process of building many templates for the given locales data, building directly for all supported locales. This is
/// fine because of how ridiculously fast builds are.
pub async fn build_app(
templates: Vec<Template<SsrNode>>,
locales: &Locales,
config_manager: &impl ConfigManager,
translations_manager: &impl TranslationsManager,
) -> Result<()> {
let locales_to_build = locales.get_default_and_common();
let locales = locales.get_all();
let mut futs = Vec::new();

for locale in locales_to_build {
for locale in locales {
futs.push(build_templates_and_translator_for_locale(
&templates,
locale.to_string(),
Expand Down
6 changes: 1 addition & 5 deletions packages/perseus/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ macro_rules! define_app {
// This deliberately enforces verbose i18n definition, and forces developers to consider i18n as integral
locales: {
default: $default_locale:literal,
// The user doesn't have to define any common/other locales
common: [$($common_locale:literal),*],
// The user doesn't have to define any other locales
other: [$($other_locale:literal),*]
}
$(,config_manager: $config_manager:expr)?
Expand All @@ -82,9 +81,6 @@ macro_rules! define_app {
pub fn get_locales() -> $crate::Locales {
$crate::Locales {
default: $default_locale.to_string(),
common: vec![
$($common_locale.to_string()),*
],
other: vec![
$($other_locale.to_string()),*
]
Expand Down
1 change: 1 addition & 0 deletions packages/perseus/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ async fn revalidate(
/// at request-time will **always** replace anything generated at build-time, incrementally, revalidated, etc.
// TODO possible further optimizations on this for futures?
pub async fn get_page(
// This must not contain the locale
path: &str,
locale: &str,
req: Request,
Expand Down
14 changes: 2 additions & 12 deletions packages/perseus/src/translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,17 @@ use std::collections::HashMap;
pub struct Locales {
/// The default locale, which will be used as a fallback if the user's locale can't be extracted. This will be built for at build-time.
pub default: String,
/// Common locales that should be built for at build-time.
pub common: Vec<String>,
/// All other supported locales, which won't be built unless requested at request-time.
/// All other supported locales, which will all be built at build time.
pub other: Vec<String>,
}
impl Locales {
/// Gets all the supported locales by combining the default, common, and other.
/// Gets all the supported locales by combining the default, and other.
pub fn get_all(&self) -> Vec<&String> {
let mut vec: Vec<&String> = vec![&self.default];
vec.extend(&self.common);
vec.extend(&self.other);

vec
}
/// Gets the locales that should be built at build time, the default and common.
pub fn get_default_and_common(&self) -> Vec<&String> {
let mut vec: Vec<&String> = vec![&self.default];
vec.extend(&self.common);

vec
}
/// Checks if the given locale is supported.
pub fn is_supported(&self, locale: &str) -> bool {
let locales = self.get_all();
Expand Down

0 comments on commit 95b476f

Please sign in to comment.