From e1721d171f5b0316a833c8bd32d5f10c91b35476 Mon Sep 17 00:00:00 2001 From: Florian Dieminger Date: Fri, 20 Sep 2024 11:12:57 +0200 Subject: [PATCH] fix(locales): rename all to for generics and spas --- crates/rari-doc/src/cached_readers.rs | 4 ++-- crates/rari-doc/src/pages/types/spa.rs | 5 ++++- crates/rari-types/src/locale.rs | 27 +++++++++++++++++++----- crates/rari-types/src/settings.rs | 29 +------------------------- 4 files changed, 29 insertions(+), 36 deletions(-) diff --git a/crates/rari-doc/src/cached_readers.rs b/crates/rari-doc/src/cached_readers.rs index 0383e55f..a18a6d45 100644 --- a/crates/rari-doc/src/cached_readers.rs +++ b/crates/rari-doc/src/cached_readers.rs @@ -114,7 +114,7 @@ pub fn gather_generic_pages() -> Result, DocError> { } }) .flat_map(|generic| { - Locale::all() + Locale::for_generic_and_spas() .iter() .map(|locale| Page::GenericPage(Arc::new(generic.as_locale(*locale)))) .collect::>() @@ -191,7 +191,7 @@ pub fn gather_contributre_spotlight() -> Result, DocError> } }) .flat_map(|cs| { - Locale::all() + Locale::for_generic_and_spas() .iter() .map(|locale| Page::ContributorSpotlight(Arc::new(cs.as_locale(*locale)))) .collect::>() diff --git a/crates/rari-doc/src/pages/types/spa.rs b/crates/rari-doc/src/pages/types/spa.rs index 045299e2..7ed7c5c8 100644 --- a/crates/rari-doc/src/pages/types/spa.rs +++ b/crates/rari-doc/src/pages/types/spa.rs @@ -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() diff --git a/crates/rari-types/src/locale.rs b/crates/rari-types/src/locale.rs index 9c554f41..431db381 100644 --- a/crates/rari-types/src/locale.rs +++ b/crates/rari-types/src/locale.rs @@ -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; @@ -87,6 +88,25 @@ impl Display for Locale { } } +static LOCALES_FOR_GENERICS_AND_SPAS: LazyLock> = 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::>() +}); + impl Locale { pub const fn as_url_str(&self) -> &str { match *self { @@ -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 } } diff --git a/crates/rari-types/src/settings.rs b/crates/rari-types/src/settings.rs index 2c013277..20863def 100644 --- a/crates/rari-types/src/settings.rs +++ b/crates/rari-types/src/settings.rs @@ -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>, + pub additional_locales_for_generics_and_spas: Vec, } 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"); @@ -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 }