Subversion Repositories Games.Chess Giants

Rev

Rev 56 | Rev 136 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 56 Rev 59
Line 1... Line 1...
1
// localizedtexts.cpp
1
// localizedtexts.cpp
2
 
2
 
3
#include "common.h"
3
#include "common.h"
4
 
4
 
5
 
5
 
6
bool LocalizedTexts_Init (const wchar_t *fmt, ...)
6
void LocalizedTexts_Init (void)
7
{
7
{
8
   // this function opens and parses the INI file containing the localized texts and fills the
8
   // this function opens and parses the INI files containing the localized texts and fills the
9
   // global text_t structure.
9
   // global language_t structure.
10
 
10
 
-
 
11
   static wchar_t lngfile_pattern[MAX_PATH];
11
   static wchar_t lngfile_pathname[MAX_PATH];
12
   static wchar_t lngfile_pathname[MAX_PATH];
12
   va_list argptr;
-
 
13
   void *inifile;
13
   void *inifile;
14
   text_t *text;
14
   text_t *text;
15
   int text_index;
15
   int text_index;
16
   wchar_t *id_string;
16
   wchar_t *id_string;
17
   wchar_t *filter_char;
17
   wchar_t *filter_char;
-
 
18
   WIN32_FIND_DATA wfd;
-
 
19
   HANDLE hFind;
18
 
20
 
-
 
21
   language_count = 0;
-
 
22
   swprintf_s (lngfile_pattern, sizeof (lngfile_pattern), L"%s\\data\\languages\\*.ini", app_path); // build the search pattern string out of the path
19
   // concatenate all the arguments in one string
23
   hFind = FindFirstFile (lngfile_pattern, &wfd); // initiate search from that point
20
   va_start (argptr, fmt);
24
   if (hFind != INVALID_HANDLE_VALUE)
-
 
25
   {
21
   wvsprintf (lngfile_pathname, fmt, argptr);
26
      // start examining search results...
-
 
27
      do
-
 
28
      {
22
   va_end (argptr);
29
         // open the INI file
-
 
30
         swprintf_s (lngfile_pathname, WCHAR_SIZEOF (lngfile_pathname), L"%s\\data\\languages\\%s", app_path, wfd.cFileName);
-
 
31
         inifile = INIFile_LoadINIFile (lngfile_pathname);
23
 
32
 
-
 
33
         // consistency check (FIXME: this should never happen)
24
   // open the INI file
34
         if (inifile == NULL)
25
   inifile = INIFile_LoadINIFile (lngfile_pathname);
35
            continue;
26
 
36
 
27
   // consistency check
37
         // file successfully opened, reallocate languages structure to hold one language more
28
   if (inifile == NULL)
-
 
29
      return (false);
38
         languages = (language_t *) SAFE_realloc (languages, language_count, language_count + 1, sizeof (language_t), false);
30
 
39
 
31
   // read the number of known localized texts and mallocate enough space for them
40
         // read the number of known localized texts and mallocate enough space for them
32
   text_count = INIFile_GetNumberOfEntries (inifile, L"texts");
41
         languages[language_count].text_count = INIFile_GetNumberOfEntries (inifile, L"texts");
33
   texts = (text_t *) SAFE_malloc (text_count, sizeof (text_t), true);
42
         languages[language_count].texts = (text_t *) SAFE_malloc (languages[language_count].text_count, sizeof (text_t), true);
34
 
43
 
35
   // now read all of them
44
         // now read all of them
36
   for (text_index = 0; text_index < text_count; text_index++)
45
         for (text_index = 0; text_index < languages[language_count].text_count; text_index++)
37
   {
46
         {
38
      text = &texts[text_index]; // quick access to text
47
            text = &languages[language_count].texts[text_index]; // quick access to text
39
 
48
 
40
      id_string = INIFile_GetEntryName (inifile, L"texts", text_index); // read this entry
49
            id_string = INIFile_GetEntryName (inifile, L"texts", text_index); // read this entry
41
      if (id_string == NULL)
50
            if (id_string == NULL)
42
      {
51
            {
43
         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);
52
               MessageBox (NULL, L"There is an inconsistency in Chess Giants's language file.\n\nPlease reinstall the program to fix the problem.", L"Chess Giants", MB_ICONERROR | MB_OK);
44
         return (false); // consistency check
53
               continue; // consistency check
45
      }
54
            }
46
 
55
 
47
      text->id_string = _wcsdup (id_string); // have a copy of ID string and save it
56
            text->id_string = _wcsdup (id_string); // have a copy of ID string and save it
48
      text->localized_string = (wchar_t *) SAFE_malloc (65536, sizeof (wchar_t), false); // allocate big space for localized string...
57
            text->localized_string = (wchar_t *) SAFE_malloc (65536, sizeof (wchar_t), false); // allocate big space for localized string...
49
      wcscpy_s (text->localized_string, 65536, INIFile_ReadEntryAsString (inifile, L"texts", text->id_string, text->id_string)); // ... read it...
58
            wcscpy_s (text->localized_string, 65536, INIFile_ReadEntryAsString (inifile, L"texts", text->id_string, text->id_string)); // ... read it...
50
      text->localized_string = (wchar_t *) SAFE_realloc (text->localized_string, 65536, wcslen (text->localized_string) + 1, sizeof (wchar_t), false); // and resize correctly
59
            text->localized_string = (wchar_t *) SAFE_realloc (text->localized_string, 65536, wcslen (text->localized_string) + 1, sizeof (wchar_t), false); // and resize correctly
51
 
60
 
52
      // conventionally, all texts whose ID end in "FileFilter" are file filters. Convert them for Windows API.
61
            // conventionally, all texts whose ID end in "FileFilter" are file filters. Convert them for Windows API.
53
      // Reminder: INI file key entries are LOWERCASE.
62
            // Reminder: INI file key entries are LOWERCASE.
54
      if (wcsistr (text->id_string, L"FileFilter") != NULL)
63
            if (wcsistr (text->id_string, L"FileFilter") != NULL)
55
      {
64
            {
56
         filter_char = text->localized_string; // convert vertical bars into string terminator characters
65
               filter_char = text->localized_string; // convert vertical bars into string terminator characters
57
         while (*filter_char != 0) { if (*filter_char == L'|') *filter_char = 0; filter_char++; }
66
               while (*filter_char != 0) { if (*filter_char == L'|') *filter_char = 0; filter_char++; }
-
 
67
            }
58
      }
68
         }
-
 
69
 
-
 
70
         // close the INI file
-
 
71
         INIFile_FreeINIFile (inifile);
-
 
72
 
-
 
73
         // save the language name (and chop off the trailing ".ini" suffix)
-
 
74
         wcscpy_s (languages[language_count].name, WCHAR_SIZEOF (languages[language_count].name), wfd.cFileName);
-
 
75
         languages[language_count].name[wcslen (languages[language_count].name) - 4] = 0;
-
 
76
 
-
 
77
         language_count++; // we've identified one language more
-
 
78
      } while (FindNextFile (hFind, &wfd)); // ...and don't stop as long as there are files to go
-
 
79
 
-
 
80
      FindClose (hFind); // close the search handle
59
   }
81
   }
60
 
82
 
61
   // close the INI file
-
 
62
   INIFile_FreeINIFile (inifile);
-
 
63
   return (true); // finished loading texts
83
   return; // finished loading texts
64
}
84
}
65
 
