Stellarium 0.12.3
StelTranslator.hpp
Go to the documentation of this file.
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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 */
19 
20 #ifndef _STELTRANSLATOR_HPP_
21 #define _STELTRANSLATOR_HPP_
22 
25 
26 #include <QMap>
27 #include <QString>
28 #include "StelUtils.hpp"
29 #include <QDebug>
30 
31 // These macro are used as global function replacing standard gettext operation
32 #include "gettext.h"
33 
37 #define q_(str) StelTranslator::globalTranslator.qtranslate(str)
38 
42 #define qc_(str, ctxt) StelTranslator::globalTranslator.qtranslate(str, ctxt)
43 
47 #define N_(str) gettext_noop(str)
48 
55 {
56 public:
57 
65  StelTranslator(const QString& adomain, const QString& amoDirectory, const QString& alangName) :
66  domain(adomain), moDirectory(amoDirectory), langName(alangName)
67  {
68  StelTranslator::lastUsed = NULL;
69  }
70 
75  QString qtranslate(const QString& s, const QString& c = QString())
76  {
77  if (s.isEmpty()) return QString();
78  reload();
79  if (c.isEmpty())
80  {
81  return QString::fromUtf8(gettext(s.toUtf8().constData()));
82  }
83  else
84  {
85  // Avoid using pgettext() by manually forming
86  // the context/message combined string.
87  QByteArray bytesC = c.toUtf8();
88  QByteArray bytesS = s.toUtf8();
89  QByteArray glue(1, (char)0x4);
90  QByteArray bytes = bytesC + glue + bytesS;
91  QString t = QString::fromUtf8(gettext(bytes.data()));
92  // If no translation is found, return the original
93  // without the context.
94  if (t.toUtf8() == bytes)
95  return s;
96  else
97  return t;
98  }
99  }
100 
103  const QString& getTrueLocaleName(void) const
104  {
105  if (langName=="system" || langName=="system_default")
106  return StelTranslator::systemLangName;
107  else
108  return langName;
109  }
110 
113 
115  QStringList getAvailableLanguagesNamesNative(const QString& localeDir="") const;
116 
119  static QString iso639_1CodeToNativeName(const QString& languageCode);
120 
122  static QString nativeNameToIso639_1Code(const QString& languageName);
123 
126  static void init(const QString& fileName);
127 
128 private:
131  static void initIso639_1LanguageCodes(const QString& fileName);
132 
134  QStringList getAvailableIso639_1Codes(const QString& localeDir="") const;
135 
137  void reload();
138 
140  QString domain;
141 
143  QString moDirectory;
144 
146  QString langName;
147 
149  static StelTranslator* lastUsed;
150 
152  static void initSystemLanguage(void);
153 
155  static QString systemLangName;
156 
158  static QMap<QString, QString> iso639codes;
159 };
160 
161 #endif // _STELTRANSLATOR_HPP_
162