Rev 11 | Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | pmbaty | 1 | // localizedtexts.cpp |
| 2 | |||
| 3 | #include "common.h" |
||
| 4 | |||
| 5 | |||
| 6 | bool LocalizedTexts_Init (const wchar_t *lngfile_pathname) |
||
| 7 | { |
||
| 8 | // this function opens and parses the INI file containing the localized texts and fills the |
||
| 9 | // global text_t structure. |
||
| 10 | |||
| 11 | wchar_t filename[MAX_PATH]; |
||
| 12 | void *inifile; |
||
| 13 | text_t *text; |
||
| 14 | int text_index; |
||
| 15 | wchar_t *id_string; |
||
| 16 | wchar_t *filter_char; |
||
| 17 | |||
| 18 | // open the INI file |
||
| 19 | swprintf_s (filename, WCHAR_SIZEOF (filename), L"%s\\%s", app_path, lngfile_pathname); |
||
| 20 | inifile = INIFile_LoadINIFile (filename); |
||
| 21 | |||
| 22 | // consistency check |
||
| 23 | if (inifile == NULL) |
||
| 24 | { |
||
| 25 | MessageBox (NULL, L"Chess Giants was unable to load its data files.\nThe game cannot start.\n\nPlease reinstall this program to fix the problem.", L"Chess Giants", MB_ICONERROR | MB_OK); |
||
| 26 | return (false); |
||
| 27 | } |
||
| 28 | |||
| 29 | // read the number of known localized texts and mallocate enough space for them |
||
| 30 | text_count = INIFile_GetNumberOfEntries (inifile, L"texts"); |
||
| 31 | texts = (text_t *) SAFE_malloc (text_count, sizeof (text_t), true); |
||
| 32 | |||
| 33 | // now read all of them |
||
| 34 | for (text_index = 0; text_index < text_count; text_index++) |
||
| 35 | { |
||
| 36 | text = &texts[text_index]; // quick access to text |
||
| 37 | |||
| 38 | id_string = INIFile_GetEntryName (inifile, L"texts", text_index); // read this entry |
||
| 39 | if (id_string == NULL) |
||
| 40 | { |
||
| 41 | 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); |
||
| 42 | return (false); // consistency check |
||
| 43 | } |
||
| 44 | |||
| 45 | text->id_string = _wcsdup (id_string); // have a copy of ID string and save it |
||
| 46 | text->localized_string = (wchar_t *) SAFE_malloc (65536, sizeof (wchar_t), false); // allocate big space for localized string... |
||
| 47 | wcscpy_s (text->localized_string, 65536, INIFile_ReadEntryAsString (inifile, L"texts", text->id_string, text->id_string)); // ... read it... |
||
| 48 | text->localized_string = (wchar_t *) SAFE_realloc (text->localized_string, 65536, wcslen (text->localized_string) + 1, sizeof (wchar_t), false); // and resize correctly |
||
| 49 | |||
| 50 | // conventionally, all texts whose ID end in "FileFilter" are file filters. Convert them for Windows API. |
||
| 51 | if (wcsstr (text->id_string, L"FileFilter") != NULL) |
||
| 52 | { |
||
| 53 | filter_char = text->localized_string; // convert vertical bars into string terminator characters |
||
| 54 | while (*filter_char != 0) { if (*filter_char == L'|') *filter_char = 0; filter_char++; } |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | // close the INI file |
||
| 59 | INIFile_FreeINIFile (inifile); |
||
| 60 | return (true); // finished loading texts |
||
| 61 | } |
||
| 62 | |||
| 63 | |||
| 64 | void LocalizedTexts_Shutdown (void) |
||
| 65 | { |
||
| 66 | // this function unloads all the contents of the localized texts array, freeing |
||
| 67 | // the associated memory space, and resets the text_count variable to zero. |
||
| 68 | |||
| 69 | int text_index; |
||
| 70 | |||
| 71 | // cycle through all the localized texts and free their mallocated space |
||
| 72 | for (text_index = 0; text_index < text_count; text_index++) |
||
| 73 | { |
||
| 74 | SAFE_free ((void **) &texts[text_index].id_string); |
||
| 75 | SAFE_free ((void **) &texts[text_index].localized_string); |
||
| 76 | } |
||
| 77 | |||
| 78 | text_count = 0; // reset the localized texts count |
||
| 79 | return; // finished unloading texts |
||
| 80 | } |
||
| 81 | |||
| 82 | |||
| 83 | wchar_t *LocalizedTexts_GetLocalizedTextFor (wchar_t *id_string) |
||
| 84 | { |
||
| 85 | // this function returns the localized text corresponding to a given id string |
||
| 86 | |||
| 87 | int text_index; |
||
| 88 | |||
| 89 | // cycle through all the localized texts and find the one we want |
||
| 90 | // NOTE: case-insensitive comparison mandatory, for dictionary entries are lowercase. |
||
| 91 | for (text_index = 0; text_index < text_count; text_index++) |
||
| 92 | if (_wcsicmp (texts[text_index].id_string, id_string) == 0) |
||
| 93 | return (texts[text_index].localized_string); // when found, return a pointer to it |
||
| 94 | |||
| 95 | Debug_Log (L"===WARNING: translation missing for string ID [%s]!===\n", id_string); |
||
| 96 | return (id_string); // when not found, return the ID string itself |
||
| 97 | } |