refactor(localization): switch block translations to item naming

Remove the `name_key` field from block definitions and use item-based
localization keys (`item.*.name`) for inventory display. Item data now
stores the localized name at load time.
This commit is contained in:
2026-08-05 15:42:53 +08:00
parent 277aab1b81
commit 322fb2fea0
19 changed files with 44 additions and 58 deletions

View File

@@ -8,5 +8,4 @@ is_passable = true
is_transitional = false is_transitional = false
is_transparent = true is_transparent = true
name = 'air' name = 'air'
name_key = 'block.air'
roughness = 1.0 roughness = 1.0

View File

@@ -8,5 +8,4 @@ is_passable = false
is_transitional = true is_transitional = true
is_transparent = false is_transparent = false
name = 'dirt' name = 'dirt'
name_key = 'block.dirt'
roughness = 1.0 roughness = 1.0

View File

@@ -8,5 +8,4 @@ is_passable = true
is_transitional = false is_transitional = false
is_transparent = true is_transparent = true
name = 'grass' name = 'grass'
name_key = 'block.grass'
roughness = 0.90000000000000002 roughness = 0.90000000000000002

View File

@@ -8,5 +8,4 @@ is_passable = false
is_transitional = true is_transitional = true
is_transparent = false is_transparent = false
name = 'grass_block' name = 'grass_block'
name_key = 'block.grass_block'
roughness = 0.90000000000000002 roughness = 0.90000000000000002

View File

@@ -8,5 +8,4 @@ is_passable = false
is_transitional = false is_transitional = false
is_transparent = true is_transparent = true
name = 'leaf' name = 'leaf'
name_key = 'block.leaf'
roughness = 0.69999999999999996 roughness = 0.69999999999999996

View File

@@ -8,5 +8,4 @@ is_passable = false
is_transitional = false is_transitional = false
is_transparent = false is_transparent = false
name = 'log' name = 'log'
name_key = 'block.log'
roughness = 0.69999999999999996 roughness = 0.69999999999999996

View File

@@ -8,5 +8,4 @@ is_passable = false
is_transitional = true is_transitional = true
is_transparent = false is_transparent = false
name = 'sand' name = 'sand'
name_key = 'block.sand'
roughness = 0.80000000000000004 roughness = 0.80000000000000004

View File

@@ -8,5 +8,4 @@ is_passable = false
is_transitional = true is_transitional = true
is_transparent = false is_transparent = false
name = 'snowy_grass_block' name = 'snowy_grass_block'
name_key = 'block.snowy_grass_block'
roughness = 0.90000000000000002 roughness = 0.90000000000000002

View File

@@ -8,5 +8,4 @@ is_passable = false
is_transitional = true is_transitional = true
is_transparent = false is_transparent = false
name = 'stone' name = 'stone'
name_key = 'block.stone'
roughness = 0.75 roughness = 0.75

View File

@@ -9,4 +9,3 @@ is_discard = false
is_blend = false is_blend = false
is_transitional = false is_transitional = false
roughness = 1.0 roughness = 1.0
name_key = "block.template"

View File

@@ -8,5 +8,4 @@ is_passable = true
is_transitional = false is_transitional = false
is_transparent = true is_transparent = true
name = 'water' name = 'water'
name_key = 'block.water'
roughness = 0.02 roughness = 0.02

View File

@@ -36,14 +36,15 @@
"joingame.server_ip": "Server Ip", "joingame.server_ip": "Server Ip",
"joingame.join_world": "Join World", "joingame.join_world": "Join World",
"error.disable_voice": "Server Disable Voice Chat", "error.disable_voice": "Server Disable Voice Chat",
"block.air": "air", "item.air.name": "air",
"block.dirt": "dirt", "item.dirt.name": "dirt",
"block.grass_block": "grass block", "item.grass_block.name": "grass block",
"block.grass": "grass", "item.grass.name": "grass",
"block.leaf": "leaf", "item.leaf.name": "leaf",
"block.log": "log", "item.log.name": "log",
"block.sand": "sand", "item.sand.name": "sand",
"block.snowy_grass_block": "snowy grass block", "item.snowy_grass_block.name": "snowy grass block",
"block.stone": "stone", "item.stone.name": "stone",
"block.water": "water" "item.water.name": "water",
"item.pig_spawn_egg.name": "pig spawn egg"
} }

View File

@@ -36,14 +36,15 @@
"joingame.server_ip": "服务器IP", "joingame.server_ip": "服务器IP",
"joingame.join_world": "加入世界", "joingame.join_world": "加入世界",
"error.disable_voice": "服务器已禁用语音聊天", "error.disable_voice": "服务器已禁用语音聊天",
"block.air": "空气", "item.air.name": "空气",
"block.dirt": "泥土", "item.dirt.name": "泥土",
"block.grass_block": "草方块", "item.grass_block.name": "草方块",
"block.grass": "草", "item.grass.name": "草",
"block.leaf": "树叶", "item.leaf.name": "树叶",
"block.log": "原木", "item.log.name": "原木",
"block.sand": "沙子", "item.sand.name": "沙子",
"block.snowy_grass_block": "覆雪草方块", "item.snowy_grass_block.name": "覆雪草方块",
"block.stone": "石头", "item.stone.name": "石头",
"block.water": "水" "item.water.name": "水",
"item.pig_spawn_egg.name": "猪猪刷怪蛋"
} }

View File

