i18n: Style fixes.

svn-id: r52257
This commit is contained in:
Johannes Schickel 2010-08-21 13:21:09 +00:00
parent d986a78834
commit a53d4dde66
2 changed files with 42 additions and 46 deletions

View file

@ -43,14 +43,14 @@ DECLARE_SINGLETON(Common::TranslationManager)
namespace Common { namespace Common {
bool operator<(const TLanguage& l1, const TLanguage& l2) { bool operator<(const TLanguage &l, const TLanguage &r) {
return strcmp(l1.name, l2.name) < 0; return strcmp(l.name, r.name) < 0;
} }
#ifdef USE_TRANSLATION #ifdef USE_TRANSLATION
// Translation enabled // Translation enabled
TranslationManager::TranslationManager() : _currentLang(-1) { TranslationManager::TranslationManager() : _currentLang(-1) {
loadTranslationsInfoDat(); loadTranslationsInfoDat();
@ -128,19 +128,19 @@ void TranslationManager::setLanguage(const char *lang) {
String langStr(lang); String langStr(lang);
if (langStr.empty()) if (langStr.empty())
langStr = _syslang; langStr = _syslang;
// Searching for a valid language // Searching for a valid language
for (unsigned int i = 0; i < _langs.size() && langIndex == -1; ++i) { for (unsigned int i = 0; i < _langs.size() && langIndex == -1; ++i) {
if (langStr == _langs[i]) if (langStr == _langs[i])
langIndex = i; langIndex = i;
} }
// Try partial match // Try partial match
for (unsigned int i = 0; i < _langs.size() && langIndex == -1; ++i) { for (unsigned int i = 0; i < _langs.size() && langIndex == -1; ++i) {
if (strncmp(langStr.c_str(), _langs[i].c_str(), 2) == 0) if (strncmp(langStr.c_str(), _langs[i].c_str(), 2) == 0)
langIndex = i; langIndex = i;
} }
// Load messages for that lang // Load messages for that lang
// Call it even if the index is -1 to unload previously loaded translations // Call it even if the index is -1 to unload previously loaded translations
if (langIndex != _currentLang) { if (langIndex != _currentLang) {
@ -153,11 +153,11 @@ const char *TranslationManager::getTranslation(const char *message) {
// if no language is set or message is empty, return msgid as is // if no language is set or message is empty, return msgid as is
if (_currentTranslationMessages.empty() || *message == '\0') if (_currentTranslationMessages.empty() || *message == '\0')
return message; return message;
// binary-search for the msgid // binary-search for the msgid
int leftIndex = 0; int leftIndex = 0;
int rightIndex = _currentTranslationMessages.size() - 1; int rightIndex = _currentTranslationMessages.size() - 1;
while (rightIndex >= leftIndex) { while (rightIndex >= leftIndex) {
const int midIndex = (leftIndex + rightIndex) / 2; const int midIndex = (leftIndex + rightIndex) / 2;
const PoMessageEntry * const m = &_currentTranslationMessages[midIndex]; const PoMessageEntry * const m = &_currentTranslationMessages[midIndex];
@ -171,7 +171,7 @@ const char *TranslationManager::getTranslation(const char *message) {
else else
leftIndex = midIndex + 1; leftIndex = midIndex + 1;
} }
return message; return message;
} }
@ -225,22 +225,22 @@ const char *TranslationManager::getLangById(int id) {
} }
void TranslationManager::loadTranslationsInfoDat() { void TranslationManager::loadTranslationsInfoDat() {
File in; File in;
in.open("translations.dat"); in.open("translations.dat");
if (!checkHeader(in)) if (!checkHeader(in))
return; return;
char buf[256]; char buf[256];
int len; int len;
// Get number of translations // Get number of translations
int nbTranslations = in.readUint16BE(); int nbTranslations = in.readUint16BE();
// Skip all the block sizes // Skip all the block sizes
for (int i = 0 ; i < nbTranslations + 2 ; ++i) for (int i = 0 ; i < nbTranslations + 2 ; ++i)
in.readUint16BE(); in.readUint16BE();
// Read list of languages // Read list of languages
_langs.resize(nbTranslations); _langs.resize(nbTranslations);
_langNames.resize(nbTranslations); _langNames.resize(nbTranslations);
@ -252,7 +252,7 @@ void TranslationManager::loadTranslationsInfoDat() {
in.read(buf, len); in.read(buf, len);
_langNames[i] = String(buf, len); _langNames[i] = String(buf, len);
} }
// Read messages // Read messages
int numMessages = in.readUint16BE(); int numMessages = in.readUint16BE();
_messageIds.resize(numMessages); _messageIds.resize(numMessages);
@ -272,42 +272,42 @@ void TranslationManager::loadLanguageDat(int index) {
warning("Invalid language index %d passed to TranslationManager::loadLanguageDat", index); warning("Invalid language index %d passed to TranslationManager::loadLanguageDat", index);
return; return;
} }
File in; File in;
in.open("translations.dat"); in.open("translations.dat");
if (!checkHeader(in)) if (!checkHeader(in))
return; return;
char buf[1024]; char buf[1024];
int len; int len;
// Get number of translations // Get number of translations
int nbTranslations = in.readUint16BE(); int nbTranslations = in.readUint16BE();
if (nbTranslations != (int)_langs.size()) { if (nbTranslations != (int)_langs.size()) {
warning("The 'translations.dat' file has changed since starting ScummVM. GUI translation will not be available"); warning("The 'translations.dat' file has changed since starting ScummVM. GUI translation will not be available");
return; return;
} }
// Get size of blocks to skip. // Get size of blocks to skip.
int skipSize = 0; int skipSize = 0;
for (int i = 0 ; i < index + 2 ; ++i) for (int i = 0 ; i < index + 2 ; ++i)
skipSize += in.readUint16BE(); skipSize += in.readUint16BE();
// We also need to skip the remaining block sizes // We also need to skip the remaining block sizes
skipSize += 2 * (nbTranslations - index); skipSize += 2 * (nbTranslations - index);
// Seek to start of block we want to read // Seek to start of block we want to read
in.seek(skipSize, SEEK_CUR); in.seek(skipSize, SEEK_CUR);
// Read number of translated messages // Read number of translated messages
int nbMessages = in.readUint16BE(); int nbMessages = in.readUint16BE();
_currentTranslationMessages.resize(nbMessages); _currentTranslationMessages.resize(nbMessages);
// Read charset // Read charset
len = in.readUint16BE(); len = in.readUint16BE();
in.read(buf, len); in.read(buf, len);
_currentCharset = String(buf, len); _currentCharset = String(buf, len);
// Read messages // Read messages
for (int i = 0; i < nbMessages; ++i) { for (int i = 0; i < nbMessages; ++i) {
_currentTranslationMessages[i].msgid = in.readUint16BE(); _currentTranslationMessages[i].msgid = in.readUint16BE();
@ -320,29 +320,29 @@ void TranslationManager::loadLanguageDat(int index) {
bool TranslationManager::checkHeader(File& in) { bool TranslationManager::checkHeader(File& in) {
char buf[13]; char buf[13];
int ver; int ver;
if (!in.isOpen()) { if (!in.isOpen()) {
warning("You're missing the 'translations.dat' file. GUI translation will not be available"); warning("You're missing the 'translations.dat' file. GUI translation will not be available");
return false; return false;
} }
in.read(buf, 12); in.read(buf, 12);
buf[12] = '\0'; buf[12] = '\0';
// Check header // Check header
if (strcmp(buf, "TRANSLATIONS")) { if (strcmp(buf, "TRANSLATIONS")) {
warning("File 'translations.dat' is corrupt. GUI translation will not be available"); warning("File 'translations.dat' is corrupt. GUI translation will not be available");
return false; return false;
} }
// Check version // Check version
ver = in.readByte(); ver = in.readByte();
if (ver != TRANSLATIONS_DAT_VER) { if (ver != TRANSLATIONS_DAT_VER) {
warning("File 'translations.dat' is wrong version. Expected %d but got %d. GUI translation will not be available", TRANSLATIONS_DAT_VER, ver); warning("File 'translations.dat' is wrong version. Expected %d but got %d. GUI translation will not be available", TRANSLATIONS_DAT_VER, ver);
return false; return false;
} }
return true; return true;
} }
@ -383,4 +383,5 @@ const char *TranslationManager::getCurrentCharset() {
#endif // USE_TRANSLATION #endif // USE_TRANSLATION
} // End of namespace Common } // End of namespace Common

View file

@ -40,26 +40,19 @@ struct TLanguage {
const char *name; const char *name;
int id; int id;
TLanguage() { TLanguage() : name(0), id(0) {}
name = 0; TLanguage(const char *n, int i) : name(n), id(i) {}
id = 0;
}
TLanguage(const char *n, int i) {
name = n;
id = i;
}
}; };
bool operator<(const TLanguage&, const TLanguage&); bool operator<(const TLanguage &l, const TLanguage &r);
typedef Array<TLanguage> TLangArray; typedef Array<TLanguage> TLangArray;
struct PoMessageEntry { struct PoMessageEntry {
int msgid; int msgid;
String msgstr; String msgstr;
}; };
/** /**
* Message translation manager. * Message translation manager.
*/ */
@ -140,21 +133,23 @@ private:
* Load the list of languages from the translations.dat file * Load the list of languages from the translations.dat file
*/ */
void loadTranslationsInfoDat(); void loadTranslationsInfoDat();
/** /**
* Load the translation for the given language from the translations.dat file * Load the translation for the given language from the translations.dat file
* *
* @param index of the language in the list of languages * @param index of the language in the list of languages
*/ */
void loadLanguageDat(int); void loadLanguageDat(int);
/** /**
* Check the header of the given file to make sure it is a valid translations data file. * Check the header of the given file to make sure it is a valid translations data file.
*/ */
bool checkHeader(File&); bool checkHeader(File&);
String _syslang; String _syslang;
StringArray _langs; StringArray _langs;
StringArray _langNames; StringArray _langNames;
StringArray _messageIds; StringArray _messageIds;
Array<PoMessageEntry> _currentTranslationMessages; Array<PoMessageEntry> _currentTranslationMessages;
String _currentCharset; String _currentCharset;