CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/Data/Text/I18n.h
Views: 1401
#pragma once12// I18N = I....18..dots.....N = INTERNATIONALIZATION34// Super simple I18N library.5// Just enough to be useful and usable.6// Spits out easy-to-edit utf-8 .INI files.78// As usual, everything is UTF-8. Nothing else allowed.910#include <map>11#include <mutex>12#include <string>13#include <string_view>14#include <vector>15#include <memory>1617#include "Common/Common.h"18#include "Common/File/Path.h"1920// Reasonably thread safe.2122class I18NRepo;23class IniFile;24class Section;2526enum class I18NCat : uint8_t {27AUDIO = 0,28CONTROLS,29CWCHEATS,30DESKTOPUI,31DEVELOPER,32DIALOG,33ERRORS, // Can't name it ERROR, clashes with many defines.34GAME,35GRAPHICS,36INSTALLZIP,37KEYMAPPING,38MAINMENU,39MAINSETTINGS,40MAPPABLECONTROLS,41NETWORKING,42PAUSE,43POSTSHADERS,44PSPCREDITS,45MEMSTICK,46REMOTEISO,47REPORTING,48SAVEDATA,49SCREEN,50SEARCH,51STORE,52SYSINFO,53SYSTEM,54TEXTURESHADERS,55THEMES,56UI_ELEMENTS,57UPGRADE,58VR,59ACHIEVEMENTS,60PSPSETTINGS,61CATEGORY_COUNT,62NONE = CATEGORY_COUNT,63};6465struct I18NEntry {66I18NEntry(std::string_view t) : text(t), readFlag(false) {}67I18NEntry() : readFlag(false) {}68std::string text;69bool readFlag;70};7172class I18NCategory {73public:74I18NCategory() {}75explicit I18NCategory(const Section §ion);7677// Faster since the string lengths don't need to be recomputed.78std::string_view T(std::string_view key, std::string_view def = "");7980// Try to avoid this. Still useful in snprintf.81const char *T_cstr(const char *key, const char *def = nullptr);8283std::map<std::string, std::string> Missed() const;8485const std::map<std::string, I18NEntry, std::less<>> &GetMap() { return map_; }86void ClearMissed() { missedKeyLog_.clear(); }87void Clear();8889private:90I18NCategory(I18NRepo *repo, const char *name) {}91void SetMap(const std::map<std::string, std::string> &m);9293// std::less<> is needed to be able to look up string_views in a string-keyed map.94std::map<std::string, I18NEntry, std::less<>> map_;95mutable std::mutex missedKeyLock_;96std::map<std::string, std::string> missedKeyLog_;9798// Noone else can create these.99friend class I18NRepo;100};101102class I18NRepo {103public:104I18NRepo();105bool IniExists(const std::string &languageID) const;106bool LoadIni(const std::string &languageID, const Path &overridePath = Path()); // NOT the filename!107108std::string LanguageID();109110std::shared_ptr<I18NCategory> GetCategory(I18NCat category);111112// Translate the string, by looking up "key" in the file, and falling back to either def or key, in that order, if the lookup fails.113// def can (and usually is) set to nullptr.114std::string_view T(I18NCat category, std::string_view key, std::string_view def = "") {115if (category == I18NCat::NONE)116return !def.empty() ? def : key;117return cats_[(size_t)category]->T(key, def);118}119const char *T_cstr(I18NCat category, const char *key, const char *def = nullptr) {120if (category == I18NCat::NONE)121return def ? def : key;122return cats_[(size_t)category]->T_cstr(key, def);123}124void LogMissingKeys() const;125126private:127Path GetIniPath(const std::string &languageID) const;128void Clear();129130mutable std::mutex catsLock_;131std::shared_ptr<I18NCategory> cats_[(size_t)I18NCat::CATEGORY_COUNT];132std::string languageID_;133};134135extern I18NRepo g_i18nrepo;136137// These are simply talking to the one global instance of I18NRepo.138139std::shared_ptr<I18NCategory> GetI18NCategory(I18NCat cat);140141inline std::string_view T(I18NCat category, std::string_view key, std::string_view def = "") {142return g_i18nrepo.T(category, key, def);143}144145inline const char *T_cstr(I18NCat category, const char *key, const char *def = "") {146return g_i18nrepo.T_cstr(category, key, def);147}148149150