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

@@ -1,6 +1,5 @@
#include "Cubed/gameplay/block_manager.hpp"
#include "Cubed/localization.hpp"
#include "Cubed/tools/cubed_assert.hpp"
#include "Cubed/tools/log.hpp"
#include "Cubed/tools/toml.utils.hpp"
@@ -36,14 +35,6 @@ const std::string& BlockManager::name_form_id(BlockType id) {
}
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) {
cacc c;
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_transitional = safe_get_value(block, "is_transitional", false);
auto roughness = safe_get_value(block, "roughness", 1.0);
auto name_key = safe_get_value(block, "name_key", "Unknow_key");
BlockData data{
*name, *name_key, static_cast<BlockType>(*id),
*is_liquid, *is_passable, *is_cross_plane,
*is_transparent, *is_gas, *is_discard,
*is_blend, *is_transitional, static_cast<float>(*roughness)};
BlockData data{*name,
static_cast<BlockType>(*id),
*is_liquid,
*is_passable,
*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))) {
Logger::error("Block Type {} already exist!", *id);

View File

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

View File

@@ -109,7 +109,7 @@ void InventoryUI::update_item_info() {
if (type != 0) {
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;
}
}