StelTranslator.hpp   StelTranslator.hpp 
skipping to change at line 26 skipping to change at line 26
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA. * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*/ */
#ifndef _STELTRANSLATOR_HPP_ #ifndef _STELTRANSLATOR_HPP_
#define _STELTRANSLATOR_HPP_ #define _STELTRANSLATOR_HPP_
//! @file StelTranslator.hpp //! @file StelTranslator.hpp
//! Define some translation macros. //! Define some translation macros.
#include "config.h"
#include <QMap> #include <QMap>
#include <QString> #include <QString>
#include "StelUtils.hpp"
#include <QDebug>
// These macro are used as global function replacing standard gettext opera
tion
#include "gettext.h"
//! @def q_(str) //! @def q_(str)
//! Return the gettext translated english text @a str using the current glo bal translator. //! Return the gettext translated english text @a str using the current glo bal translator.
//! The returned value is a localized QString. //! The returned value is a localized QString.
#define q_(str) StelTranslator::globalTranslator.qtranslate(str) #define q_(str) StelTranslator::globalTranslator->qtranslate(str)
//! @def qc_(str, ctxt) //! @def qc_(str, ctxt)
//! Return the gettext translated english text @a str in context @b ctxt us ing the current global translator. //! Return the gettext translated english text @a str in context @b ctxt us ing the current global translator.
//! The returned value is a localized QString. //! The returned value is a localized QString.
#define qc_(str, ctxt) StelTranslator::globalTranslator.qtranslate(str, ctx t) #define qc_(str, ctxt) StelTranslator::globalTranslator->qtranslate(str, ct xt)
//! @def N_(str) //! @def N_(str)
//! A pseudo function call that serves as a marker for the automated extrac tion of messages. //! A pseudo function call that serves as a marker for the automated extrac tion of messages.
//! A call to N_() doesn't translate. //! A call to N_() doesn't translate.
#define N_(str) gettext_noop(str) #define N_(str) str
//! Class used to translate strings to any language. //! Class used to translate strings to any language.
//! Implements a nice interface to gettext which is UTF-8 compliant and is somewhat multiplateform //! Implements a nice interface to gettext which is UTF-8 compliant and is somewhat multiplateform
//! All its operations do not modify the global locale. //! All its operations do not modify the global locale.
//! The purpose of this class is to remove all non-OO C locale functions fr om stellarium. //! The purpose of this class is to remove all non-OO C locale functions fr om stellarium.
//! @author Fabien Chereau //! @author Fabien Chereau
class StelTranslator class StelTranslator
{ {
public: public:
//! Create a translator from a language name. //! Create a translator from a language name.
//! If the passed locale name cannot be handled by the system, defau lt value will be used. //! If the passed locale name cannot be handled by the system, defau lt value will be used.
//! The passed language name is a language code string like "fr" or "fr_FR". //! The passed language name is a language code string like "fr" or "fr_FR".
//! This class wrap gettext to simulate an object oriented multiplat eform gettext UTF8 translator //! This class wrap gettext to simulate an object oriented multiplat eform gettext UTF8 translator
//! @param adomain The name of the domain to use for translation //! @param adomain The name of the domain to use for translation
//! @param amoDirectory The directory where to look for the domain.m o translation files. //! @param amoDirectory The directory where to look for the domain.m o translation files.
//! @param alangName The C locale name or language name like "fr" or "fr_FR". If string is "" or "system" it will use the system locale. //! @param alangName The C locale name or language name like "fr" or "fr_FR". If string is "" or "system" it will use the system locale.
StelTranslator(const QString& adomain, const QString& amoDirectory, StelTranslator(const QString& adomain, const QString& alangName);
const QString& alangName) :
domain(adomain), moDirectory(amoDirectory), langName ~StelTranslator();
(alangName)
{
StelTranslator::lastUsed = NULL;
}
//! Translate input message and return it as a QString. //! Translate input message and return it as a QString.
//! @param s input string in english. //! @param s input string in english.
//! @param c disambiguation string (gettext "context" string). //! @param c disambiguation string (gettext "context" string).
//! @return The translated QString //! @return The translated QString
QString qtranslate(const QString& s, const QString& c = QString()) QString qtranslate(const QString& s, const QString& c = QString()) c
{ onst;
if (s.isEmpty()) return QString();
reload();
if (c.isEmpty())
{
return QString::fromUtf8(gettext(s.toUtf8().constDat
a()));
}
else
{
// Avoid using pgettext() by manually forming
// the context/message combined string.
QByteArray bytesC = c.toUtf8();
QByteArray bytesS = s.toUtf8();
QByteArray glue(1, (char)0x4);
QByteArray bytes = bytesC + glue + bytesS;
QString t = QString::fromUtf8(gettext(bytes.data()))
;
// If no translation is found, return the original
// without the context.
if (t.toUtf8() == bytes)
return s;
else
return t;
}
}
//! Get true translator locale name. Actual locale, never "system". //! Get true translator locale name. Actual locale, never "system".
//! @return Locale name e.g "fr_FR" //! @return Locale name e.g "fr_FR"
const QString& getTrueLocaleName(void) const const QString& getTrueLocaleName() const
{ {
if (langName=="system" || langName=="system_default") if (langName=="system" || langName=="system_default")
return StelTranslator::systemLangName; return StelTranslator::systemLangName;
else else
return langName; return langName;
} }
//! Used as a global translator by the whole app //! Used as a global translator by the whole app
static StelTranslator globalTranslator; static StelTranslator* globalTranslator;
//! Get available language name in native language from passed local es directory //! Get available language name in native language from passed local es directory
QStringList getAvailableLanguagesNamesNative(const QString& localeDi r="") const; QStringList getAvailableLanguagesNamesNative(const QString& localeDi r="") const;
//! Convert from ISO639-1 langage code to native language name //! Convert from ISO639-1 langage code to native language name
//! @param languageCode the code to look up //! @param languageCode the code to look up
static QString iso639_1CodeToNativeName(const QString& languageCode) ; static QString iso639_1CodeToNativeName(const QString& languageCode) ;
//! Convert from native language name to ISO639-1 2 letters langage code //! Convert from native language name to ISO639-1 2 letters langage code
static QString nativeNameToIso639_1Code(const QString& languageName) ; static QString nativeNameToIso639_1Code(const QString& languageName) ;
//! Initialize Translation //! Initialize Translation
//! @param fileName file containing the list of language codes //! @param fileName file containing the list of language codes
static void init(const QString& fileName); static void init(const QString& fileName);
private: private:
StelTranslator(const StelTranslator& );
const StelTranslator& operator=(const StelTranslator&);
//! Initialize the languages code list from the passed file //! Initialize the languages code list from the passed file
//! @param fileName file containing the list of language codes //! @param fileName file containing the list of language codes
static void initIso639_1LanguageCodes(const QString& fileName); static void initIso639_1LanguageCodes(const QString& fileName);
//! Get available language codes from passed locales directory //! Get available language codes from passed locales directory
QStringList getAvailableIso639_1Codes(const QString& localeDir="") c onst; QStringList getAvailableIso639_1Codes(const QString& localeDir="") c onst;
//! Reload the current locale info so that gettext use them
void reload();
//! The domain name //! The domain name
QString domain; QString domain;
//! The directory where the locale file tree stands
QString moDirectory;
//! The two letter string defining the current language name //! The two letter string defining the current language name
QString langName; QString langName;
//! Keep in memory which one was the last used transator to prevent //! QTranslator instance
reloading it at each tranlate() call class QTranslator* translator;
static StelTranslator* lastUsed;
//! Try to determine system language from system configuration //! Try to determine system language from system configuration
static void initSystemLanguage(void); static void initSystemLanguage(void);
//! Store the system default language name as taken from LANGUAGE en vironement variable //! Store the system default language name as taken from LANGUAGE en vironement variable
static QString systemLangName; static QString systemLangName;
//! Contains the list of all iso639 languages codes //! Contains the list of all iso639 languages codes
static QMap<QString, QString> iso639codes; static QMap<QString, QString> iso639codes;
}; };
 End of changes. 13 change blocks. 
54 lines changed or deleted 17 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/