Stellarium 0.11.4
Home · All Namespaces · All Classes · Functions · Coding Style · Scripting · Plugins · File Structure

core/StelTranslator.hpp

Go to the documentation of this file.
00001 /*
00002 * Stellarium
00003 * Copyright (C) 2005 Fabien Chereau
00004 *
00005 * This program is free software; you can redistribute it and/or
00006 * modify it under the terms of the GNU General Public License
00007 * as published by the Free Software Foundation; either version 2
00008 * of the License, or (at your option) any later version.
00009 * 
00010 * This program is distributed in the hope that it will be useful,
00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 * GNU General Public License for more details.
00014 * 
00015 * You should have received a copy of the GNU General Public License
00016 * along with this program; if not, write to the Free Software
00017 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA.
00018 */
00019 
00020 #ifndef _STELTRANSLATOR_HPP_
00021 #define _STELTRANSLATOR_HPP_
00022 
00025 
00026 #include <QMap>
00027 #include <QString>
00028 #include "StelUtils.hpp"
00029 #include <QDebug>
00030 
00031 // These macro are used as global function replacing standard gettext operation
00032 #include "gettext.h"
00033 
00037 #define q_(str) StelTranslator::globalTranslator.qtranslate(str)
00038 
00042 #define qc_(str, ctxt) StelTranslator::globalTranslator.qtranslate(str, ctxt)
00043 
00047 #define N_(str) gettext_noop(str)
00048 
00054 class StelTranslator
00055 {
00056 public:
00057 
00065     StelTranslator(const QString& adomain, const QString& amoDirectory, const QString& alangName) :
00066             domain(adomain), moDirectory(amoDirectory), langName(alangName)
00067     {
00068         StelTranslator::lastUsed = NULL;
00069     }
00070     
00075     QString qtranslate(const QString& s, const QString& c = QString())
00076     {
00077         if (s.isEmpty()) return QString();
00078         reload();
00079         if (c.isEmpty())
00080         {
00081             return QString::fromUtf8(gettext(s.toUtf8().constData()));
00082         }
00083         else
00084         {
00085             // Avoid using pgettext() by manually forming
00086             // the context/message combined string.
00087             QByteArray bytesC = c.toUtf8();
00088             QByteArray bytesS = s.toUtf8();
00089             QByteArray glue(1, (char)0x4);
00090             QByteArray bytes = bytesC + glue + bytesS;
00091             QString t = QString::fromUtf8(gettext(bytes.data()));
00092             // If no translation is found, return the original
00093             // without the context.
00094             if (t.toUtf8() == bytes)
00095                 return s;
00096             else
00097                 return t;
00098         }
00099     }
00100     
00103     const QString& getTrueLocaleName(void) const
00104     {
00105         if (langName=="system" || langName=="system_default")
00106             return StelTranslator::systemLangName;
00107         else
00108             return langName;
00109     }
00110 
00112     static StelTranslator globalTranslator;
00113 
00115     QStringList getAvailableLanguagesNamesNative(const QString& localeDir="") const;    
00116 
00119     static QString iso639_1CodeToNativeName(const QString& languageCode);
00120     
00122     static QString nativeNameToIso639_1Code(const QString& languageName);
00123     
00126     static void init(const QString& fileName);
00127     
00128 private:
00131     static void initIso639_1LanguageCodes(const QString& fileName);
00132     
00134     QStringList getAvailableIso639_1Codes(const QString& localeDir="") const;
00135     
00137     void reload();
00138 
00140     QString domain;
00141 
00143     QString moDirectory;
00144 
00146     QString langName;
00147 
00149     static StelTranslator* lastUsed;
00150 
00152     static void initSystemLanguage(void);
00153     
00155     static QString systemLangName;
00156     
00158     static QMap<QString, QString> iso639codes;
00159 };
00160 
00161 #endif // _STELTRANSLATOR_HPP_
00162 
Generated on Sat Aug 25 22:13:30 2012 for Stellarium by  doxygen 1.6.3