// localizedtexts.cpp
#include "common.h"
bool LocalizedTexts_Init (const wchar_t *fmt, ...)
{
// this function opens and parses the INI file containing the localized texts and fills the
// global text_t structure.
static wchar_t lngfile_pathname[MAX_PATH];
va_list argptr;
void *inifile;
text_t *text;
int text_index;
wchar_t *id_string;
wchar_t *filter_char;
// concatenate all the arguments in one string
va_start (argptr, fmt);
wvsprintf (lngfile_pathname, fmt, argptr);
va_end (argptr);
// open the INI file
inifile = INIFile_LoadINIFile (lngfile_pathname);
// consistency check
if (inifile == NULL)
return (false);
// read the number of known localized texts and mallocate enough space for them
text_count = INIFile_GetNumberOfEntries (inifile, L"texts");
texts = (text_t *) SAFE_malloc (text_count, sizeof (text_t), true);
// now read all of them
for (text_index = 0; text_index < text_count; text_index++)
{
text = &texts[text_index]; // quick access to text
id_string = INIFile_GetEntryName (inifile, L"texts", text_index); // read this entry
if (id_string == NULL)
{
MessageBox (NULL, L"There is an inconsistency in Chess Giants's language file.\nThe game cannot start.\n\nPlease reinstall this program to fix the problem.", L"Chess Giants", MB_ICONERROR | MB_OK);
return (false); // consistency check
}
text->id_string = _wcsdup (id_string); // have a copy of ID string and save it
text->localized_string = (wchar_t *) SAFE_malloc (65536, sizeof (wchar_t), false); // allocate big space for localized string...
wcscpy_s (text->localized_string, 65536, INIFile_ReadEntryAsString (inifile, L"texts", text->id_string, text->id_string)); // ... read it...
text->localized_string = (wchar_t *) SAFE_realloc (text->localized_string, 65536, wcslen (text->localized_string) + 1, sizeof (wchar_t), false); // and resize correctly
// conventionally, all texts whose ID end in "FileFilter" are file filters. Convert them for Windows API.
// Reminder: INI file key entries are LOWERCASE.
if (wcsistr (text->id_string, L"FileFilter") != NULL)
{
filter_char = text->localized_string; // convert vertical bars into string terminator characters
while (*filter_char != 0) { if (*filter_char == L'|') *filter_char = 0; filter_char++; }
}
}
// close the INI file
INIFile_FreeINIFile (inifile);
return (true); // finished loading texts
}
void LocalizedTexts_Shutdown (void)
{
// this function unloads all the contents of the localized texts array, freeing
// the associated memory space, and resets the text_count variable to zero.
int text_index;
// cycle through all the localized texts and free their mallocated space
for (text_index = 0; text_index < text_count; text_index++)
{
SAFE_free ((void **) &texts[text_index].id_string);
SAFE_free ((void **) &texts[text_index].localized_string);
}
text_count = 0; // reset the localized texts count
return; // finished unloading texts
}
wchar_t *LocalizedTexts_GetLocalizedTextFor (wchar_t *id_string)
{
// this function returns the localized text corresponding to a given id string
int text_index;
// cycle through all the localized texts and find the one we want
// NOTE: case-insensitive comparison mandatory, for dictionary entries are lowercase.
for (text_index = 0; text_index < text_count; text_index++)
if (_wcsicmp (texts[text_index].id_string, id_string) == 0)
return (texts[text_index].localized_string); // when found, return a pointer to it
Debug_Log (L"===WARNING: translation missing for string ID [%s]!===\n", id_string);
return (id_string); // when not found, return the ID string itself
}