looper/translation.cpp

63 lines
No EOL
1.6 KiB
C++

#include "translation.hpp"
#include <libintl.h>
#include <locale.h>
#include "config.h"
static const char *orig_language;
static const char *cur_locale_dir;
static const char *cur_locale;
const char *tr_ctx(const char *ctx, const char *msgid) {
std::string msg_ctxt_id = (std::string(ctx) + std::string("\004") + std::string(msgid));
const char *translation = gettext(msg_ctxt_id.c_str());
if (std::string(translation) == msg_ctxt_id) {
return msgid;
} else {
return translation;
}
}
const char *tr(const char *msgid) {
return gettext(msgid);
}
void set_language(const char *language) {
if (language == nullptr) {
language = "";
}
#ifdef LIBINTL_LITE_API
if (language[0] == '\0') {
setenv("LANGUAGE", orig_language, 1);
} else {
setenv("LANGUAGE", language, 1);
}
#else
setlocale(LC_ALL, language);
#endif
bindtextdomain(cur_locale, cur_locale_dir);
textdomain(cur_locale);
}
char *get_language() {
#ifdef LIBINTL_LITE_API
return getenv("LANGUAGE");
#else
return setlocale(LC_MESSAGES, NULL);
#endif
}
void setup_locale(const char *locale, const char *locale_dir) {
if (orig_language == nullptr) {
orig_language = getenv("LANGUAGE");
if (orig_language == nullptr) {
orig_language = "C.UTF-8";
}
}
if (locale_dir == nullptr) {
if (cur_locale_dir == nullptr) {
locale_dir = LOCALE_DIR;
} else {
locale_dir = cur_locale_dir;
}
}
if (cur_locale_dir == nullptr) {
cur_locale_dir = locale_dir;
}
cur_locale = locale;
set_language();
}