Created
March 19, 2021 00:30
-
-
Save Blaumaus/7ae5ec97f8e0b563ed6459e42b9efa91 to your computer and use it in GitHub Desktop.
C++ internalisation mini-library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef INTERNALISATION_EN_HPP | |
#define INTERNALISATION_EN_HPP | |
#pragma once | |
#include <map> | |
#include <string> | |
namespace i18n { | |
std::map<std::string, std::wstring> en { | |
{"hello", L"Hello"}, | |
{"world", L"World"}, | |
}; | |
} | |
#endif // INTERNALISATION_EN_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef INTERNALISATION_HPP | |
#define INTERNALISATION_HPP | |
#pragma once | |
#include <string> | |
#include <map> | |
#include <vector> | |
#include "en.hpp" | |
#include "uk.hpp" | |
namespace i18n { | |
using std::string; | |
using std::vector; | |
using std::map; | |
// WinAPI function to retrieve user's locale, e.g. 'en' or 'uk' | |
std::string get_user_localisation() { | |
wchar_t lpLocaleName[LOCALE_NAME_MAX_LENGTH] = { 0 }; | |
if (GetUserDefaultLocaleName(lpLocaleName, LOCALE_NAME_MAX_LENGTH) != 0) { | |
// check for supported locales here, otherwise return 'en' by default | |
return std::wstring(lpLocaleName).substr(0, 2); | |
} | |
} | |
map<std::string, map<string, wstring>> t { | |
{ "en", en }, | |
{ "uk", uk }, | |
}; | |
class Internalisation { | |
private: | |
vector<string> available; | |
vector<string>::iterator lang_it; | |
// Returns available translation codes | |
vector<string> get_keys() { | |
vector<string> keys; | |
for (auto const& it : t) { | |
keys.push_back(it.first); | |
} | |
return keys; | |
} | |
public: | |
Internalisation() { | |
available = get_keys(); | |
lang_it = available.begin(); | |
} | |
string get_lang() { | |
return *lang_it; | |
} | |
// If a valid language code is provided, it switches current language into it, | |
// otherwise it switches the current language to the next language available | |
void switch_language (string code = "") { | |
if (!code.empty()) { | |
for (auto it = available.begin(); it != available.end(); ++it) { | |
if (*it == code) { | |
lang_it = it; | |
return; | |
} | |
} | |
} | |
if (lang_it == available.end() - 1) lang_it = available.begin(); | |
else ++lang_it; | |
} | |
// Returns the corresponding translation of 'key' | |
std::wstring translate (string key) { | |
return t[*lang_it][key]; | |
} | |
}; | |
} // namespace i18n | |
#endif // INTERNALISATION_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #include all other needed libs | |
#include "i18n/i18n.hpp" | |
using std::wcout; | |
using std::endl; | |
using namespace i18n; | |
int main () { | |
Internalisation i; | |
i.switch_language(get_user_localisation()); // set the initial user's locale | |
wcout << i.translate("hello") << " " << i.translate("world") << endl; // Hello World | |
i.switch_language("uk"); // calling it without params iterates over all available languages step by step | |
wcout << i.translate("hello") << " " << i.translate("world") << endl; // Привіт Світ | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef INTERNALISATION_UK_HPP | |
#define INTERNALISATION_UK_HPP | |
#pragma once | |
#include <map> | |
#include <string> | |
namespace i18n { | |
std::map<std::string, std::wstring> en { | |
{"hello", L"Привіт"}, | |
{"world", L"Світ"}, | |
}; | |
} | |
#endif // INTERNALISATION_UK_HPP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment