GigaPasjans
Loading...
Searching...
No Matches
Languages.h
1// Wykonane przez Piotra ChudziƄskiego w dniu 14.05.2025
2#pragma once
3
4#include <foundation/Common.h>
5#include <nlohmann/json.hpp>
6#include <unordered_map>
7#include "vfspp/IFileSystem.h"
8
9#define _TW(key) Phosphorus::Languages::TranslateWide(key)
10#define _T(key) Phosphorus::Languages::Translate(key)
11
12namespace Phosphorus
13{
18 {
19 private:
20 struct Language
21 {
22 String Code;
23 nlohmann::json Dictionary;
24 };
25
26 inline static int32 m_CurrentLanguage = 0;
27
31 inline static std::unordered_map<int32, Language> m_Dictionaries;
32
33 public:
37 template<typename TELang>
38 static void SetLanguage(TELang lang)
39 {
40 m_CurrentLanguage = static_cast<int32>(lang);
41 }
42
46 template<typename TELang>
47 static void LoadDictionary(vfspp::IFileSystemPtr system, const String &path, TELang lang)
48 {
49 const vfspp::IFilePtr ptr = system->OpenFile(vfspp::FileInfo(path.Get()), vfspp::IFile::FileMode::Read);
50
51 std::vector<uint8> fileData;
52 fileData.resize(ptr->Size());
53 ptr->Read(fileData.data(), ptr->Size());
54
55 std::string s = std::string{reinterpret_cast<int8 *>(fileData.data()), fileData.size()};
56
57 const auto &json = nlohmann::json::parse(s);
58
59 Language language;
60 language.Dictionary = json;
61 language.Code = json["generic.code"].get<std::string>();
62
63 m_Dictionaries.emplace(static_cast<int32>(lang), language);
64 INFO("[Languages] Loaded '{}' language ('{}')", json["generic.name"].get<std::string>(),
65 json["generic.name"].get<std::string>());
66 }
67
68 static String Translate(const String &key);
69 static WideString TranslateWide(const String &key);
70 };
71} // namespace Phosphorus
Manages language dictionaries for localization.
Definition Languages.h:18
static void LoadDictionary(vfspp::IFileSystemPtr system, const String &path, TELang lang)
Loads a language dictionary from the specified file path.
Definition Languages.h:47
static void SetLanguage(TELang lang)
Sets current language.
Definition Languages.h:38
Represents a mutable sequence of characters, providing various member functions for string manipulati...
Definition String.h:16
FORCE_INLINE char * Get() const
Gets a raw char sequence.
Definition String.h:163
Represents a mutable sequence of utf-16 characters, providing various member functions for string man...
Definition WideString.h:14