3
* Copyright (C) 2005 Fabien Chereau
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28
#include "translator.h"
30
Translator* Translator::lastUsed = NULL;
32
string Translator::systemLangName = "C";
34
// Use system locale language by default
35
Translator Translator::globalTranslator = Translator(PACKAGE, LOCALEDIR, "system");
42
//! Try to determine system language from system configuration
43
void Translator::initSystemLanguage(void)
45
char* lang = getenv("LANGUAGE");
46
if (lang) Translator::systemLangName = lang;
49
lang = getenv("LANG");
50
if (lang) Translator::systemLangName = lang;
55
if(GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SISO639LANGNAME, cc, 3))
58
Translator::systemLangName = cc;
62
Translator::systemLangName = "C";
65
Translator::systemLangName = "C";
71
void Translator::reload()
73
if (Translator::lastUsed == this) return;
74
// This needs to be static as it is used a each gettext call... It tooks me quite a while before I got that :(
75
static char envstr[25];
77
if (langName=="system" || langName=="system_default")
79
snprintf(envstr, 25, "LANGUAGE=%s", Translator::systemLangName.c_str());
80
// cout << "TEST=" << envstr << " " << Translator::systemLangName << endl;
84
snprintf(envstr, 25, "LANGUAGE=%s", langName.c_str());
87
if (langName=="system" || langName=="system_default")
89
snprintf(envstr, 25, "LANG=%s", Translator::systemLangName.c_str());
93
snprintf(envstr, 25, "LANG=%s", langName.c_str());
95
//printf("Setting locale: %s\n", envstr);
97
setlocale(LC_MESSAGES, "");
99
std::string result = bind_textdomain_codeset(domain.c_str(), "UTF-8");
100
assert(result=="UTF-8");
101
bindtextdomain (domain.c_str(), moDirectory.c_str());
102
textdomain (domain.c_str());
103
Translator::lastUsed = this;
107
//! Convert from char* UTF-8 to wchar_t UCS4 - stolen from SDL_ttf library
108
static wchar_t *UTF8_to_UNICODE(wchar_t *unicode, const char *utf8, int len)
111
unsigned short ch; // 16 bits
113
for ( i=0, j=0; i < len; ++i, ++j )
115
ch = ((const unsigned char *)utf8)[i];
118
ch = (unsigned short)(utf8[i]&0x07) << 18;
119
ch |= (unsigned short)(utf8[++i]&0x3F) << 12;
120
ch |= (unsigned short)(utf8[++i]&0x3F) << 6;
121
ch |= (unsigned short)(utf8[++i]&0x3F);
126
ch = (unsigned short)(utf8[i]&0x3F) << 12;
127
ch |= (unsigned short)(utf8[++i]&0x3F) << 6;
128
ch |= (unsigned short)(utf8[++i]&0x3F);
133
ch = (unsigned short)(utf8[i]&0x3F) << 6;
134
ch |= (unsigned short)(utf8[++i]&0x3F);
144
//! Convert from UTF-8 to wchar_t
145
//! Warning this is likely to be not very portable
146
std::wstring Translator::UTF8stringToWstring(const string& s)
148
wchar_t* outbuf = new wchar_t[s.length()+1];
149
UTF8_to_UNICODE(outbuf, s.c_str(), s.length());
155
//! Get available language codes from directory tree
156
std::string Translator::getAvailableLanguagesCodes(const string& localeDir)
158
struct dirent *entryp;
160
std::vector<string> result;
162
//cout << "Reading stellarium translations in directory: " << localeDir << endl;
164
if ((dp = opendir(localeDir.c_str())) == NULL)
166
cerr << "Unable to find locale directory containing translations:" << localeDir << endl;
170
while ((entryp = readdir(dp)) != NULL)
172
string tmp = entryp->d_name;
173
string tmpdir = localeDir+"/"+tmp+"/LC_MESSAGES/stellarium.mo";
174
FILE* fic = fopen(tmpdir.c_str(), "r");
177
result.push_back(tmp);
183
// Sort the language names by alphabetic order
184
std::sort(result.begin(), result.end());
187
std::vector<string>::iterator iter;
188
for (iter=result.begin();iter!=result.end();++iter)
190
if (iter!=result.begin()) output+="\n";