Translate when printing to console

This commit is contained in:
Alexander Medvedev 2025-01-23 16:57:23 +01:00
parent e7f1420da3
commit 57e54284c5
5 changed files with 24 additions and 2 deletions

1
assets/en_us.json Normal file

File diff suppressed because one or more lines are too long

View File

@ -6,6 +6,7 @@ edition.workspace = true
[dependencies] [dependencies]
pumpkin-nbt = { path = "../pumpkin-nbt" } pumpkin-nbt = { path = "../pumpkin-nbt" }
serde.workspace = true serde.workspace = true
serde_json.workspace = true
bytes.workspace = true bytes.workspace = true
uuid.workspace = true uuid.workspace = true

View File

@ -3,6 +3,7 @@ pub mod math;
pub mod permission; pub mod permission;
pub mod random; pub mod random;
pub mod text; pub mod text;
pub mod translation;
pub use gamemode::GameMode; pub use gamemode::GameMode;
pub use permission::PermissionLvl; pub use permission::PermissionLvl;

View File

@ -1,7 +1,7 @@
use core::str; use core::str;
use std::borrow::Cow; use std::borrow::Cow;
use crate::text::color::ARGBColor; use crate::{text::color::ARGBColor, translation::get_translation_en_us};
use click::ClickEvent; use click::ClickEvent;
use color::Color; use color::Color;
use colored::Colorize; use colored::Colorize;
@ -77,7 +77,12 @@ impl TextComponent {
pub fn to_pretty_console(self) -> String { pub fn to_pretty_console(self) -> String {
let mut text = match self.content { let mut text = match self.content {
TextContent::Text { text } => text.into_owned(), TextContent::Text { text } => text.into_owned(),
TextContent::Translate { translate, with: _ } => translate.into_owned(), TextContent::Translate { translate, with } => {
let translate = translate.into_owned();
get_translation_en_us(&translate, with)
.unwrap_or(translate.to_string())
.clone()
}
TextContent::EntityNames { TextContent::EntityNames {
selector, selector,
separator: _, separator: _,

View File

@ -0,0 +1,14 @@
use std::{borrow::Cow, collections::HashMap, sync::LazyLock};
const EN_US_JSON: &str = include_str!("../../assets/en_us.json");
pub static EN_US: LazyLock<HashMap<String, String>> =
LazyLock::new(|| serde_json::from_str(EN_US_JSON).expect("Could not parse en_us.json."));
pub fn get_translation_en_us(key: &str, with: Vec<Cow<'static, str>>) -> Option<String> {
let mut translation = EN_US.get(key)?.clone();
for replace in with {
translation = translation.replacen("%s", &replace, 1);
}
Some(translation)
}