Skip to content

Commit

Permalink
fix(l10n): improve en-US fallback in sidebars
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Dec 19, 2024
1 parent d253cf3 commit 7f91855
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 15 deletions.
10 changes: 2 additions & 8 deletions crates/rari-doc/src/pages/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,8 @@ impl Page {
match page_category {
PageCategory::SPA => SPA::from_slug(slug, locale)
.ok_or(DocError::PageNotFound(url.to_string(), PageCategory::SPA)),
PageCategory::Doc => {
let doc = Doc::page_from_slug_path(&folder_path, locale);
if doc.is_err() && locale != Default::default() && fallback {
Doc::page_from_slug_path(&folder_path, Default::default())
} else {
doc
}
}
PageCategory::Doc => Doc::page_from_slug_path(&folder_path, locale, fallback)
.map_err(|_| DocError::PageNotFound(url.to_string(), PageCategory::Doc)),
PageCategory::BlogPost => BlogPost::page_from_url(url).ok_or(DocError::PageNotFound(
url.to_string(),
PageCategory::BlogPost,
Expand Down
23 changes: 18 additions & 5 deletions crates/rari-doc/src/pages/types/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::sync::Arc;
use pretty_yaml::config::{FormatOptions, LanguageOptions};
use rari_md::m2h;
use rari_types::fm_types::{FeatureStatus, PageType};
use rari_types::locale::Locale;
use rari_types::locale::{default_locale, Locale};
use rari_types::RariEnv;
use rari_utils::io::read_to_string;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -116,11 +116,24 @@ pub struct Doc {
pub type ADoc = Arc<Doc>;

impl Doc {
pub fn page_from_slug(slug: &str, locale: Locale) -> Result<Page, DocError> {
Doc::page_from_slug_path(&url_to_folder_path(slug), locale)
pub fn page_from_slug(slug: &str, locale: Locale, fallback: bool) -> Result<Page, DocError> {
Doc::page_from_slug_path(&url_to_folder_path(slug), locale, fallback)
}

pub fn page_from_slug_path(path: &Path, locale: Locale) -> Result<Page, DocError> {
pub fn page_from_slug_path(
path: &Path,
locale: Locale,
fallback: bool,
) -> Result<Page, DocError> {
let doc = Self::page_from_slug_path_internal(path, locale);
if doc.is_err() && locale != default_locale() && fallback {
Self::page_from_slug_path_internal(path, Default::default())
} else {
doc
}
}

fn page_from_slug_path_internal(path: &Path, locale: Locale) -> Result<Page, DocError> {
let mut file = root_for_locale(locale)?.to_path_buf();
file.push(locale.as_folder_str());
file.push(path);
Expand Down Expand Up @@ -164,7 +177,7 @@ impl PageReader for Doc {
let mut doc = read_doc(&path)?;

if doc.meta.locale != Default::default() && !doc.is_conflicting() && !doc.is_orphaned() {
match Doc::page_from_slug(&doc.meta.slug, Default::default()) {
match Doc::page_from_slug(&doc.meta.slug, Default::default(), false) {
Ok(Page::Doc(super_doc)) => {
doc.copy_meta_from_super(&super_doc);
}
Expand Down
2 changes: 2 additions & 0 deletions crates/rari-doc/src/sidebars/apiref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub fn sidebar(slug: &str, group: Option<&str>, locale: Locale) -> Result<MetaSi
content: SidebarMetaEntryContent::Page(Doc::page_from_slug(
&format!("Web/API/{}", overview.replace(' ', "_")),
locale,
true,
)?),
..Default::default()
});
Expand All @@ -98,6 +99,7 @@ pub fn sidebar(slug: &str, group: Option<&str>, locale: Locale) -> Result<MetaSi
content: SidebarMetaEntryContent::Page(Doc::page_from_slug(
&format!("Web/API/{main_if}"),
locale,
true,
)?),
..Default::default()
});
Expand Down
1 change: 1 addition & 0 deletions crates/rari-doc/src/sidebars/default_api_sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub fn sidebar(group: &str, locale: Locale) -> Result<MetaSidebar, DocError> {
content: SidebarMetaEntryContent::Page(Doc::page_from_slug(
&format!("Web/API/{}", overview.replace(' ', "_")),
locale,
true,
)?),
..Default::default()
});
Expand Down
1 change: 1 addition & 0 deletions crates/rari-doc/src/sidebars/jsref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub fn sidebar(slug: &str, locale: Locale) -> Result<MetaSidebar, DocError> {
content: SidebarMetaEntryContent::Page(Doc::page_from_slug(
"Web/JavaScript/Reference/Global_Objects",
locale,
true,
)?),
..Default::default()
});
Expand Down
1 change: 1 addition & 0 deletions crates/rari-doc/src/templ/templs/api_list_specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub fn api_list_specs() -> Result<String, DocError> {
let page = Doc::page_from_slug(
&format!("Web/API/{}", overview.replace(' ', "_")),
env.locale,
true,
)?;
let out = out_by_letter.entry(first_letter).or_default();
write_li_with_badges(out, &page, env.locale, false, true)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/rari-doc/src/translations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub(crate) fn get_other_translations_for(slug: &str, locale: Locale) -> Vec<(Loc
Locale::for_generic_and_spas()
.iter()
.filter_map(|l| {
Doc::page_from_slug(slug, *l)
Doc::page_from_slug(slug, *l, false)
.ok()
.map(|d| (*l, d.title().to_string()))
})
Expand Down
2 changes: 1 addition & 1 deletion crates/rari-tools/src/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fn do_remove(
redirect: Option<&str>,
dry_run: bool,
) -> Result<Vec<String>, ToolError> {
let doc = Doc::page_from_slug(slug, locale)?;
let doc = Doc::page_from_slug(slug, locale, false)?;
let real_slug = doc.slug();

// If we get a redirect value passed in, it is either a slug or a complete url.
Expand Down

0 comments on commit 7f91855

Please sign in to comment.