2022-04-10 07:57:02 +00:00
|
|
|
#ifndef COMPONENTS_LUA_I18N_H
|
|
|
|
#define COMPONENTS_LUA_I18N_H
|
|
|
|
|
|
|
|
#include "luastate.hpp"
|
|
|
|
|
|
|
|
#include <components/l10n/messagebundles.hpp>
|
|
|
|
|
2022-07-18 16:36:41 +00:00
|
|
|
namespace VFS
|
|
|
|
{
|
|
|
|
class Manager;
|
|
|
|
}
|
|
|
|
|
2022-04-10 07:57:02 +00:00
|
|
|
namespace LuaUtil
|
|
|
|
{
|
|
|
|
|
|
|
|
class L10nManager
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
L10nManager(const VFS::Manager* vfs, LuaState* lua)
|
|
|
|
: mVFS(vfs)
|
|
|
|
, mLua(lua)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void init();
|
2022-06-06 21:54:22 +00:00
|
|
|
void clear() { mContexts.clear(); }
|
2022-04-10 07:57:02 +00:00
|
|
|
|
|
|
|
void setPreferredLocales(const std::vector<std::string>& locales);
|
|
|
|
const std::vector<icu::Locale>& getPreferredLocales() const { return mPreferredLocales; }
|
|
|
|
|
|
|
|
sol::object getContext(const std::string& contextName, const std::string& fallbackLocale = "en");
|
2022-07-04 17:45:44 +00:00
|
|
|
std::string translate(const std::string& contextName, const std::string& key);
|
2022-04-10 07:57:02 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
struct Context
|
|
|
|
{
|
|
|
|
const std::string mName;
|
|
|
|
// Must be a shared pointer so that sol::make_object copies the pointer, not the data structure.
|
|
|
|
std::shared_ptr<l10n::MessageBundles> mMessageBundles;
|
|
|
|
|
|
|
|
void updateLang(L10nManager* manager);
|
|
|
|
void readLangData(L10nManager* manager, const icu::Locale& lang);
|
|
|
|
std::string translate(std::string_view key, const sol::object& data);
|
|
|
|
};
|
|
|
|
|
|
|
|
const VFS::Manager* mVFS;
|
|
|
|
LuaState* mLua;
|
|
|
|
std::vector<icu::Locale> mPreferredLocales;
|
|
|
|
std::map<std::string, Context> mContexts;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // COMPONENTS_LUA_I18N_H
|