(svn r21163) -Codechange: pass a LanguageMetadata struct instead of its index to ReadLanguagePack, and simplify one of its callers

This commit is contained in:
rubidium 2010-11-13 11:38:01 +00:00
parent a664227c8e
commit 68b94e61e0
4 changed files with 22 additions and 19 deletions

View File

@ -59,4 +59,6 @@ struct LanguageMetadata : public LanguagePackHeader {
/** The currently loaded language. */ /** The currently loaded language. */
extern const LanguageMetadata *_current_language; extern const LanguageMetadata *_current_language;
bool ReadLanguagePack(const LanguageMetadata *lang);
#endif /* LANGUAGE_H */ #endif /* LANGUAGE_H */

View File

@ -418,7 +418,7 @@ struct GameOptionsWindow : Window {
break; break;
case GOW_LANG_DROPDOWN: // Change interface language case GOW_LANG_DROPDOWN: // Change interface language
ReadLanguagePack(index); ReadLanguagePack(&_dynlang.ent[index]);
CheckForMissingGlyphsInLoadedLanguagePack(); CheckForMissingGlyphsInLoadedLanguagePack();
UpdateAllVirtCoords(); UpdateAllVirtCoords();
ReInitAllWindows(); ReInitAllWindows();

View File

@ -1323,11 +1323,11 @@ bool LanguagePackHeader::IsValid() const
StrValid(this->digit_decimal_separator, lastof(this->digit_decimal_separator)); StrValid(this->digit_decimal_separator, lastof(this->digit_decimal_separator));
} }
bool ReadLanguagePack(int lang_index) bool ReadLanguagePack(const LanguageMetadata *lang)
{ {
/* Current language pack */ /* Current language pack */
size_t len; size_t len;
LanguagePack *lang_pack = (LanguagePack *)ReadFileToMem(_dynlang.ent[lang_index].file, &len, 200000); LanguagePack *lang_pack = (LanguagePack *)ReadFileToMem(lang->file, &len, 200000);
if (lang_pack == NULL) return false; if (lang_pack == NULL) return false;
/* End of read data (+ terminating zero added in ReadFileToMem()) */ /* End of read data (+ terminating zero added in ReadFileToMem()) */
@ -1385,7 +1385,7 @@ bool ReadLanguagePack(int lang_index)
free(_langpack_offs); free(_langpack_offs);
_langpack_offs = langpack_offs; _langpack_offs = langpack_offs;
_current_language = &_dynlang.ent[lang_index]; _current_language = lang;
_current_text_dir = (TextDirection)_current_language->text_dir; _current_text_dir = (TextDirection)_current_language->text_dir;
const char *c_file = strrchr(_current_language->file, PATHSEPCHAR) + 1; const char *c_file = strrchr(_current_language->file, PATHSEPCHAR) + 1;
strecpy(_config_language_file, c_file, lastof(_config_language_file)); strecpy(_config_language_file, c_file, lastof(_config_language_file));
@ -1538,33 +1538,35 @@ void InitializeLanguagePacks()
const char *lang = GetCurrentLocale("LC_MESSAGES"); const char *lang = GetCurrentLocale("LC_MESSAGES");
if (lang == NULL) lang = "en_GB"; if (lang == NULL) lang = "en_GB";
int chosen_language = -1; ///< Matching the language in the configuartion file or the current locale const LanguageMetadata *chosen_language = NULL; ///< Matching the language in the configuartion file or the current locale
int language_fallback = -1; ///< Using pt_PT for pt_BR locale when pt_BR is not available const LanguageMetadata *language_fallback = NULL; ///< Using pt_PT for pt_BR locale when pt_BR is not available
int en_GB_fallback = 0; ///< Fallback when no locale-matching language has been found const LanguageMetadata *en_GB_fallback = _dynlang.ent; ///< Fallback when no locale-matching language has been found
_dynlang.num = language_count; _dynlang.num = language_count;
/* Fill the dynamic languages structures */ /* Fill the dynamic languages structures */
for (uint i = 0; i < language_count; i++) { for (uint i = 0; i < language_count; i++) {
const LanguageMetadata *lng = &_dynlang.ent[i];
/* We are trying to find a default language. The priority is by /* We are trying to find a default language. The priority is by
* configuration file, local environment and last, if nothing found, * configuration file, local environment and last, if nothing found,
* english. If def equals -1, we have not picked a default language */ * english. */
const char *lang_file = strrchr(_dynlang.ent[i].file, PATHSEPCHAR) + 1; const char *lang_file = strrchr(lang, PATHSEPCHAR) + 1;
if (strcmp(lang_file, _config_language_file) == 0) chosen_language = i; if (strcmp(lang_file, _config_language_file) == 0) {
chosen_language = lng;
if (chosen_language == -1) { break;
if (strcmp (_dynlang.ent[i].isocode, "en_GB") == 0) en_GB_fallback = i;
if (strncmp(_dynlang.ent[i].isocode, lang, 5) == 0) chosen_language = i;
if (strncmp(_dynlang.ent[i].isocode, lang, 2) == 0) language_fallback = i;
} }
if (strcmp (lng->isocode, "en_GB") == 0) en_GB_fallback = lng;
if (strncmp(lng->isocode, lang, 5) == 0) chosen_language = lng;
if (strncmp(lng->isocode, lang, 2) == 0) language_fallback = lng;
} }
/* We haven't found the language in the config nor the one in the locale. /* We haven't found the language in the config nor the one in the locale.
* Now we set it to one of the fallback languages */ * Now we set it to one of the fallback languages */
if (chosen_language == -1) { if (chosen_language == NULL) {
chosen_language = (language_fallback != -1) ? language_fallback : en_GB_fallback; chosen_language = (language_fallback != NULL) ? language_fallback : en_GB_fallback;
} }
if (!ReadLanguagePack(chosen_language)) usererror("Can't read language pack '%s'", _dynlang.ent[chosen_language].file); if (!ReadLanguagePack(chosen_language)) usererror("Can't read language pack '%s'", chosen_language->file);
} }
/** /**

View File

@ -98,7 +98,6 @@ static inline void CopyOutDParam(uint64 *dst, int offs, int num)
extern DynamicLanguages _dynlang; // defined in strings.cpp extern DynamicLanguages _dynlang; // defined in strings.cpp
extern TextDirection _current_text_dir; ///< Text direction of the currently selected language extern TextDirection _current_text_dir; ///< Text direction of the currently selected language
bool ReadLanguagePack(int index);
void InitializeLanguagePacks(); void InitializeLanguagePacks();
const char *GetCurrentLanguageIsoCode(); const char *GetCurrentLanguageIsoCode();