diff options
Diffstat (limited to 'common/translation.cpp')
-rw-r--r-- | common/translation.cpp | 130 |
1 files changed, 34 insertions, 96 deletions
diff --git a/common/translation.cpp b/common/translation.cpp index a33e1a5243..69824dd299 100644 --- a/common/translation.cpp +++ b/common/translation.cpp @@ -31,17 +31,14 @@ #define TRANSLATIONS_DAT_VER 2 -#include "translation.h" +#include "common/translation.h" #include "common/archive.h" #include "common/config-manager.h" +#include "common/file.h" +#include "common/fs.h" +#include "common/system.h" -DECLARE_SINGLETON(Common::TranslationManager) - -#ifdef USE_DETECTLANG -#ifndef WIN32 -#include <locale.h> -#endif // !WIN32 -#endif +DECLARE_SINGLETON(Common::TranslationManager); namespace Common { @@ -56,66 +53,7 @@ bool operator<(const TLanguage &l, const TLanguage &r) { TranslationManager::TranslationManager() : _currentLang(-1) { loadTranslationsInfoDat(); -#ifdef USE_DETECTLANG -#ifdef WIN32 - // We can not use "setlocale" (at least not for MSVC builds), since it - // will return locales like: "English_USA.1252", thus we need a special - // way to determine the locale string for Win32. - char langName[9]; - char ctryName[9]; - - const LCID languageIdentifier = GetThreadLocale(); - - // GetLocalInfo is only supported starting from Windows 2000, according to this: - // http://msdn.microsoft.com/en-us/library/dd318101%28VS.85%29.aspx - // On the other hand the locale constants used, seem to exist on Windows 98 too, - // check this for that: http://msdn.microsoft.com/en-us/library/dd464799%28v=VS.85%29.aspx - // - // I am not exactly sure what is the truth now, it might be very well that this breaks - // support for systems older than Windows 2000.... - // - // TODO: Check whether this (or ScummVM at all ;-) works on a system with Windows 98 for - // example and if it does not and we still want Windows 9x support, we should definitly - // think of another solution. - if (GetLocaleInfo(languageIdentifier, LOCALE_SISO639LANGNAME, langName, sizeof(langName)) != 0 && - GetLocaleInfo(languageIdentifier, LOCALE_SISO3166CTRYNAME, ctryName, sizeof(ctryName)) != 0) { - _syslang = langName; - _syslang += "_"; - _syslang += ctryName; - } else { - _syslang = "C"; - } -#else // WIN32 - // Activating current locale settings - const char *locale = setlocale(LC_ALL, ""); - - // Detect the language from the locale - if (!locale) { - _syslang = "C"; - } else { - int length = 0; - - // Strip out additional information, like - // ".UTF-8" or the like. We do this, since - // our translation languages are usually - // specified without any charset information. - for (int i = 0; locale[i]; ++i) { - // TODO: Check whether "@" should really be checked - // here. - if (locale[i] == '.' || locale[i] == ' ' || locale[i] == '@') { - length = i; - break; - } - - length = i; - } - - _syslang = String(locale, length); - } -#endif // WIN32 -#else // USE_DETECTLANG - _syslang = "C"; -#endif // USE_DETECTLANG + _syslang = g_system->getSystemLanguage(); // Set the default language setLanguage(""); @@ -124,7 +62,7 @@ TranslationManager::TranslationManager() : _currentLang(-1) { TranslationManager::~TranslationManager() { } -void TranslationManager::setLanguage(const char *lang) { +void TranslationManager::setLanguage(const String &lang) { // Get lang index int langIndex = -1; String langStr(lang); @@ -151,11 +89,11 @@ void TranslationManager::setLanguage(const char *lang) { } } -const char *TranslationManager::getTranslation(const char *message) { +const char *TranslationManager::getTranslation(const char *message) const { return getTranslation(message, NULL); } -const char *TranslationManager::getTranslation(const char *message, const char *context) { +const char *TranslationManager::getTranslation(const char *message, const char *context) const { // if no language is set or message is empty, return msgid as is if (_currentTranslationMessages.empty() || *message == '\0') return message; @@ -207,23 +145,23 @@ const char *TranslationManager::getTranslation(const char *message, const char * return message; } -const char *TranslationManager::getCurrentCharset() { +String TranslationManager::getCurrentCharset() const { if (_currentCharset.empty()) return "ASCII"; - return _currentCharset.c_str(); + return _currentCharset; } -const char *TranslationManager::getCurrentLanguage() { +String TranslationManager::getCurrentLanguage() const { if (_currentLang == -1) return "C"; - return _langs[_currentLang].c_str(); + return _langs[_currentLang]; } -String TranslationManager::getTranslation(const String &message) { +String TranslationManager::getTranslation(const String &message) const { return getTranslation(message.c_str()); } -String TranslationManager::getTranslation(const String &message, const String &context) { +String TranslationManager::getTranslation(const String &message, const String &context) const { return getTranslation(message.c_str(), context.c_str()); } @@ -240,7 +178,7 @@ const TLangArray TranslationManager::getSupportedLanguageNames() const { return languages; } -int TranslationManager::parseLanguage(const String lang) { +int TranslationManager::parseLanguage(const String &lang) const { for (unsigned int i = 0; i < _langs.size(); i++) { if (lang == _langs[i]) return i + 1; @@ -249,7 +187,7 @@ int TranslationManager::parseLanguage(const String lang) { return kTranslationBuiltinId; } -const char *TranslationManager::getLangById(int id) { +String TranslationManager::getLangById(int id) const { switch (id) { case kTranslationAutodetectId: return ""; @@ -257,7 +195,7 @@ const char *TranslationManager::getLangById(int id) { return "C"; default: if (id >= 0 && id - 1 < (int)_langs.size()) - return _langs[id - 1].c_str(); + return _langs[id - 1]; } // In case an invalid ID was specified, we will output a warning @@ -335,10 +273,10 @@ void TranslationManager::loadTranslationsInfoDat() { for (int i = 0; i < nbTranslations; ++i) { len = in.readUint16BE(); in.read(buf, len); - _langs[i] = String(buf, len); + _langs[i] = String(buf, len-1); len = in.readUint16BE(); in.read(buf, len); - _langNames[i] = String(buf, len); + _langNames[i] = String(buf, len-1); } // Read messages @@ -347,7 +285,7 @@ void TranslationManager::loadTranslationsInfoDat() { for (int i = 0; i < numMessages; ++i) { len = in.readUint16BE(); in.read(buf, len); - _messageIds[i] = String(buf, len); + _messageIds[i] = String(buf, len-1); } } @@ -395,18 +333,18 @@ void TranslationManager::loadLanguageDat(int index) { // Read charset len = in.readUint16BE(); in.read(buf, len); - _currentCharset = String(buf, len); + _currentCharset = String(buf, len-1); // Read messages for (int i = 0; i < nbMessages; ++i) { _currentTranslationMessages[i].msgid = in.readUint16BE(); len = in.readUint16BE(); in.read(buf, len); - _currentTranslationMessages[i].msgstr = String(buf, len); + _currentTranslationMessages[i].msgstr = String(buf, len-1); len = in.readUint16BE(); if (len > 0) { in.read(buf, len); - _currentTranslationMessages[i].msgctxt = String(buf, len); + _currentTranslationMessages[i].msgctxt = String(buf, len-1); } } } @@ -444,29 +382,29 @@ TranslationManager::TranslationManager() {} TranslationManager::~TranslationManager() {} -void TranslationManager::setLanguage(const char *lang) {} +void TranslationManager::setLanguage(const String &lang) {} -const char *TranslationManager::getLangById(int id) { - return ""; +String TranslationManager::getLangById(int id) const { + return String(); } -int TranslationManager::parseLanguage(const String lang) { +int TranslationManager::parseLanguage(const String &lang) const { return kTranslationBuiltinId; } -const char *TranslationManager::getTranslation(const char *message) { +const char *TranslationManager::getTranslation(const char *message) const { return message; } -String TranslationManager::getTranslation(const String &message) { +String TranslationManager::getTranslation(const String &message) const { return message; } -const char *TranslationManager::getTranslation(const char *message, const char *) { +const char *TranslationManager::getTranslation(const char *message, const char *) const { return message; } -String TranslationManager::getTranslation(const String &message, const String &) { +String TranslationManager::getTranslation(const String &message, const String &) const { return message; } @@ -474,11 +412,11 @@ const TLangArray TranslationManager::getSupportedLanguageNames() const { return TLangArray(); } -const char *TranslationManager::getCurrentCharset() { +String TranslationManager::getCurrentCharset() const { return "ASCII"; } -const char *TranslationManager::getCurrentLanguage() { +String TranslationManager::getCurrentLanguage() const { return "C"; } |