/
gettext_boost.cpp
140 lines (120 loc) · 3.63 KB
/
gettext_boost.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
Copyright (C) 2003 - 2014 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "global.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include <iostream>
#include <locale>
#include <boost/locale.hpp>
#include <set>
#define DBG_G LOG_STREAM(debug, lg::general)
#define LOG_G LOG_STREAM(info, lg::general)
#define WRN_G LOG_STREAM(warn, lg::general)
#define ERR_G LOG_STREAM(err, lg::general)
namespace
{
struct translation_manager
{
translation_manager()
: loaded_paths_()
, loaded_domains_()
, current_language_()
, generator_()
, current_locale_()
{
generator_.use_ansi_encoding(false);
update_locale();
}
void update_locale() { current_locale_ = generator_.generate(current_language_); }
std::set<std::string> loaded_paths_;
std::set<std::string> loaded_domains_;
std::string current_language_;
boost::locale::generator generator_;
std::locale current_locale_;
};
translation_manager& get_manager()
{
static translation_manager* mng = new translation_manager();
return *mng;
}
}
namespace translation
{
std::string dgettext(const char* domain, const char* msgid)
{
return boost::locale::dgettext(domain, msgid, get_manager().current_locale_);
}
std::string egettext(char const *msgid)
{
return msgid[0] == '\0' ? msgid : boost::locale::gettext(msgid, get_manager().current_locale_);
}
std::string dsgettext (const char * domainname, const char *msgid)
{
std::string msgval = dgettext (domainname, msgid);
if (msgval == msgid) {
const char* firsthat = std::strrchr (msgid, '^');
if (firsthat == NULL)
msgval = msgid;
else
msgval = firsthat + 1;
}
return msgval;
}
std::string dsngettext (const char * domainname, const char *singular, const char *plural, int n)
{
std::string msgval = boost::locale::dngettext(domainname, singular, plural, n, get_manager().current_locale_);
if (msgval == singular) {
const char* firsthat = std::strrchr (singular, '^');
if (firsthat == NULL)
msgval = singular;
else
msgval = firsthat + 1;
}
return msgval;
}
void bind_textdomain(const char* domain, const char* direcory, const char* /*encoding*/)
{
LOG_G << "adding textdomain '" << domain << "' in directory '" << direcory << "'\n";
get_manager().generator_.add_messages_domain(domain);
get_manager().generator_.add_messages_path(direcory);
get_manager().update_locale();
}
void set_default_textdomain(const char* domain)
{
get_manager().generator_.set_default_messages_domain(domain);
get_manager().update_locale();
}
void set_language(const std::string& language, const std::vector<std::string>* /*alternates*/)
{
// why shoudl we need alternates? which languages we support shoudl only be related
// to which languages we ship with and not which the os supports
LOG_G << "setting language to '" << language << "' \n";
std::string::size_type at_pos = language.rfind('@');
if(language.empty())
{
//Don't add "UTF-8" to default language.
get_manager().current_language_ = "";
}
else if(at_pos != std::string::npos)
{
get_manager().current_language_ = language.substr(0, at_pos) + ".UTF-8" + language.substr(at_pos);
}
else
{
get_manager().current_language_ = language + ".UTF-8";
}
get_manager().update_locale();
}
void init()
{
}
}