Skip to content

Commit 10c4805

Browse files
committed
feat(translations): add other translations field
TODO: add proper native strings
1 parent 71adb36 commit 10c4805

File tree

6 files changed

+65
-3
lines changed

6 files changed

+65
-3
lines changed

Diff for: crates/rari-doc/src/docs/build.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use super::doc::{render_md_to_html, Doc};
1414
use super::dummy::Dummy;
1515
use super::json::{
1616
BuiltDocy, Compat, JsonBlogPost, JsonBlogPostDoc, JsonCurriculum, JsonDoADoc, JsonDoc, Prose,
17-
Section, Source, SpecificationSection, TocEntry,
17+
Section, Source, SpecificationSection, TocEntry, Translation,
1818
};
1919
use super::page::PageLike;
2020
use super::parents::parents;
@@ -30,6 +30,7 @@ use crate::html::sidebar::{
3030
};
3131
use crate::specs::extract_specifications;
3232
use crate::templ::render::{decode_ref, render, Rendered};
33+
use crate::translations::get_translations_for;
3334

3435
impl<'a> From<BuildSection<'a>> for Section {
3536
fn from(value: BuildSection) -> Self {
@@ -241,6 +242,14 @@ pub fn build_doc(doc: &Doc) -> Result<BuiltDocy, DocError> {
241242
);
242243

243244
let popularity = popularities().popularities.get(doc.url()).cloned();
245+
let other_translations = get_translations_for(doc.slug(), doc.locale())
246+
.into_iter()
247+
.map(|(locale, title)| Translation {
248+
native: locale.into(),
249+
locale,
250+
title,
251+
})
252+
.collect();
244253

245254
Ok(BuiltDocy::Doc(Box::new(JsonDoADoc {
246255
doc: JsonDoc {
@@ -269,6 +278,7 @@ pub fn build_doc(doc: &Doc) -> Result<BuiltDocy, DocError> {
269278
last_commit_url,
270279
},
271280
browser_compat: doc.meta.browser_compat.clone(),
281+
other_translations,
272282
..Default::default()
273283
},
274284
url: doc.meta.url.clone(),

Diff for: crates/rari-doc/src/docs/json.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub struct Parent {
3434
#[derive(Debug, Clone, Serialize, Default)]
3535
pub struct Translation {
3636
pub locale: Locale,
37+
pub title: String,
3738
pub native: Native,
3839
}
3940

Diff for: crates/rari-doc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ pub mod resolve;
1111
pub mod sidebars;
1212
pub mod specs;
1313
pub mod templ;
14+
pub mod translations;
1415
pub mod utils;
1516
pub mod walker;

Diff for: crates/rari-doc/src/translations.rs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use std::collections::{BTreeMap, HashMap};
2+
use std::sync::OnceLock;
3+
4+
use rari_types::locale::Locale;
5+
6+
use crate::cached_readers::STATIC_PAGE_FILES;
7+
use crate::docs::page::PageLike;
8+
9+
pub type TranslationsOf<'a> = BTreeMap<Locale, &'a str>;
10+
11+
pub type AllTranslationsOf<'a> = HashMap<&'a str, TranslationsOf<'a>>;
12+
13+
pub static TRANSLATIONS_BY_SLUG: OnceLock<AllTranslationsOf> = OnceLock::new();
14+
15+
pub fn init_translations_from_static_docs() {
16+
let mut all = HashMap::new();
17+
18+
if let Some(static_pages) = STATIC_PAGE_FILES.get() {
19+
for page in static_pages.values() {
20+
let entry: &mut TranslationsOf<'static> = all.entry(page.slug()).or_default();
21+
entry.insert(page.locale(), page.title());
22+
}
23+
};
24+
25+
TRANSLATIONS_BY_SLUG.set(all).unwrap();
26+
}
27+
28+
pub fn get_translations_for(slug: &str, locale: Locale) -> Vec<(Locale, String)> {
29+
TRANSLATIONS_BY_SLUG
30+
.get()
31+
.and_then(|by_slug| {
32+
by_slug.get(slug).map(|translations| {
33+
translations
34+
.iter()
35+
.filter_map(|(t_locale, title)| {
36+
if *t_locale != locale {
37+
Some((*t_locale, title.to_string()))
38+
} else {
39+
None
40+
}
41+
})
42+
.collect()
43+
})
44+
})
45+
.unwrap_or_default()
46+
}

Diff for: crates/rari-types/src/locale.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
55
use serde_variant::to_variant_name;
66
use thiserror::Error;
77

8-
#[derive(PartialEq, Debug, Clone, Copy, Deserialize, Serialize, Default)]
8+
#[derive(PartialEq, Debug, Clone, Copy, Deserialize, Serialize, Default, PartialOrd, Eq, Ord)]
99
pub enum Native {
1010
#[default]
1111
#[serde(rename = "English (US)")]
@@ -54,7 +54,9 @@ pub enum LocaleError {
5454
IOError(#[from] std::io::Error),
5555
}
5656

57-
#[derive(PartialEq, Eq, Debug, Clone, Copy, Deserialize, Serialize, Default, Hash)]
57+
#[derive(
58+
PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy, Deserialize, Serialize, Default, Hash,
59+
)]
5860
pub enum Locale {
5961
#[default]
6062
#[serde(rename = "en-US")]

Diff for: src/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rari_doc::build::{build_blog_pages, build_curriculum_pages, build_docs};
1111
use rari_doc::cached_readers::{CACHED_PAGE_FILES, STATIC_PAGE_FILES};
1212
use rari_doc::docs::doc::Doc;
1313
use rari_doc::docs::page::PageLike;
14+
use rari_doc::translations::init_translations_from_static_docs;
1415
use rari_doc::utils::TEMPL_RECORDER_SENDER;
1516
use rari_doc::walker::read_docs_parallel;
1617
use rari_tools::history::gather_history;
@@ -165,6 +166,7 @@ fn main() -> Result<(), anyhow::Error> {
165166
.collect(),
166167
)
167168
.unwrap();
169+
init_translations_from_static_docs()
168170
}
169171
println!("Took: {: >10.3?} for {}", start.elapsed(), docs.len());
170172
let mut urls = Vec::new();

0 commit comments

Comments
 (0)