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.
29
#include "StelUtils.hpp"
31
// These macro are used as global function replacing standard gettext operation
33
#define _(String) Translator::globalTranslator.translate( gettext_noop(String) ).c_str()
34
#define N_(String) gettext_noop(String)
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
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)
58
Translator::lastUsed = NULL;
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)
67
return gettext(s.c_str());
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)
75
if (s=="") return L"";
76
return StelUtils::stringToWstring(translateUTF8(s));
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
83
if (langName=="system" || langName=="system_default") {
84
return Translator::systemLangName;
90
//! Used as a global translator by the whole app
91
static Translator globalTranslator;
93
//! Get available language name in native language from passed locales directory
94
static std::wstring getAvailableLanguagesNamesNative(const string& localeDir);
96
//! Get available language codes from passed locales directory
97
static std::vector<string> getAvailableLanguagesIso639_1Codes(const string& localeDir);
99
//! Convert from ISO639-1 2 letters langage code to native language name
100
static std::wstring iso639_1LanguageCodeToNativeName(const string& languageCode);
102
//! Convert from native language name to ISO639-1 2 letters langage code
103
static std::string nativeLanguageNameCodeToIso639_1(const wstring& languageName);
105
//! Try to determine system language from system configuration
106
static void initSystemLanguage(void);
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);
113
//! Reload the current locale info so that gettext use them
119
//! The directory where the locale file tree stands
122
//! The two letter string defining the current language name
125
//! Keep in memory which one was the last used transator to prevent reloading it at each tranlate() call
126
static Translator* lastUsed;
128
//! Store the system default language name as taken from LANGUAGE environement variable
129
static string systemLangName;
131
//! Contains the list of all iso639 languages codes
132
static map<string, wstring> iso639codes;