-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(l10n): move rari-l10n to rari-doc
And clean up a bit.
- Loading branch information
Showing
24 changed files
with
114 additions
and
145 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use std::collections::HashMap; | ||
use std::fs; | ||
use std::sync::LazyLock; | ||
|
||
use rari_types::globals::content_root; | ||
use rari_types::locale::Locale; | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, PartialEq, Clone, Error)] | ||
pub enum L10nError { | ||
#[error("Invalid key for L10n json data: {0}")] | ||
InvalidKey(String), | ||
#[error("EnUS missing in L10n json data")] | ||
NoEnUs, | ||
} | ||
|
||
/// Look up a translation from mdn/content's `jsondata folder. | ||
/// `typ` refers to the `L1on-<typ>.json` file. | ||
pub fn l10n_json_data(typ: &str, key: &str, locale: Locale) -> Result<&'static str, L10nError> { | ||
if let Some(data) = JSON_L10N_FILES.get(typ).and_then(|file| file.get(key)) { | ||
get_for_locale(locale, data) | ||
.map(|s| s.as_str()) | ||
.ok_or(L10nError::NoEnUs) | ||
} else { | ||
Err(L10nError::InvalidKey(key.to_string())) | ||
} | ||
} | ||
|
||
fn get_for_locale<T>(locale: Locale, lookup: &HashMap<String, T>) -> Option<&T> { | ||
if let Some(value) = lookup.get(locale.as_url_str()) { | ||
Some(value) | ||
} else if locale != Locale::default() { | ||
lookup.get(Locale::default().as_url_str()) | ||
} else { | ||
None | ||
} | ||
} | ||
pub type JsonL10nFile = HashMap<String, HashMap<String, String>>; | ||
|
||
static JSON_L10N_FILES: LazyLock<HashMap<String, JsonL10nFile>> = LazyLock::new(|| { | ||
content_root() | ||
.join("jsondata") | ||
.read_dir() | ||
.expect("unable to read jsondata dir") | ||
.filter_map(|f| { | ||
if let Ok(f) = f { | ||
if f.path().is_file() | ||
&& f.path() | ||
.extension() | ||
.map_or(false, |ext| ext.eq_ignore_ascii_case("json")) | ||
&& f.path() | ||
.file_stem() | ||
.and_then(|s| s.to_str()) | ||
.map_or(false, |s| s.starts_with("L10n-")) | ||
{ | ||
return Some(f.path()); | ||
} | ||
} | ||
None | ||
}) | ||
.map(|f| { | ||
let typ = f | ||
.file_stem() | ||
.and_then(|s| s.to_str()) | ||
.unwrap_or_default() | ||
.strip_prefix("L10n-") | ||
.unwrap_or_default(); | ||
let json_str = fs::read_to_string(&f).expect("unable to read l10n json"); | ||
let l10n_json: JsonL10nFile = | ||
serde_json::from_str(&json_str).expect("unable to parse l10n json"); | ||
(typ.into(), l10n_json) | ||
}) | ||
.collect() | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
crates/rari-doc/src/templ/templs/embeds/embedinteractiveexample.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.