85
 
66
 
86
 
67
void LocalizedTexts_Shutdown (void)
87
void LocalizedTexts_Shutdown (void)
68
{
88
{
69
   // this function unloads all the contents of the localized texts array, freeing
89
   // this function unloads all the contents of the localized texts array, freeing
70
   // the associated memory space, and resets the text_count variable to zero.
90
   // the associated memory space, and resets the text_count variable to zero.
71
 
91
 
-
 
92
   int language_index;
72
   int text_index;
93
   int text_index;
73
 
94
 
74
   // cycle through all the localized texts and free their mallocated space
95
   // cycle through all the localized texts and free their mallocated space
75
   for (text_index = 0; text_index < text_count; text_index++)
96
   for (language_index = 0; language_index < language_count; language_index++)
76
   {
97
   {
-
 
98
      for (text_index = 0; text_index < languages[language_index].text_count; text_index++)
-
 
99
      {
77
      SAFE_free ((void **) &texts[text_index].id_string);
100
         SAFE_free ((void **) &languages[language_index].texts[text_index].id_string);
78
      SAFE_free ((void **) &texts[text_index].localized_string);
101
         SAFE_free ((void **) &languages[language_index].texts[text_index].localized_string);
-
 
102
      }
-
 
103
      SAFE_free ((void **) &languages[language_index].texts); // free the texts array themselves
-
 
104
      languages[language_index].text_count = 0; // reset the localized texts count
79
   }
105
   }
-
 
106
   SAFE_free ((void **) &languages); // free the languages array themselves
-
 
107
   language_count = 0; // and reset the languages count
80
 
108
 
81
   text_count = 0; // reset the localized texts count
-
 
82
   return; // finished unloading texts
109
   return; // finished unloading texts
83
}
110
}
84
 
111
 
85
 
112
 
86
wchar_t *LocalizedTexts_GetLocalizedTextFor (wchar_t *id_string)
113
wchar_t *LocalizedTexts_GetLocalizedTextFor (wchar_t *id_string)
87
{
114
{
88
   // this function returns the localized text corresponding to a given id string
115
   // this function returns the localized text corresponding to a given id string
89
 
116
 
-
 
117
   language_t *language;
90
   int text_index;
118
   int text_index;
-
 
119
 
-
 
120
   language = &languages[language_id]; // quick access to language
91
 
121
 
92
   // cycle through all the localized texts and find the one we want
122
   // cycle through all the localized texts and find the one we want
93
   // NOTE: case-insensitive comparison mandatory, for dictionary entries are lowercase.
123
   // NOTE: case-insensitive comparison mandatory, for dictionary entries are lowercase.
94
   for (text_index = 0; text_index < text_count; text_index++)
124
   for (text_index = 0; text_index < language->text_count; text_index++)
95
      if (_wcsicmp (texts[text_index].id_string, id_string) == 0)
125
      if (_wcsicmp (language->texts[text_index].id_string, id_string) == 0)
96
         return (texts[text_index].localized_string); // when found, return a pointer to it
126
         return (language->texts[text_index].localized_string); // when found, return a pointer to it
97
 
127
 
98
   Debug_Log (L"===WARNING: translation missing for string ID [%s]!===\n", id_string);
128
   Debug_Log (L"===WARNING: translation missing for string ID [%s]!===\n", id_string);
99
   return (id_string); // when not found, return the ID string itself
129
   return (id_string); // when not found, return the ID string itself
100
}
130
}