Initial commit

This commit is contained in:
SweetId
2023-10-20 20:17:31 -04:00
commit 2fd0d0034e
13 changed files with 694 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
#pragma once
#include <NazaraUtils/Prerequisites.hpp>
#if defined(NAZARA_LOCALIZATION_STATIC)
#define NAZARA_LOCALIZATION_API
#else
#ifdef NAZARA_LOCALIZATION_BUILD
#define NAZARA_LOCALIZATION_API NAZARA_EXPORT
#else
#define NAZARA_LOCALIZATION_API NAZARA_IMPORT
#endif
#endif

View File

@@ -0,0 +1,46 @@
#pragma once
#include <Nazara/Core/Core.hpp>
#include <NazaraLocalization/Config.hpp>
#include <map>
namespace Nz
{
class NAZARA_LOCALIZATION_API Localization : public Nz::ModuleBase<Localization>
{
friend ModuleBase;
public:
using Dependencies = TypeList<Core>;
struct Config {};
Localization(Config config);
~Localization();
// Loads a CSV containing the localization strings
bool LoadLocalizationFile(const std::filesystem::path& filepath);
// Changes the locale
bool SetLocale(const std::string& locale);
bool FindIndexForKey(std::string_view key, size_t& index) const;
const std::string& GetStringAtIndex(size_t index) const;
private:
struct Locale
{
std::string name;
std::vector<std::string> localizedStrings;
};
std::vector<Locale> m_locales;
std::unordered_map<std::string, size_t> m_lookupTable;
Locale* m_currentLocale;
static Localization* s_instance;
friend class LocalizedText;
};
}

View File

@@ -0,0 +1,71 @@
#pragma once
#include <NazaraLocalization/Config.hpp>
#include <NazaraLocalization/Localization.hpp>
#include <variant>
#include <optional>
#include <ostream>
#include <string_view>
namespace Nz
{
class NAZARA_LOCALIZATION_API LocalizedText
{
public:
LocalizedText();
LocalizedText(std::string_view str);
template <typename T> LocalizedText& Arg(T&& v)
{
NamedParameter p;
p.first = v;
m_parameters.push_back(std::move(p));
return *this;
}
template <> LocalizedText& Arg(const char*&& v) { return Arg(std::string(v)); }
template <typename T> LocalizedText& Arg(std::string_view name, T&& v)
{
NamedParameter p;
p.first = v;
p.second = name;
m_parameters.push_back(std::move(p));
return *this;
}
template <> LocalizedText& Arg(std::string_view name, const char*&& v) { return Arg(name, std::string(v)); }
const char* c_str() const;
const char* data() const;
size_t length() const;
size_t size() const;
const std::string& ToString() const;
friend std::ostream& operator<<(std::ostream& out, const Nz::LocalizedText& dt)
{
return out << dt.ToString();
}
private:
void Update() const;
std::string BuildFormattedString() const;
// Cache for localization
mutable std::optional<size_t> m_index; // index in the lookup array
mutable Localization::Locale* m_locale; // current locale to avoid fetching pointer every time
mutable std::optional<std::string> m_cachedStr; // computed string (loca + parameters formatting)
std::string m_str;
using Parameter = std::variant<
Int8, Int16, Int32, Int64,
UInt8, UInt16, UInt32, UInt64,
float, double,
std::string
>;
using NamedParameter = std::pair<Parameter, std::optional<std::string>>;
std::vector<NamedParameter> m_parameters;
};
}