@@ -42,7 +42,6 @@ struct LookBlock {
struct BlockData { struct BlockData {
std::string name; std::string name;
std::string name_key;
BlockType id = 0; BlockType id = 0;
bool is_liquid = false; bool is_liquid = false;
@@ -56,17 +55,14 @@ struct BlockData {
bool is_blend = false; bool is_blend = false;
bool is_transitional = false; bool is_transitional = false;
float roughness = 1.0f; float roughness = 1.0f;
BlockData(std::string_view b_name, std::string_view name_k, BlockType b_id, BlockData(std::string_view b_name, BlockType b_id, bool liquid,
bool liquid, bool passable, bool cross_plane, bool transparent, bool passable, bool cross_plane, bool transparent, bool gas,
bool gas, bool discard, bool blend, bool transitional, float r) bool discard, bool blend, bool transitional, float r)
: name(b_name), name_key(name_k), id(b_id), is_liquid(liquid), : name(b_name), id(b_id), is_liquid(liquid), is_gas(gas),
is_gas(gas), is_passable(passable), is_cross_plane(cross_plane), is_passable(passable), is_cross_plane(cross_plane),
is_transparent(transparent), is_discard(discard), is_blend(blend), is_transparent(transparent), is_discard(discard), is_blend(blend),
is_transitional(transitional), roughness(r) {} is_transitional(transitional), roughness(r) {}
BlockData() { BlockData() { name = ""; }
name = "";
name_key = "";
}
}; };
} // namespace Cubed } // namespace Cubed

View File

@@ -11,7 +11,6 @@ public:
static unsigned sums(); static unsigned sums();
static unsigned cross_plane_sum(); static unsigned cross_plane_sum();
static const std::string& name_form_id(BlockType id); static const std::string& name_form_id(BlockType id);
static std::string local_name(BlockType id);
static bool is_gas(BlockType id); static bool is_gas(BlockType id);
static bool is_liquid(BlockType id); static bool is_liquid(BlockType id);

View File

@@ -19,6 +19,7 @@ using ItemProperty = std::variant<BlockType, std::string>;
struct ItemData { struct ItemData {
ItemID id = 0; ItemID id = 0;
std::string name; std::string name;
std::string local_name;
std::string description; std::string description;
std::string path; std::string path;
ItemKind kind = ItemKind::NONE; ItemKind kind = ItemKind::NONE;

View File

@@ -1,6 +1,5 @@
#include "Cubed/gameplay/block_manager.hpp" #include "Cubed/gameplay/block_manager.hpp"
#include "Cubed/localization.hpp"
#include "Cubed/tools/cubed_assert.hpp" #include "Cubed/tools/cubed_assert.hpp"
#include "Cubed/tools/log.hpp" #include "Cubed/tools/log.hpp"
#include "Cubed/tools/toml.utils.hpp" #include "Cubed/tools/toml.utils.hpp"
@@ -36,14 +35,6 @@ const std::string& BlockManager::name_form_id(BlockType id) {
} }
return c->second.name; return c->second.name;
} }
std::string BlockManager::local_name(BlockType id) {
cacc c;
if (!m_datas.find(c, id)) {
ASSERT(false);
return EMPTY.name_key;
}
return tr(c->second.name_key);
}
bool BlockManager::is_gas(BlockType id) { bool BlockManager::is_gas(BlockType id) {
cacc c; cacc c;
if (!m_datas.find(c, id)) { if (!m_datas.find(c, id)) {
@@ -160,12 +151,18 @@ void BlockManager::init() {
auto is_blend = safe_get_value(block, "is_blend", false); auto is_blend = safe_get_value(block, "is_blend", false);
auto is_transitional = safe_get_value(block, "is_transitional", false); auto is_transitional = safe_get_value(block, "is_transitional", false);
auto roughness = safe_get_value(block, "roughness", 1.0); auto roughness = safe_get_value(block, "roughness", 1.0);
auto name_key = safe_get_value(block, "name_key", "Unknow_key");
BlockData data{ BlockData data{*name,
*name, *name_key, static_cast<BlockType>(*id), static_cast<BlockType>(*id),
*is_liquid, *is_passable, *is_cross_plane, *is_liquid,
*is_transparent, *is_gas, *is_discard, *is_passable,
*is_blend, *is_transitional, static_cast<float>(*roughness)}; *is_cross_plane,
*is_transparent,
*is_gas,
*is_discard,
*is_blend,
*is_transitional,
static_cast<float>(*roughness)};
if (!m_datas.emplace(static_cast<BlockType>(*id), std::move(data))) { if (!m_datas.emplace(static_cast<BlockType>(*id), std::move(data))) {
Logger::error("Block Type {} already exist!", *id); Logger::error("Block Type {} already exist!", *id);

View File

@@ -1,6 +1,7 @@
#include "Cubed/gameplay/item_manager.hpp" #include "Cubed/gameplay/item_manager.hpp"
#include "Cubed/gameplay/block_manager.hpp" #include "Cubed/gameplay/block_manager.hpp"
#include "Cubed/localization.hpp"
#include "Cubed/tools/cubed_assert.hpp" #include "Cubed/tools/cubed_assert.hpp"
#include "Cubed/tools/log.hpp" #include "Cubed/tools/log.hpp"
@@ -76,6 +77,7 @@ void ItemManager::add(const std::filesystem::path& path) {
data.property = "cubed:pig"; data.property = "cubed:pig";
} }
} }
data.local_name = tr(std::format("item.{}.name", data.name));
acc a; acc a;
if (m_map.emplace(a, data.id, std::move(data))) { if (m_map.emplace(a, data.id, std::move(data))) {
if (!m_id_map.emplace(a->second.name, a->first)) { if (!m_id_map.emplace(a->second.name, a->first)) {

View File

@@ -109,7 +109,7 @@ void InventoryUI::update_item_info() {
if (type != 0) { if (type != 0) {
auto& data = ItemManager::get(type); auto& data = ItemManager::get(type);
m_item_info->set_text(data.name).set_visible(true); m_item_info->set_text(data.local_name).set_visible(true);
return true; return true;
} }
} }