130 lines
2.6 KiB
C++
130 lines
2.6 KiB
C++
/**
|
|
* @file dl_language.h
|
|
* @brief 本地化
|
|
*
|
|
*
|
|
* @version 1.0
|
|
* @author lveyou
|
|
* @date 21-10-02
|
|
*
|
|
* @note 主要配置管理不同语言的信息
|
|
* @note 由于要实现动态切换,需要保存所有语言的文本
|
|
*/
|
|
#pragma once
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
#include <optional>
|
|
|
|
#include "dl_range.h"
|
|
#include "base/dl_unordered_map2.h"
|
|
#include "io/dl_log.h"
|
|
#include "base/dl_string.h"
|
|
|
|
namespace dl
|
|
{
|
|
//! 语言管理器
|
|
class Language
|
|
{
|
|
public:
|
|
Language();
|
|
/**
|
|
* @brief 从CSV文件加载语言配置文件
|
|
* @note 可以多次调用以读取不同的文件
|
|
*/
|
|
void LoadCsv(std::string_view path_name);
|
|
|
|
/**
|
|
* @brief 失败返回特定字符串,并写入日志
|
|
*/
|
|
std::string_view Get(std::string_view tag, std::string_view name);
|
|
|
|
|
|
/**
|
|
* @brief 失败返回false
|
|
*/
|
|
std::optional<std::string_view> GetCheck(std::string_view tag, std::string_view name)
|
|
{
|
|
Node* node = _map2.Get({ std::string{name}, std::string{tag} });
|
|
if (node)
|
|
{
|
|
return node->_str[_typeCur];
|
|
}
|
|
else
|
|
return {};
|
|
}
|
|
|
|
/**
|
|
* @brief 返回tag包含的子项数量
|
|
*/
|
|
std::size_t GetTagSize(std::string_view tag)
|
|
{
|
|
return _map2.GetXSize(std::string{ tag });
|
|
}
|
|
|
|
/**
|
|
* @brief 返回tag子项,以name字符串开头所有元素
|
|
* @note 例如 {name, ai},将会返回{name, ai_0}、{name, ai_1}、{name, ai_abc}
|
|
*/
|
|
std::vector<std::string_view> GetTagNameAll(std::string_view tag, std::string_view name)
|
|
{//这个函数没有必要返回数量
|
|
auto y = _map2.GetContainerY(std::string{ tag });
|
|
if (!y)
|
|
return {};
|
|
|
|
std::vector<std::string_view> ret;
|
|
for (auto& iter1 : *y)
|
|
{
|
|
const std::string& str = iter1.first;
|
|
if (Sub::IsBegin<std::string_view>(str, name))
|
|
ret.push_back(str);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief 返回所有语言
|
|
*/
|
|
const std::vector<std::string>& GetLangAll();
|
|
|
|
/**
|
|
* @brief 设置当前语言
|
|
*/
|
|
void SetLangCurrent(std::size_t index);
|
|
|
|
~Language();
|
|
private:
|
|
std::vector<std::string> _vecName;//语言标签
|
|
|
|
struct Node
|
|
{
|
|
std::vector<std::string> _str;
|
|
};
|
|
|
|
//有Node,则必须保证至少有一个元素
|
|
UnorderedMap2<std::string, Node> _map2;
|
|
|
|
|
|
size_t _typeCur;
|
|
};
|
|
|
|
extern Language* g_lang;
|
|
|
|
/**
|
|
* @brief 返回一个语言配置的字符串,传入 menu、start
|
|
* @retval 失败返回特定字符串,并输出错误
|
|
*/
|
|
inline std::string_view lang(std::string_view tag, std::string_view name)
|
|
{
|
|
return g_lang->Get(tag, name);
|
|
}
|
|
|
|
/**
|
|
* @brief 返回一个语言配置的字符串,传入 @menu.start
|
|
* @retval 失败返回特定字符串,并输出错误
|
|
*/
|
|
std::string_view lang(std::string_view tag_name);
|
|
|
|
} |