Skip to content

Commit

Permalink
fix(locales): rename all to for generics and spas
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Sep 20, 2024
1 parent 162721c commit e1721d1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 36 deletions.
4 changes: 2 additions & 2 deletions crates/rari-doc/src/cached_readers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub fn gather_generic_pages() -> Result<HashMap<String, Page>, DocError> {
}
})
.flat_map(|generic| {
Locale::all()
Locale::for_generic_and_spas()
.iter()
.map(|locale| Page::GenericPage(Arc::new(generic.as_locale(*locale))))
.collect::<Vec<_>>()
Expand Down Expand Up @@ -191,7 +191,7 @@ pub fn gather_contributre_spotlight() -> Result<HashMap<String, Page>, DocError>
}
})
.flat_map(|cs| {
Locale::all()
Locale::for_generic_and_spas()
.iter()
.map(|locale| Page::ContributorSpotlight(Arc::new(cs.as_locale(*locale))))
.collect::<Vec<_>>()
Expand Down
5 changes: 4 additions & 1 deletion crates/rari-doc/src/pages/types/spa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ impl SPA {
if build_spa.en_us_only || content_translated_root().is_none() {
vec![(slug, Locale::EnUs)]
} else {
Locale::all().iter().map(|locale| (slug, *locale)).collect()
Locale::for_generic_and_spas()
.iter()
.map(|locale| (slug, *locale))
.collect()
}
})
.collect()
Expand Down
27 changes: 22 additions & 5 deletions crates/rari-types/src/locale.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt::Display;
use std::str::FromStr;
use std::sync::LazyLock;

use serde::{Deserialize, Serialize};
use serde_variant::to_variant_name;
Expand Down Expand Up @@ -87,6 +88,25 @@ impl Display for Locale {
}
}

static LOCALES_FOR_GENERICS_AND_SPAS: LazyLock<Vec<Locale>> = LazyLock::new(|| {
let default_locales = [
Locale::EnUs,
Locale::Es,
Locale::Fr,
Locale::Ja,
Locale::Ko,
Locale::PtBr,
Locale::Ru,
Locale::ZhCn,
Locale::ZhTw,
];
default_locales
.iter()
.chain(settings().additional_locales_for_generics_and_spas.iter())
.map(ToOwned::to_owned)
.collect::<Vec<_>>()
});

impl Locale {
pub const fn as_url_str(&self) -> &str {
match *self {
Expand All @@ -111,11 +131,8 @@ impl Locale {
}
}

pub fn all() -> &'static [Self] {
settings()
.active_locales
.as_deref()
.unwrap_or([Locale::EnUs].as_slice())
pub fn for_generic_and_spas() -> &'static [Self] {
&LOCALES_FOR_GENERICS_AND_SPAS
}
}

Expand Down
29 changes: 1 addition & 28 deletions crates/rari-types/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ pub struct Settings {
pub live_samples_base_url: String,
pub legacy_live_samples_base_url: String,
pub interactive_examples_base_url: String,
pub active_locales: Option<Vec<Locale>>,
pub additional_locales_for_generics_and_spas: Vec<Locale>,
}

impl Settings {
#[cfg(not(target_arch = "wasm32"))]
fn validate(mut self) -> Self {
use std::iter::once;
use std::str::FromStr;

self.content_root =
std::fs::canonicalize(self.content_root).expect("CONTENT_ROOT is not a valid path");

Expand All @@ -38,30 +35,6 @@ impl Settings {
std::fs::canonicalize(translated_content_root)
.expect("CONTENT_TRANSLATED_ROOT is not a valid path")
});
if self.active_locales.is_none() {
if let Some(content_translated_root) = &self.content_translated_root {
self.active_locales = Some(
once(Locale::EnUs)
.chain(
std::fs::read_dir(content_translated_root)
.expect("Unable to read CONTENT_TRANSLATED_ROOT")
.filter_map(|f| f.ok())
.filter_map(|f| {
f.file_type().ok().and_then(|ft| {
if ft.is_dir() {
Locale::from_str(&f.file_name().to_string_lossy()).ok()
} else {
None
}
})
}),
)
.collect(),
)
} else {
self.active_locales = Some(vec![Locale::EnUs]);
}
}
self
}

Expand Down

0 comments on commit e1721d1

Please sign in to comment.