Merge pull request #1806 from Excurs1ons/fix/localized-ui-text

🤖 Agent: thank you! Merged after the contribution gate (checked against vanilla 26.3; one integration branch of main + its batch: clippy -D warnings, tests, the affected parity harnesses (111/112, all pass), in game on the server thread and in-frame, lan_e2e in both host modes) and this head's verify.
This commit is contained in:
Ludovic Marie
2026-10-25 21:51:23 +01:00
committed by GitHub
19 changed files with 217 additions and 74 deletions
+90 -2
View File
@@ -71,10 +71,52 @@ impl Names {
.cloned()
.unwrap_or_else(|| path.replace('_', " "))
}
/// An item's display name in the selected language: `item.*` then
/// `block.*`, then the pinned English map (`Names::item`).
pub fn display_item(&self, id: &str) -> String {
let (namespace, path) = id.split_once(':').unwrap_or(("minecraft", id));
if let Some(lang) = crate::lang::current() {
for prefix in ["item", "block"] {
if let Some(name) = lang.get(&format!("{prefix}.{namespace}.{path}")) {
return name.to_owned();
}
}
}
self.item(id)
}
/// A translation key's text in the selected language, else the pinned
/// English map (`HoverName` and tooltip keys such as potion names).
pub fn display_get(&self, key: &str) -> Option<String> {
crate::lang::current()
.and_then(|lang| lang.get(key).map(str::to_owned))
.or_else(|| self.get(key).map(str::to_owned))
}
pub fn get(&self, key: &str) -> Option<&str> {
self.map.get(key).map(String::as_str)
}
}
/// `CreativeModeTabs`' key for an exported tab id (`itemGroup.*`); the
/// export's `title` is the English fallback.
pub fn item_group_key(id: &str) -> Option<&'static str> {
Some(match id.trim_start_matches("minecraft:") {
"building_blocks" => "itemGroup.buildingBlocks",
"colored_blocks" => "itemGroup.coloredBlocks",
"natural_blocks" => "itemGroup.natural",
"functional_blocks" => "itemGroup.functional",
"redstone_blocks" => "itemGroup.redstone",
"hotbar" => "itemGroup.hotbar",
"search" => "itemGroup.search",
"tools_and_utilities" => "itemGroup.tools",
"combat" => "itemGroup.combat",
"food_and_drinks" => "itemGroup.foodAndDrink",
"ingredients" => "itemGroup.ingredients",
"spawn_eggs" => "itemGroup.spawnEggs",
"op_blocks" => "itemGroup.op",
"inventory" => "itemGroup.inventory",
_ => return None,
})
}
impl Creative {
pub const WIDTH: f32 = 195.0;
pub const HEIGHT: f32 = 136.0;
@@ -303,7 +345,17 @@ impl Creative {
.collect();
}
// FullTextSearchTree: tooltip lines, and the item id after a colon.
// The export's lines are English (they come from the vanilla jars);
// the hover name in the selected language is matched too, so a
// localized search finds the item. It is the whole name (a tipped
// arrow's potion, the ominous banner), as the first line is.
let text = java_lower(term);
let localized = |i: usize| {
java_lower(&crate::item_tooltip::name_line(
&self.search_items[i],
&|k| names.display_get(k),
))
};
all.filter(|&i| {
let e = &self.search_index[i];
let fallback;
@@ -314,11 +366,15 @@ impl Creative {
&e.lines
};
match text.split_once(':') {
None => any(lines.iter().map(String::as_str), &text),
None => {
any(lines.iter().map(String::as_str), &text) || localized(i).contains(&text)
}
Some((ns, path)) => {
let (ns, path) = (java_trim(ns), java_trim(path));
e.namespace.contains(ns)
&& (e.path.contains(path) || any(lines.iter().map(String::as_str), path))
&& (e.path.contains(path)
|| any(lines.iter().map(String::as_str), path)
|| localized(i).contains(path))
}
}
})
@@ -759,6 +815,24 @@ mod tests {
assert_eq!(q("#craft"), Vec::<usize>::new());
}
#[test]
fn search_matches_the_display_name_in_the_selected_language() {
let c = creative(0);
let names = Names {
map: HashMap::from([
("item.minecraft.anvil".to_owned(), "铁砧".to_owned()),
("item.minecraft.wooden_hoe".to_owned(), "木锄".to_owned()),
]),
};
let q = |s: &str| c.search_results(s, &names);
assert_eq!(q("铁砧"), [0]);
assert_eq!(q("木锄"), [2]);
// The English tooltip lines the export indexed still match.
assert_eq!(q("anvil"), [0]);
// A colon's path searches the display name too.
assert_eq!(q("minecraft:铁砧"), [0]);
}
#[test]
fn scrollbar_drag_follows_the_mouse_while_held() {
let mut c = creative(90);
@@ -794,6 +868,20 @@ mod tests {
assert!(!c.scrolling);
}
#[test]
fn creative_tab_titles_have_vanilla_keys() {
assert_eq!(
item_group_key("minecraft:building_blocks"),
Some("itemGroup.buildingBlocks")
);
assert_eq!(
item_group_key("minecraft:food_and_drinks"),
Some("itemGroup.foodAndDrink")
);
assert_eq!(item_group_key("minecraft:hotbar"), Some("itemGroup.hotbar"));
assert_eq!(item_group_key("minecraft:nonsense"), None);
}
#[test]
fn wheel_moves_one_row_per_notch() {
let mut c = creative(90);
+38 -19
View File
@@ -5334,22 +5334,32 @@ impl Game<'_> {
Some(BlockEntity::Chest(_)) if self.draw_machine_screen(batch, ox, oy) => {}
Some(BlockEntity::Chest(_)) => self.draw_chest_screen(batch, ox, oy),
Some(BlockEntity::Furnace(f)) => {
let (tex, title) = match f.kind {
let (tex, key, fallback) = match f.kind {
horizon_client::containers::FurnaceKind::Furnace => {
("gui/container/furnace", "Furnace")
}
horizon_client::containers::FurnaceKind::BlastFurnace => {
("gui/container/blast_furnace", "Blast Furnace")
("gui/container/furnace", "container.furnace", "Furnace")
}
horizon_client::containers::FurnaceKind::BlastFurnace => (
"gui/container/blast_furnace",
"container.blast_furnace",
"Blast Furnace",
),
horizon_client::containers::FurnaceKind::Smoker => {
("gui/container/smoker", "Smoker")
("gui/container/smoker", "container.smoker", "Smoker")
}
};
part(batch, tex, 0.0, 0.0, 176.0, 166.0, ox, oy);
let title = &self.container_title(title.to_owned());
let tw = HudBatch::text_width(atlas, title);
batch.text(atlas, title, ox + (176.0 - tw) / 2.0, oy + 6.0, grey, false);
batch.text(atlas, "Inventory", ox + 8.0, oy + 72.0, grey, false);
let title = self.container_title(self.translate(key, fallback));
let tw = HudBatch::text_width(atlas, &title);
batch.text(
atlas,
&title,
ox + (176.0 - tw) / 2.0,
oy + 6.0,
grey,
false,
);
let inventory = self.translate("container.inventory", "Inventory");
batch.text(atlas, &inventory, ox + 8.0, oy + 72.0, grey, false);
if f.lit_remaining > 0 {
// AbstractFurnaceScreen: flame height ceil(lit * 13) + 1.
let total = if f.lit_total == 0 { 200 } else { f.lit_total };
@@ -5403,14 +5413,12 @@ impl Game<'_> {
"gui/container/inventory"
};
part(batch, texture, 0.0, 0.0, 176.0, 166.0, ox, oy);
let (title, tx) = if self.craft_size == 3 {
("Crafting", 29.0)
} else {
("Crafting", 97.0)
};
batch.text(atlas, title, ox + tx, oy + 6.0, grey, false);
let title = self.translate("container.crafting", "Crafting");
let tx = if self.craft_size == 3 { 29.0 } else { 97.0 };
batch.text(atlas, &title, ox + tx, oy + 6.0, grey, false);
if self.craft_size == 3 {
batch.text(atlas, "Inventory", ox + 8.0, oy + 72.0, grey, false);
let inventory = self.translate("container.inventory", "Inventory");
batch.text(atlas, &inventory, ox + 8.0, oy + 72.0, grey, false);
}
}
}
@@ -9446,6 +9454,11 @@ impl Game<'_> {
fn creative_origin(batch: &HudBatch) -> (f32, f32) {
batch.centered_origin(Creative::WIDTH, Creative::HEIGHT)
}
/// A creative tab's title: `itemGroup.*`, else the export's English title.
fn creative_tab_title(&self, tab: &horizon_client::creative::Tab) -> String {
horizon_client::creative::item_group_key(&tab.id)
.map_or_else(|| tab.title.clone(), |key| self.translate(key, &tab.title))
}
/// Vanilla tooltip: dark box with a purple gradient border.
fn tooltip(&self, batch: &mut HudBatch, text: &str, x: f32, y: f32) {
let atlas = &self.gui.atlas;
@@ -9515,9 +9528,10 @@ impl Game<'_> {
}
let atlas = &self.gui.atlas;
if !tab.inventory {
let title = self.creative_tab_title(&tab);
batch.text(
atlas,
&tab.title,
&title,
ox + 8.0,
oy + 6.0,
rgba(64, 64, 64, 255),
@@ -9628,7 +9642,12 @@ impl Game<'_> {
&& self.cursor.1 >= oy + ty
&& self.cursor.1 < oy + ty + 32.0
{
self.tooltip(batch, &t.title, self.cursor.0, self.cursor.1);
self.tooltip(
batch,
&self.creative_tab_title(t),
self.cursor.0,
self.cursor.1,
);
}
}
self.draw_carried(batch);
+1 -1
View File
@@ -444,7 +444,7 @@ impl Game<'_> {
.find(|i| i.id == id)
.map(|i| i.stack.item.to_string())
.unwrap_or_default();
Text::literal(self.pack.names.item(&item))
Text::literal(self.pack.names.display_item(&item))
}
Target::Mob(id)
if self
+31 -19
View File
@@ -889,13 +889,31 @@ impl Game<'_> {
let key_is_cart = self
.container
.is_some_and(|k| k.1 == super::minecarts::CART_KEY_Y);
let (texture, height, title) = match screen {
Screen::Hopper if key_is_cart => {
("gui/container/hopper", 133.0, "Minecart with Hopper")
}
Screen::Hopper => ("gui/container/hopper", 133.0, "Item Hopper"),
Screen::Dispenser => ("gui/container/dispenser", 166.0, "Dispenser"),
Screen::Dropper => ("gui/container/dispenser", 166.0, "Dropper"),
let (texture, height, key, fallback) = match screen {
Screen::Hopper if key_is_cart => (
"gui/container/hopper",
133.0,
"entity.minecraft.hopper_minecart",
"Minecart with Hopper",
),
Screen::Hopper => (
"gui/container/hopper",
133.0,
"container.hopper",
"Item Hopper",
),
Screen::Dispenser => (
"gui/container/dispenser",
166.0,
"container.dispenser",
"Dispenser",
),
Screen::Dropper => (
"gui/container/dispenser",
166.0,
"container.dropper",
"Dropper",
),
};
if let Some(mut r) = atlas.sprites.get(texture).copied() {
let dv = (r.v1 - r.v0) / r.h as f32;
@@ -905,23 +923,17 @@ impl Game<'_> {
batch.rect_uv(ox, oy, 176.0, height, r, u32::MAX);
}
let grey = horizon_client::hud::rgba(64, 64, 64, 255);
let title = &self
let title = self
.remote_menu_title()
.unwrap_or_else(|| self.container_title(title.to_owned()));
.unwrap_or_else(|| self.container_title(self.translate(key, fallback)));
let title_x = if screen == Screen::Hopper {
8.0
} else {
(176.0 - horizon_client::hud::HudBatch::text_width(atlas, title)) / 2.0
(176.0 - horizon_client::hud::HudBatch::text_width(atlas, &title)) / 2.0
};
batch.text(atlas, title, ox + title_x, oy + 6.0, grey, false);
batch.text(
atlas,
"Inventory",
ox + 8.0,
oy + height - 94.0,
grey,
false,
);
batch.text(atlas, &title, ox + title_x, oy + 6.0, grey, false);
let inventory = self.translate("container.inventory", "Inventory");
batch.text(atlas, &inventory, ox + 8.0, oy + height - 94.0, grey, false);
true
}
}
@@ -256,7 +256,8 @@ impl Game<'_> {
grey,
false,
);
batch.text(atlas, "Inventory", ox + 8.0, oy + 72.0, grey, false);
let inventory = self.translate("container.inventory", "Inventory");
batch.text(atlas, &inventory, ox + 8.0, oy + 72.0, grey, false);
self.paint_screen(batch, ox, oy, rest, true);
}
/// `gui.togglable_slot` over an empty, enabled grid slot.
+2 -1
View File
@@ -470,7 +470,8 @@ impl Game<'_> {
None => String::new(),
};
batch.text(atlas, &title, ox + 8.0, oy + 6.0, grey, false);
batch.text(atlas, "Inventory", ox + 8.0, oy + 72.0, grey, false);
let inventory = self.translate("container.inventory", "Inventory");
batch.text(atlas, &inventory, ox + 8.0, oy + 72.0, grey, false);
if let Some(m) = self.mobs.iter().find(|m| m.id == open.mob) {
self.draw_mob_preview(
batch,
@@ -247,6 +247,7 @@ impl Game<'_> {
pub(super) fn load_language(&mut self, code: &str) {
let root = self.options.resources.clone();
lang::set(lang::Language::load(&[&root], code));
self.refresh_remote_language();
self.ui.chat.rescale(&self.gui.atlas);
}
/// Settings that Horizon applies beyond render distance, volume and GUI scale.
+1
View File
@@ -203,6 +203,7 @@ impl Game<'_> {
horizon_client::mobs::villager_hats::VillagerHats::load(root);
horizon_client::ui::load_metadata(root);
lang::set(lang::Language::load(&[&root.to_path_buf()], &lang::code()));
self.refresh_remote_language();
self.audio.reload(root);
self.clear_lod();
self.streaming.reload_resources(&mut self.world, context);
+1 -1
View File
@@ -755,7 +755,7 @@ impl Game<'_> {
image_width,
&self.potions.effects.list,
self.clock.rate(),
&|key| self.pack.names.get(key).map(str::to_owned),
&|key| self.pack.names.display_get(key),
&|text| self.gui.atlas.font.width(text).ceil() as i32,
);
if let Some(lines) = inventory_effects::paint(
@@ -502,7 +502,7 @@ impl Game<'_> {
horizon_client::item_tooltip::lines(
stack,
self.res.items().get(&stack.item),
&|k| self.pack.names.get(k).map(str::to_owned),
&|k| self.pack.names.display_get(k),
)
})
{
+9
View File
@@ -4906,6 +4906,15 @@ impl Game<'_> {
pub(super) fn remote_menu_title(&self) -> Option<String> {
self.remote.as_ref()?.menu_title.clone()
}
/// A language change reaches the server-sent components too: their titles
/// are resolved when the packet arrives, with the session's language.
pub(super) fn refresh_remote_language(&mut self) {
if let Some(lang) = horizon_client::lang::current()
&& let Some(r) = self.remote.as_mut()
{
r.lang = Arc::new(lang.map().clone());
}
}
/// A server's chest-like menu (`MenuType.GENERIC_9xN`): its rows and title.
pub(super) fn remote_chest_menu(&self) -> Option<(usize, String)> {
let r = self.remote.as_ref()?;
+6 -5
View File
@@ -248,13 +248,14 @@ impl Game<'_> {
let atlas = &self.gui.atlas;
let (w, h) = (batch.width, batch.height);
batch.fill(atlas, 0.0, 0.0, w, h, rgba(16, 16, 16, 160));
let title = if sign.hanging {
"Edit Hanging Sign Message"
let (key, fallback) = if sign.hanging {
("hanging_sign.edit", "Edit Hanging Sign Message")
} else {
"Edit Sign Message"
("sign.edit", "Edit Sign Message")
};
let tw = HudBatch::text_width(atlas, title);
batch.text(atlas, title, (w - tw) / 2.0, 40.0, u32::MAX, true);
let title = self.translate(key, fallback);
let tw = HudBatch::text_width(atlas, &title);
batch.text(atlas, &title, (w - tw) / 2.0, 40.0, u32::MAX, true);
let name = self
.res
.facts()
+15 -18
View File
@@ -277,15 +277,10 @@ fn argb(c: i32) -> u32 {
}
impl Game<'_> {
fn data(&self) -> &stations::StationData {
self.res.stations()
}
/// `Component.translatable`: the selected language (which `lang` layers
/// over `en_us`), the caller's English text last.
pub(super) fn translate(&self, key: &str, fallback: &str) -> String {
self.data()
.enchantments
.translate(key)
.unwrap_or(fallback)
.to_owned()
horizon_client::lang::get_or(key, fallback)
}
fn brewing_open(&self) -> bool {
matches!(self.container_kind(), Some(BlockEntity::Brewing(_)))
@@ -421,7 +416,8 @@ impl Game<'_> {
pub(super) fn anvil_item_name(&self, s: &Station) -> String {
match &s.inputs[0] {
Some(input)
if input.custom_name().is_none() && s.name == self.pack.names.item(&input.item) =>
if input.custom_name().is_none()
&& s.name == self.pack.names.display_item(&input.item) =>
{
String::new()
}
@@ -462,7 +458,7 @@ impl Game<'_> {
let name = self.anvil_item_name(&s);
let hover = s.inputs[0]
.as_ref()
.map(|i| stations::hover_name(&|id| self.pack.names.item(id), i))
.map(|i| stations::hover_name(&|id| self.pack.names.display_item(id), i))
.unwrap_or_default();
s.anvil = stations::anvil_result(
&data.enchantments,
@@ -895,7 +891,7 @@ impl Game<'_> {
s.anvil_last = s.inputs[0].clone();
s.name = s.inputs[0]
.as_ref()
.map(|i| stations::hover_name(&|id| self.pack.names.item(id), i))
.map(|i| stations::hover_name(&|id| self.pack.names.display_item(id), i))
.unwrap_or_default();
}
self.station_changed();
@@ -1648,28 +1644,29 @@ impl Game<'_> {
let name = if let Some(n) = stack.custom_name() {
n.to_owned()
} else if stack.data().is_some_and(|d| d.ominous_banner) {
e.translate("block.minecraft.ominous_banner")
.map_or_else(|| "Ominous Banner".to_owned(), str::to_owned)
self.translate("block.minecraft.ominous_banner", "Ominous Banner")
} else if stack.data().and_then(|d| d.potion.as_ref()).is_some()
|| stack.item.ends_with("potion")
|| &*stack.item == "minecraft:tipped_arrow"
{
let key = brewing::potion_name_key(&stack.item, stack.potion());
e.translate(&key)
.map_or_else(|| self.pack.names.item(&stack.item), str::to_owned)
self.pack
.names
.display_get(&key)
.unwrap_or_else(|| self.pack.names.display_item(&stack.item))
} else {
self.pack.names.item(&stack.item)
self.pack.names.display_item(&stack.item)
};
let display = horizon_client::item_tooltip::TooltipDisplay::for_stack(stack);
let mut lines = Vec::new();
if display.name_visible(self.screen == Screen::Creative) {
lines.push((name, color));
}
let translate = |k: &str| e.translate(k).map(str::to_owned);
let translate = |k: &str| self.pack.names.display_get(k);
// ItemContainerContents.addToTooltip.
let hover_name = |s: &ItemStack| {
s.custom_name()
.map_or_else(|| self.pack.names.item(&s.item), str::to_owned)
.map_or_else(|| self.pack.names.display_item(&s.item), str::to_owned)
};
if display.shows("minecraft:container") {
for line in
+4 -2
View File
@@ -273,7 +273,8 @@ impl Game<'_> {
);
let title = self.translate("container.shulkerBox", "Shulker Box");
batch.text(atlas, &title, ox + 8.0, oy + 6.0, grey, false);
batch.text(atlas, "Inventory", ox + 8.0, oy + 73.0, grey, false);
let inventory = self.translate("container.inventory", "Inventory");
batch.text(atlas, &inventory, ox + 8.0, oy + 73.0, grey, false);
return;
}
let rows = self.chest_rows().unwrap_or(3);
@@ -301,9 +302,10 @@ impl Game<'_> {
grey,
false,
);
let inventory = self.translate("container.inventory", "Inventory");
batch.text(
atlas,
"Inventory",
&inventory,
ox + 8.0,
oy + 114.0 + rows as f32 * 18.0 - 94.0,
grey,
+1 -1
View File
@@ -30,7 +30,7 @@ impl Game<'_> {
Some(HeldName {
item: stack.item.to_string(),
hover_name: Text::literal(item_tooltip::name_line(stack, &|key| {
self.pack.names.get(key).map(str::to_owned)
self.pack.names.display_get(key)
})),
rarity_color: match rarity {
"uncommon" => text::YELLOW,
+1 -1
View File
@@ -399,7 +399,7 @@ impl Game<'_> {
}
let views: Vec<Option<ItemStack>> = slots.iter().map(|&s| self.slot_view(s)).collect();
let clicks = {
let names = |id: &str| self.pack.names.item(id);
let names = |id: &str| self.pack.names.display_item(id);
let rank = |s: &ItemStack| {
let order = self.creative_screen.order_of(s).unwrap_or(usize::MAX);
(order, hover_name(&names, s))
+5
View File
@@ -138,6 +138,11 @@ impl Language {
pub fn get(&self, key: &str) -> Option<&str> {
self.map.get(key).map(String::as_str)
}
/// The resolved map, for `horizon_protocol::text::plain`: a server's
/// components carry keys, and the network layer resolves them with this.
pub fn map(&self) -> &HashMap<String, String> {
&self.map
}
pub fn len(&self) -> usize {
self.map.len()
}
+1 -1
View File
@@ -1001,7 +1001,7 @@ impl Menu {
fn anvil_result(&mut self, env: &Env) {
let hover = self.containers[MAIN][0]
.as_ref()
.map(|i| crate::stations::hover_name(&|id| env.names.item(id), i))
.map(|i| crate::stations::hover_name(&|id| env.names.display_item(id), i))
.unwrap_or_default();
let out = crate::stations::anvil_result(
env.enchantments,
+7 -1
View File
@@ -686,8 +686,14 @@ pub fn json_text(v: &serde_json::Value, lang: &Language) -> String {
}
}
/// en_us.json from the prepared resources.
/// The language a session resolves server-sent components with: the selected
/// one as `lang` loaded it (`en_us` under it, packs included), so a menu title
/// the server sends as a key follows the language screen. A process with no
/// language set (a dedicated server's log) reads en_us.json.
pub fn load_language(resources: &std::path::Path) -> Language {
if let Some(lang) = crate::lang::current() {
return lang.map().clone();
}
std::fs::read(resources.join("assets/minecraft/lang/en_us.json"))
.ok()
.and_then(|b| serde_json::from_slice(&b).ok())