StelTranslator.cpp   StelTranslator.cpp 
skipping to change at line 20 skipping to change at line 20
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* 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.
*/ */
#ifdef _MSC_BUILD #include "StelTranslator.hpp"
#include "kdewin32/dirent.h" #include "StelFileMgr.hpp"
#else #include "StelUtils.hpp"
#include <dirent.h>
#endif
#include <cstdio> #include <cstdio>
#include <algorithm> #include <algorithm>
#include <fstream> #include <fstream>
#include <clocale> #include <clocale>
#include <cstdlib> #include <cstdlib>
#include <QtGlobal>
#ifdef Q_OS_WIN
#include <winsock2.h>
#include <windows.h>
#endif
#include <QFile> #include <QFile>
#include <QDebug> #include <QDebug>
#include <QStringList> #include <QStringList>
#include <QRegExp> #include <QRegExp>
#include <QLocale> #include <QLocale>
#include <QDir> #include <QDir>
#include <QTranslator>
#include "StelUtils.hpp"
#include "StelTranslator.hpp"
#include "StelFileMgr.hpp"
// Init static members // Init static members
StelTranslator* StelTranslator::lastUsed = NULL;
QMap<QString, QString> StelTranslator::iso639codes; QMap<QString, QString> StelTranslator::iso639codes;
QString StelTranslator::systemLangName; QString StelTranslator::systemLangName;
// Use system locale language by default // Use system locale language by default
StelTranslator StelTranslator::globalTranslator = StelTranslator("stellariu m", "./locale/", "system"); StelTranslator* StelTranslator::globalTranslator = NULL;
#ifdef Q_OS_WIN StelTranslator::StelTranslator(const QString& adomain, const QString& alang
# include <windows.h> Name)
# include <winnls.h> : domain(adomain),
#define putenv(x) _putenv((x)) langName(alangName)
#endif {
translator = new QTranslator();
bool res = translator->load(StelFileMgr::getLocaleDir()+"/"+adomain+
"/"+getTrueLocaleName()+".qm");
if (!res)
qWarning() << "Couldn't load translations for language " <<
getTrueLocaleName();
if (translator->isEmpty())
qWarning() << "Empty translation file for language " << getT
rueLocaleName();
}
StelTranslator::~StelTranslator()
{
delete translator;
translator = 0;
}
QString StelTranslator::qtranslate(const QString& s, const QString& c) cons
t
{
if (s.isEmpty())
return "";
QString res = translator->translate("", s.toUtf8().constData(), c.to
Utf8().constData());
if (res.isEmpty())
return s;
return res;
}
//! Initialize Translation //! Initialize Translation
//! @param fileName file containing the list of language codes //! @param fileName file containing the list of language codes
void StelTranslator::init(const QString& fileName) void StelTranslator::init(const QString& fileName)
{ {
StelTranslator::initSystemLanguage(); StelTranslator::initSystemLanguage();
StelTranslator::initIso639_1LanguageCodes(fileName); StelTranslator::initIso639_1LanguageCodes(fileName);
Q_ASSERT(StelTranslator::globalTranslator==NULL);
StelTranslator::globalTranslator = new StelTranslator("stellarium",
"system");
} }
//! Try to determine system language from system configuration //! Try to determine system language from system configuration
void StelTranslator::initSystemLanguage(void) void StelTranslator::initSystemLanguage()
{ {
char* lang = getenv("LANGUAGE"); systemLangName = QLocale::system().name();
if (lang) systemLangName = lang;
else
{
lang = getenv("LANG");
if (lang) systemLangName = lang;
else
{
#ifdef Q_OS_WIN
char ulng[3], ctry[3];
if (GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCALE_SISO6
39LANGNAME, ulng, 3))
{
ulng[2] = '\0';
if (GetLocaleInfoA(LOCALE_USER_DEFAULT, LOCA
LE_SISO3166CTRYNAME, ctry, 3))
{
ctry[2] = '\0';
systemLangName = QString("%1_%2").ar
g(ulng).arg(ctry);
}
else
{
systemLangName = ulng;
}
}
else
{
systemLangName = "C";
}
#else
systemLangName = "C";
#endif
}
}
if (systemLangName.isEmpty()) if (systemLangName.isEmpty())
systemLangName = "en"; systemLangName = "en";
//change systemLangName to ISO 639 / ISO 3166. //change systemLangName to ISO 639 / ISO 3166.
int pos = systemLangName.indexOf(':', 0); int pos = systemLangName.indexOf(':', 0);
if (pos != -1) systemLangName.resize(pos); if (pos != -1) systemLangName.resize(pos);
pos = systemLangName.indexOf('.', 0); pos = systemLangName.indexOf('.', 0);
if (pos != -1) systemLangName.resize(pos); if (pos != -1) systemLangName.resize(pos);
} }
void StelTranslator::reload()
{
if (StelTranslator::lastUsed == this)
return;
// Find out what the system language is if not defined yet
if (systemLangName.isEmpty())
initSystemLanguage();
// Apply that
// This needs to be static as it is used a each gettext call... It t
ooks me quite a while before I got that :(
static char envstr[128];
if (langName=="system" || langName=="system_default")
#ifdef Q_OS_MAC
{
qsnprintf(envstr, 128, "LANG=%s", StelTranslator::systemLang
Name.toUtf8().constData());
}
else
{
qsnprintf(envstr, 128, "LANG=%s", langName.toUtf8().constDat
a());
}
#elif defined (Q_OS_WIN) //MSCVER
{
qsnprintf(envstr, 128, "LANGUAGE=%s", StelTranslator::system
LangName.toUtf8().constData());
}
else
{
qsnprintf(envstr, 128, "LANGUAGE=%s", langName.toUtf8().cons
tData());
}
#else // UNIX
{
qsnprintf(envstr, 128, "LANGUAGE=%s", StelTranslator::system
LangName.toUtf8().constData());
}
else
{
qsnprintf(envstr, 128, "LANGUAGE=%s", langName.toUtf8().cons
tData());
}
#endif
putenv(envstr);
#ifndef Q_OS_WIN
setlocale(LC_MESSAGES, "");
#else
setlocale(LC_CTYPE,"");
#endif
QString result = bind_textdomain_codeset(domain.toUtf8().constData()
, "UTF-8");
Q_ASSERT(result=="UTF-8");
bindtextdomain (domain.toUtf8().constData(), QFile::encodeName(moDir
ectory).constData());
textdomain (domain.toUtf8().constData());
StelTranslator::lastUsed = this;
}
//! Convert from ISO639-1 2(+3) letters langage code to native language nam e //! Convert from ISO639-1 2(+3) letters langage code to native language nam e
QString StelTranslator::iso639_1CodeToNativeName(const QString& languageCod e) QString StelTranslator::iso639_1CodeToNativeName(const QString& languageCod e)
{ {
if (languageCode=="C") if (languageCode=="C")
return "English"; return "English";
//QLocale loc(languageCode); //QLocale loc(languageCode);
//QString l = loc.name(); //QString l = loc.name();
// There is a QLocale for this code. This should be the case for mo st // There is a QLocale for this code. This should be the case for mo st
// language codes, but there are a few without QLocales, e.g. Interl ingua // language codes, but there are a few without QLocales, e.g. Interl ingua
skipping to change at line 187 skipping to change at line 129
if (iso639codes.contains(languageCode)) if (iso639codes.contains(languageCode))
return iso639codes[languageCode]; return iso639codes[languageCode];
// qWarning() << "WARNING: Cannot determine name of language for cod e" << languageCode; // qWarning() << "WARNING: Cannot determine name of language for cod e" << languageCode;
return languageCode; return languageCode;
} }
//! Convert from native language name to ISO639-1 2(+3) letters langage cod e //! Convert from native language name to ISO639-1 2(+3) letters langage cod e
QString StelTranslator::nativeNameToIso639_1Code(const QString& languageNam e) QString StelTranslator::nativeNameToIso639_1Code(const QString& languageNam e)
{ {
QMap<QString, QString>::iterator iter; QMap<QString, QString>::ConstIterator iter;
for (iter=iso639codes.begin();iter!=iso639codes.end();++iter) for (iter=iso639codes.begin();iter!=iso639codes.end();++iter)
if (iter.value() == languageName) if (iter.value() == languageName)
return iter.key(); return iter.key();
return languageName; return languageName;
} }
//! Get available native language names from directory tree //! Get available native language names from directory tree
QStringList StelTranslator::getAvailableLanguagesNamesNative(const QString& localeDir) const QStringList StelTranslator::getAvailableLanguagesNamesNative(const QString& localeDir) const
{ {
QString tmpDir = localeDir; QString tmpDir = localeDir;
if (tmpDir.isEmpty())
tmpDir = moDirectory;
QStringList codeList = getAvailableIso639_1Codes(tmpDir); QStringList codeList = getAvailableIso639_1Codes(tmpDir);
QStringList output; QStringList output;
foreach (QString lang, codeList) foreach (const QString& lang, codeList)
{ {
output += iso639_1CodeToNativeName(lang); output += iso639_1CodeToNativeName(lang);
} }
return output; return output;
} }
//! Get available language codes from directory tree //! Get available language codes from directory tree
QStringList StelTranslator::getAvailableIso639_1Codes(const QString& locale Dir) const QStringList StelTranslator::getAvailableIso639_1Codes(const QString& locale Dir) const
{ {
QString locDir = localeDir; QDir dir(localeDir+"/stellarium/");
if (locDir.isEmpty())
locDir = moDirectory;
struct dirent *entryp;
DIR *dp;
QStringList result;
if ((dp = opendir(QFile::encodeName(locDir).constData())) == NULL) if (!dir.exists())
{ {
qWarning() << "Unable to find locale directory containing tr anslations:" << QDir::toNativeSeparators(localeDir); qWarning() << "Unable to find locale directory containing tr anslations:" << QDir::toNativeSeparators(localeDir);
return result; return QStringList();
} }
while ((entryp = readdir(dp)) != NULL) QStringList result;
foreach (QString path, dir.entryList(QDir::Files, QDir::Name))
{ {
QString tmp = entryp->d_name; if (!path.endsWith(".qm"))
QString tmpdir = locDir+"/"+tmp+"/LC_MESSAGES/stellarium.mo" continue;
; path.chop(3);
FILE* fic = fopen(QFile::encodeName(tmpdir).constData(), "r" result.append(path);
);
if (fic)
{
result.push_back(tmp);
fclose(fic);
}
} }
closedir(dp);
// Sort the language names by alphabetic order
result.sort();
return result; return result;
} }
//! 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
void StelTranslator::initIso639_1LanguageCodes(const QString& fileName) void StelTranslator::initIso639_1LanguageCodes(const QString& fileName)
{ {
QFile inf(fileName); QFile inf(fileName);
if (!inf.open(QIODevice::ReadOnly)) if (!inf.open(QIODevice::ReadOnly))
{ {
 End of changes. 19 change blocks. 
142 lines changed or deleted 62 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/