~ubuntu-branches/debian/jessie/stellarium/jessie

« back to all changes in this revision

Viewing changes to src/stelutils/Translator.hpp

Tags: upstream-0.9.0
Import upstream version 0.9.0

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
#ifndef TRANSLATOR_H
 
21
#define TRANSLATOR_H
 
22
 
 
23
#include <config.h>
 
24
 
 
25
#include <string>
 
26
#include <iostream>
 
27
#include <map>
 
28
#include <vector>
 
29
#include "StelUtils.hpp"
 
30
 
 
31
// These macro are used as global function replacing standard gettext operation
 
32
#include "gettext.h"
 
33
#define _(String) Translator::globalTranslator.translate( gettext_noop(String) ).c_str()
 
34
#define N_(String) gettext_noop(String)
 
35
 
 
36
using namespace std;
 
37
 
 
38
//! Class used to translate strings to any language.
 
39
//! Implements a nice interface to gettext which is UTF-8 compliant and is somewhat multiplateform
 
40
//! All its operations do not modify the global locale.
 
41
//! The purpose of this class is to remove all non-OO C locale functions from stellarium.
 
42
//! It could be extended for all locale management using e.g the portable IBM ICU library.
 
43
//! @author Fabien Chereau
 
44
class Translator
 
45
{
 
46
public:
 
47
 
 
48
        //! @brief Create a translator from a language name.
 
49
        //! If the passed locale name cannot be handled by the system, default value will be used.
 
50
        //! The passed language name is a language code string like "fr" or "fr_FR".
 
51
        //! This class wrap gettext to simulate an object oriented multiplateform gettext UTF8 translator
 
52
        //! @param _domain The name of the domain to use for translation
 
53
        //! @param _moDirectory The directory where to look for the domain.mo translation files.
 
54
        //! @param _langName The C locale name or language name like "fr" or "fr_FR". If string is "" or "system" it will use the system locale.
 
55
        Translator(const std::string& _domain, const std::string& _moDirectory, const std::string& _langName) :
 
56
                        domain(_domain), moDirectory(_moDirectory), langName(_langName)
 
57
        {
 
58
                Translator::lastUsed = NULL;
 
59
        }
 
60
 
 
61
        //! @brief Translate input message.
 
62
        //! @param s input string in english.
 
63
        //! @return The translated string in UTF-8 characters.
 
64
        std::string translateUTF8(const std::string& s)
 
65
        {
 
66
                reload();
 
67
                return gettext(s.c_str());
 
68
        }
 
69
 
 
70
        //! @brief Translate input message.
 
71
        //! @param s input string in english.
 
72
        //! @return The translated string in wide characters.
 
73
        std::wstring translate(const std::string& s)
 
74
        {
 
75
                if (s=="") return L"";
 
76
                return StelUtils::stringToWstring(translateUTF8(s));
 
77
        }
 
78
        
 
79
        //! @brief Get true translator locale name. Actual locale, never "system" 
 
80
        //! @return Locale name e.g "fr_FR"
 
81
        const std::string& getTrueLocaleName(void) const
 
82
        {
 
83
                if (langName=="system" || langName=="system_default") {
 
84
                        return Translator::systemLangName;
 
85
                } else {
 
86
                        return langName;
 
87
                }
 
88
        }
 
89
 
 
90
        //! Used as a global translator by the whole app
 
91
        static Translator globalTranslator;
 
92
 
 
93
        //! Get available language name in native language from passed locales directory
 
94
        static std::wstring getAvailableLanguagesNamesNative(const string& localeDir);  
 
95
        
 
96
        //! Get available language codes from passed locales directory
 
97
        static std::vector<string> getAvailableLanguagesIso639_1Codes(const string& localeDir);
 
98
 
 
99
        //! Convert from ISO639-1 2 letters langage code to native language name
 
100
        static std::wstring iso639_1LanguageCodeToNativeName(const string& languageCode);
 
101
        
 
102
        //! Convert from native language name to ISO639-1 2 letters langage code 
 
103
        static std::string nativeLanguageNameCodeToIso639_1(const wstring& languageName);
 
104
        
 
105
        //! Try to determine system language from system configuration
 
106
        static void initSystemLanguage(void);
 
107
 
 
108
        //! Initialize the languages code list from the passed file
 
109
        //! @param fileName file containing the list of language codes
 
110
        static void initIso639_1LanguageCodes(const string& fileName);
 
111
        
 
112
private:
 
113
        //! Reload the current locale info so that gettext use them
 
114
        void reload();
 
115
 
 
116
        //! The domain name
 
117
        string domain;
 
118
 
 
119
        //! The directory where the locale file tree stands
 
120
        string moDirectory;
 
121
 
 
122
        //! The two letter string defining the current language name
 
123
        string langName;
 
124
 
 
125
        //! Keep in memory which one was the last used transator to prevent reloading it at each tranlate() call
 
126
        static Translator* lastUsed;
 
127
 
 
128
        //! Store the system default language name as taken from LANGUAGE environement variable
 
129
        static string systemLangName;
 
130
        
 
131
        //! Contains the list of all iso639 languages codes
 
132
        static map<string, wstring> iso639codes;
 
133
};
 
134
 
 
135
#endif