mirror of
https://repo.dec05eba.com/gpu-screen-recorder-ui
synced 2026-03-31 09:17:04 +09:00
Implemented a basic translation system
This commit is contained in:
36
include/Translation.hpp
Normal file
36
include/Translation.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace gsr {
|
||||
class Translation {
|
||||
public:
|
||||
static Translation& instance();
|
||||
void init(const char* translations_directory, const char* initial_language = nullptr);
|
||||
bool load_language(const char* lang);
|
||||
const char* translate(const char* key);
|
||||
|
||||
template<typename... Args>
|
||||
std::string format(const char* key, Args&&... args) {
|
||||
const char* fmt = translate(key);
|
||||
|
||||
// result buffer
|
||||
char buffer[4096];
|
||||
snprintf(buffer, sizeof(buffer), fmt, std::forward<Args>(args)...);
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
private:
|
||||
std::string get_system_language();
|
||||
std::string trim(const std::string& str);
|
||||
|
||||
private:
|
||||
std::string translations_directory;
|
||||
std::string current_language = "en";
|
||||
std::unordered_map<std::string, std::string> translations;
|
||||
};
|
||||
}
|
||||
|
||||
#define TR(s) gsr::Translation::instance().translate(s)
|
||||
#define TRF(s, ...) gsr::Translation::instance().format(s, __VA_ARGS__)
|
||||
Reference in New Issue
Block a user