~ubuntu-branches/debian/squeeze/stellarium/squeeze

« back to all changes in this revision

Viewing changes to src/translator.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cédric Delfosse
  • Date: 2008-05-19 21:28:23 UTC
  • mfrom: (3.1.5 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080519212823-m5nfiuntxstxzxj7
Tags: 0.9.1-4
Add libxcursor-dev, libxfixes-dev, libxinerama-dev, libqt4-opengl-dev to
build-deps (Closes: #479906)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
* Stellarium
3
 
* Copyright (C) 2005 Fabien Chereau
4
 
*
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.
9
 
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.
14
 
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.
18
 
*/
19
 
 
20
 
#include <config.h>
21
 
#include <cassert>
22
 
#include <dirent.h>
23
 
#include <cstdio>
24
 
#include <vector>
25
 
#include <algorithm>
26
 
 
27
 
#include "bytes.h"
28
 
#include "translator.h"
29
 
 
30
 
Translator* Translator::lastUsed = NULL;
31
 
 
32
 
string Translator::systemLangName = "C";
33
 
 
34
 
// Use system locale language by default
35
 
Translator Translator::globalTranslator = Translator(PACKAGE, LOCALEDIR, "system");
36
 
 
37
 
#ifdef WIN32
38
 
# include <Windows.h>
39
 
# include <Winnls.h>
40
 
#endif
41
 
 
42
 
//! Try to determine system language from system configuration
43
 
void Translator::initSystemLanguage(void)
44
 
{
45
 
        char* lang = getenv("LANGUAGE");
46
 
        if (lang) Translator::systemLangName = lang;
47
 
        else
48
 
        {
49
 
                lang = getenv("LANG");
50
 
                if (lang) Translator::systemLangName = lang;
51
 
                else
52
 
                {
53
 
#ifdef WIN32            
54
 
            char cc[3];
55
 
            if(GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SISO639LANGNAME, cc, 3))
56
 
            {
57
 
                cc[2] = '\0';
58
 
                Translator::systemLangName = cc;
59
 
            }
60
 
            else
61
 
            {
62
 
                Translator::systemLangName = "C";
63
 
            }
64
 
#else
65
 
     Translator::systemLangName = "C";
66
 
#endif
67
 
        }
68
 
        }
69
 
}
70
 
 
71
 
void Translator::reload()
72
 
{
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];
76
 
#ifndef MACOSX  
77
 
        if (langName=="system" || langName=="system_default")
78
 
        {
79
 
                snprintf(envstr, 25, "LANGUAGE=%s", Translator::systemLangName.c_str());
80
 
//              cout << "TEST=" << envstr << " " << Translator::systemLangName << endl;
81
 
        }
82
 
        else
83
 
        {
84
 
                snprintf(envstr, 25, "LANGUAGE=%s", langName.c_str());
85
 
        }
86
 
#else
87
 
        if (langName=="system" || langName=="system_default")
88
 
        {
89
 
                snprintf(envstr, 25, "LANG=%s", Translator::systemLangName.c_str());
90
 
        }
91
 
        else
92
 
        {
93
 
                snprintf(envstr, 25, "LANG=%s", langName.c_str());      
94
 
#endif
95
 
        //printf("Setting locale: %s\n", envstr);
96
 
        putenv(envstr);
97
 
        setlocale(LC_MESSAGES, "");
98
 
 
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;
104
 
}
105
 
 
106
 
 
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)
109
 
{
110
 
        int i, j;
111
 
        unsigned short ch;  // 16 bits
112
 
 
113
 
        for ( i=0, j=0; i < len; ++i, ++j )
114
 
        {
115
 
                ch = ((const unsigned char *)utf8)[i];
116
 
                if ( ch >= 0xF0 )
117
 
                {
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);
122
 
                }
123
 
                else
124
 
                        if ( ch >= 0xE0 )
125
 
                        {
126
 
                                ch  =  (unsigned short)(utf8[i]&0x3F) << 12;
127
 
                                ch |=  (unsigned short)(utf8[++i]&0x3F) << 6;
128
 
                                ch |=  (unsigned short)(utf8[++i]&0x3F);
129
 
                        }
130
 
                        else
131
 
                                if ( ch >= 0xC0 )
132
 
                                {
133
 
                                        ch  =  (unsigned short)(utf8[i]&0x3F) << 6;
134
 
                                        ch |=  (unsigned short)(utf8[++i]&0x3F);
135
 
                                }
136
 
 
137
 
                unicode[j] = ch;
138
 
        }
139
 
        unicode[j] = 0;
140
 
 
141
 
        return unicode;
142
 
}
143
 
 
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)
147
 
{
148
 
        wchar_t* outbuf = new wchar_t[s.length()+1];
149
 
        UTF8_to_UNICODE(outbuf, s.c_str(), s.length());
150
 
        wstring ws(outbuf);
151
 
        delete[] outbuf;
152
 
        return ws;
153
 
}
154
 
 
155
 
//! Get available language codes from directory tree
156
 
std::string Translator::getAvailableLanguagesCodes(const string& localeDir)
157
 
{
158
 
        struct dirent *entryp;
159
 
        DIR *dp;
160
 
        std::vector<string> result;
161
 
        
162
 
        //cout << "Reading stellarium translations in directory: " << localeDir << endl;
163
 
 
164
 
        if ((dp = opendir(localeDir.c_str())) == NULL)
165
 
        {
166
 
                cerr << "Unable to find locale directory containing translations:" << localeDir << endl;
167
 
                return "";
168
 
        }
169
 
 
170
 
        while ((entryp = readdir(dp)) != NULL)
171
 
        {
172
 
                string tmp = entryp->d_name;
173
 
                string tmpdir = localeDir+"/"+tmp+"/LC_MESSAGES/stellarium.mo";
174
 
                FILE* fic = fopen(tmpdir.c_str(), "r");
175
 
                if (fic)
176
 
                {
177
 
                        result.push_back(tmp);
178
 
                        fclose(fic);
179
 
                }
180
 
        }
181
 
        closedir(dp);
182
 
        
183
 
        // Sort the language names by alphabetic order
184
 
        std::sort(result.begin(), result.end());
185
 
 
186
 
        string output;
187
 
        std::vector<string>::iterator iter;
188
 
        for (iter=result.begin();iter!=result.end();++iter)
189
 
        {
190
 
                if (iter!=result.begin()) output+="\n";
191
 
                output+=*iter;
192
 
        }
193
 
 
194
 
        return output;
195
 
}
196