2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Localization/localize.cpp $
17 * Revision 1.9 2005/10/01 21:35:51 taylor
18 * various FS1 related changes, removes need to extra fs1.vp file
20 * Revision 1.8 2005/03/29 02:18:47 taylor
21 * Various 64-bit platform fixes
22 * Fix compiler errors with MAKE_FS1 and fix gr_set_bitmap() too
23 * Make sure that turrets can fire at asteroids for FS1 (needed for a couple missions)
24 * Streaming audio support (big thanks to Pierre Willenbrock!!)
25 * Removed dependance on strings.tbl for FS1 since we don't actually need it now
27 * Revision 1.7 2004/07/04 11:39:06 taylor
28 * fix missing debrief text, crash on exit, path separator's, warning fixes, no GR_SOFT
30 * Revision 1.6 2003/06/11 18:30:32 taylor
33 * Revision 1.5 2003/06/03 04:00:40 taylor
34 * Polish language support (Janusz Dziemidowicz)
36 * Revision 1.4 2003/05/18 03:55:30 taylor
37 * automatic language selection support
39 * Revision 1.3 2002/06/09 04:41:22 relnev
40 * added copyright header
42 * Revision 1.2 2002/05/07 03:16:46 theoddone33
43 * The Great Newline Fix
45 * Revision 1.1.1.1 2002/05/03 03:28:09 root
49 * 91 6/16/00 3:16p Jefff
50 * sim of the year dvd version changes, a few german soty localization
53 * 90 11/02/99 8:20p Jefff
54 * more translation changes/additions
56 * 89 11/02/99 3:24p Jefff
57 * added translation functions for a few key instances where english was
60 * 88 10/29/99 10:40p Jefff
63 * 87 10/28/99 2:05a Jefff
64 * safety fix in localization stuff
66 * 86 10/26/99 10:53a Jefff
67 * German builds now correctly default to German on first run
69 * 85 10/25/99 11:22p Jefff
70 * mo' strings (launcher)
72 * 84 10/25/99 5:46p Jefff
73 * Many localization fixes/changes for German builds
75 * 83 10/14/99 3:25p Jefff
76 * soo...many....xstrs....
78 * 82 10/14/99 2:52p Jefff
79 * localization fixes. added support for hi-res specific xstr offsets
81 * 81 10/13/99 3:58p Jefff
82 * added a bunch of missed XSTRs
84 * 80 9/13/99 11:30a Dave
85 * Added checkboxes and functionality for disabling PXO banners as well as
86 * disabling d3d zbuffer biasing.
88 * 79 9/08/99 9:59a Jefff
90 * 78 9/07/99 4:01p Dave
91 * Fixed up a string.tbl paroblem (self destruct message). Make sure IPX
92 * does everything properly (setting up address when binding). Remove
93 * black rectangle background from UI_INPUTBOX.
95 * 77 9/07/99 1:54p Jefff
96 * skip mission strings added
98 * 76 9/03/99 10:55a Jefff
101 * 75 9/03/99 1:31a Dave
102 * CD checking by act. Added support to play 2 cutscenes in a row
103 * seamlessly. Fixed super low level cfile bug related to files in the
104 * root directory of a CD. Added cheat code to set campaign mission # in
107 * 74 8/27/99 12:04a Dave
108 * Campaign loop screen.
110 * 73 8/26/99 12:48p Jefff
112 * 72 8/22/99 5:53p Dave
113 * Scoring fixes. Added self destruct key. Put callsigns in the logfile
114 * instead of ship designations for multiplayer players.
116 * 71 8/22/99 1:55p Dave
117 * Cleaned up host/team-captain leaving code.
119 * 70 8/17/99 7:32p Jefff
122 * 69 8/16/99 5:15p Dave
123 * Upped string count.
125 * 68 8/16/99 4:04p Dave
126 * Big honking checkin.
128 * 67 8/11/99 2:09p Jefff
129 * ill give you three guesses.
131 * 66 8/10/99 7:57p Jefff
134 * 65 8/05/99 2:40p Jefff
137 * 64 8/02/99 9:13p Dave
140 * 63 8/02/99 12:01p Jefff
143 * 62 7/29/99 10:47p Dave
144 * Standardized D3D fogging using vertex fog. Shook out Savage 4 bugs.
146 * 61 7/29/99 9:44a Jefff
147 * added some strings for ingame objectives screen
149 * 60 7/27/99 7:15p Jefff
150 * 3 new interface strings
152 * 59 7/27/99 6:53p Dave
153 * Hi-res main hall support.
155 * 58 7/24/99 1:54p Dave
156 * Hud text flash gauge. Reworked dead popup to use 4 buttons in red-alert
159 * 57 7/22/99 4:08p Jefff
162 * 56 7/19/99 7:20p Dave
163 * Beam tooling. Specialized player-killed-self messages. Fixed d3d nebula
166 * 55 7/19/99 2:13p Dave
167 * Added some new strings for Heiko.
169 * 54 7/15/99 2:37p Jefff
171 * 53 7/13/99 6:07p Jefff
172 * Added support for localization string offsets.
174 * 52 7/09/99 10:32p Dave
175 * Command brief and red alert screens.
177 * 51 6/25/99 2:51p Jasons
180 * 50 6/25/99 11:59a Dave
181 * Multi options screen.
183 * 49 6/24/99 12:34a Dave
184 * Main options screen.
186 * 48 6/19/99 3:58p Dave
189 * 47 6/14/99 5:19p Dave
191 * 46 6/04/99 11:32a Dave
192 * Added reset text to ship select screen. Fixed minor xstr bug in ui
195 * 45 6/01/99 6:07p Dave
196 * New loading/pause/please wait bar.
198 * 44 6/01/99 4:03p Dave
199 * Changed some popup flag #defines. Upped string count to 1336
201 * 43 5/26/99 11:46a Dave
202 * Added ship-blasting lighting and made the randomization of lighting
203 * much more customizable.
205 * 42 5/22/99 6:05p Dave
206 * Fixed a few localization # problems.
208 * 41 5/21/99 6:45p Dave
209 * Sped up ui loading a bit. Sped up localization disk access stuff. Multi
210 * start game screen, multi password, and multi pxo-help screen.
212 * 40 5/17/99 2:52p Dave
213 * Fixed dumb localization problem.
215 * 39 5/17/99 10:07a Dave
216 * Updated string count.
218 * 38 5/04/99 6:38p Dave
219 * Finished multi join-wait screen.
221 * 37 5/04/99 5:20p Dave
222 * Fixed up multiplayer join screen and host options screen. Should both
225 * 36 5/03/99 11:04p Dave
226 * Most of the way done with the multi join screen.
228 * 35 5/03/99 8:32p Dave
229 * New version of multi host options screen.
231 * 34 4/09/99 2:28p Dave
232 * Upped Xstring count.
234 * 33 4/09/99 2:21p Dave
235 * Multiplayer beta stuff. CD checking.
237 * 32 4/08/99 12:50p Neilk
238 * changed number of xstr constant
240 * 31 4/07/99 5:54p Dave
241 * Fixes for encryption in updatelauncher.
243 * 30 3/24/99 4:05p Dave
244 * Put in support for assigning the player to a specific squadron with a
245 * specific logo. Preliminary work for doing pos/orient checksumming in
246 * multiplayer to reduce bandwidth.
248 * 29 3/23/99 3:35p Andsager
249 * Add error check for duplicate string indices
251 * 28 3/22/99 2:56p Andsager
252 * Localize Launcher Updater
254 * 27 2/23/99 11:18a Andsager
255 * Localize launcher using strings.tbl
257 * 26 2/22/99 9:35p Andsager
258 * Add lcl_get_language_name() returns string with current lang. Added
259 * localization for launcher.
261 * 25 2/21/99 6:02p Dave
262 * Fixed standalone WSS packets.
264 * 24 2/17/99 2:11p Dave
265 * First full run of squad war. All freespace and tracker side stuff
268 * 23 2/12/99 6:15p Dave
269 * Pre-mission Squad War code is 95% done.
271 * 22 2/02/99 7:27p Neilk
272 * implemented lc_close() to free up some string memory when shutting down
274 * 21 1/30/99 1:29a Dave
275 * Fixed nebula thumbnail problem. Full support for 1024x768 choose pilot
276 * screen. Fixed beam weapon death messages.
278 * 20 1/29/99 7:56p Neilk
279 * Added new mission log interface
281 * 19 1/29/99 4:17p Dave
282 * New interface screens.
284 * 18 1/29/99 12:47a Dave
285 * Put in sounds for beam weapon. A bunch of interface screens (tech
288 * 17 1/13/99 2:10p Andsager
291 * 16 12/14/98 12:13p Dave
292 * Spiffed up xfer system a bit. Put in support for squad logo file xfer.
295 * 15 12/03/98 5:22p Dave
296 * Ported over Freespace 1 multiplayer ships.tbl and weapons.tbl
299 * 14 12/02/98 5:47p Dave
300 * Put in interface xstr code. Converted barracks screen to new format.
302 * 13 12/01/98 4:46p Dave
303 * Put in targa bitmap support (16 bit).
311 #include "localize.h"
313 #include "osregistry.h"
317 // ------------------------------------------------------------------------------------------------------------
318 // LOCALIZE DEFINES/VARS
321 // general language/localization data ---------------------
324 int Lcl_current_lang = LCL_ENGLISH;
326 // language info table
327 typedef struct lang_info {
328 char lang_name[LCL_LANG_NAME_LEN + 1]; // literal name of the language
329 char lang_ext[LCL_LANG_NAME_LEN + 1]; // for adding to names on disk access
332 lang_info Lcl_languages[LCL_NUM_LANGUAGES] = {
333 { "English", "" }, // english
334 { "German", "gr" }, // german
335 { "French", "fr" }, // french
336 { "Polish", "pl" }, // polish
339 //#if defined(GERMAN_BUILD)
340 //#define DEFAULT_LANGUAGE "German"
342 #define DEFAULT_LANGUAGE "English"
345 // following is the offset where special characters start in our font
346 #define LCL_SPECIAL_CHARS_FR 164
347 #define LCL_SPECIAL_CHARS_GR 164
348 #define LCL_SPECIAL_CHARS_PL 127
349 #define LCL_SPECIAL_CHARS 127
350 int Lcl_special_chars;
352 // use these to replace *_BUILD, values before
353 // only 1 will be active at a time
360 // executable string localization data --------------------
362 // XSTR_SIZE is the total count of unique XSTR index numbers. An index is used to
363 // reference an entry in strings.tbl. This is used for translation of strings from
364 // the english version (in the code) to a foreign version (in the table). Thus, if you
365 // add a new string to the code, you must assign it a new index. Use the number below for
366 // that index and increase the number below by one.
367 #define XSTR_SIZE 1570
370 // struct to allow for strings.tbl-determined x offset
371 // offset is 0 for english, by default
374 int offset_x; // string offset in 640
375 int offset_x_hi; // string offset in 1024
378 //char *Xstr_table[XSTR_SIZE];
379 lcl_xstr Xstr_table[XSTR_SIZE];
383 // table/mission externalization stuff --------------------
385 #define TABLE_STRING_FILENAME "tstrings.tbl"
386 // filename of the file to use when localizing table strings
387 char *Lcl_ext_filename = NULL;
388 CFILE *Lcl_ext_file = NULL;
390 // for scanning/parsing tstrings.tbl (from ExStr)
391 #define PARSE_TEXT_STRING_LEN 2048
392 #define PARSE_ID_STRING_LEN 128
393 #define TS_SCANNING 0 // scanning for a line of text
394 #define TS_ID_STRING 1 // reading in an id string
395 #define TS_OPEN_QUOTE 2 // looking for an open quote
396 #define TS_STRING 3 // reading in the text string itself
397 int Ts_current_state = 0;
398 char Ts_text[PARSE_TEXT_STRING_LEN]; // string we're currently working with
399 char Ts_id_text[PARSE_ID_STRING_LEN]; // id string we're currently working with
403 // file pointers for optimized string lookups
404 // some example times for Freespace2 startup with granularities (mostly .tbl files, ~500 strings in the table file, many looked up more than once)
405 // granularity 20 : 13 secs
406 // granularity 10 : 11 secs
407 // granularity 5 : 9 secs
408 // granularity 2 : 7-8 secs
409 #define LCL_GRANULARITY 1 // how many strings between each pointer (lower granularities should give faster lookup times)
410 #define LCL_MAX_POINTERS 4500 // max # of pointers
411 #define LCL_MAX_STRINGS (LCL_GRANULARITY * LCL_MAX_POINTERS)
412 int Lcl_pointers[LCL_MAX_POINTERS];
413 int Lcl_pointer_count = 0;
416 // strings.tbl for FS1, hard coded
417 extern char *FS1_trans[LCL_NUM_LANGUAGES_FS1][LCL_NUM_STRINGS_FS1];
420 // ------------------------------------------------------------------------------------------------------------
421 // LOCALIZE FORWARD DECLARATIONS
424 // associate table file externalization with the specified input file
425 void lcl_ext_associate(char *filename);
427 // given a valid XSTR() tag piece of text, extract the string portion, return it in out, nonzero on success
428 int lcl_ext_get_text(char *xstr, char *out);
430 // given a valid XSTR() tag piece of text, extract the id# portion, return the value in out, nonzero on success
431 int lcl_ext_get_id(char *xstr, int *out);
433 // given a valid XSTR() id#, lookup the string in tstrings.tbl, filling in out if found, nonzero on success
434 int lcl_ext_lookup(char *out, int id);
436 // if the char is a valid char for a signed integer value string
437 int lcl_is_valid_numeric_char(char c);
439 // sub-parse function for individual lines of tstrings.tbl (from Exstr)
440 // returns : integer with the low bits having the following values :
441 // 0 on fail, 1 on success, 2 if found a matching id/string pair, 3 if end of language has been found
442 // for cases 1 and 2 : the high bit (1<<31) will be set if the parser detected the beginning of a new string id on this line
443 // so be sure to mask this value out to get the low portion of the return value
445 int lcl_ext_lookup_sub(char *text, char *out, int id);
447 // initialize the pointer array into tstrings.tbl (call from lcl_ext_open() ONLY)
448 void lcl_ext_setup_pointers();
451 // ------------------------------------------------------------------------------------------------------------
452 // LOCALIZE FUNCTIONS
455 // initialize localization, if no language is passed - use the language specified in the registry
456 void lcl_init(int lang_init)
458 char lang_string[128];
462 // initialize encryption
465 // read the language from the registry
467 memset(lang_string, 0, 128);
469 // default to DEFAULT_LANGUAGE (which should be english so we dont have to put German text
470 // in tstrings in the #default section
471 ret = os_config_read_string(NULL, "Language", DEFAULT_LANGUAGE);
475 strcpy(lang_string, DEFAULT_LANGUAGE);
477 strcpy(lang_string, ret);
482 for(idx=0; idx<LCL_NUM_LANGUAGES; idx++){
483 if(!stricmp(Lcl_languages[idx].lang_name, lang_string)){
492 Assert((lang_init >= 0) && (lang_init < LCL_NUM_LANGUAGES));
497 if (lang >= LCL_NUM_LANGUAGES_FS1)
502 Lcl_pointer_count = 0;
504 // associate the table string file
505 lcl_ext_associate(TABLE_STRING_FILENAME);
507 // set the language (this function takes care of setting up file pointers)
508 lcl_set_language(lang);
511 // added 2.2.99 by NeilK to take care of fs2 launcher memory leaks
512 // shutdown localization
515 // if the filename exists, free it up
516 if(Lcl_ext_filename != NULL){
517 free(Lcl_ext_filename);
520 // free the Xstr_table
524 // determine what language we're running in, see LCL_* defines above
525 int lcl_get_language()
527 return Lcl_current_lang;
530 // initialize the xstr table
536 char language_tag[512];
538 char *p_offset = NULL;
539 int offset_lo = 0, offset_hi = 0;
541 for (i=0; i<XSTR_SIZE; i++){
542 Xstr_table[i].str = NULL;
545 if ((rval = setjmp(parse_abort)) != 0) {
546 mprintf(("Error parsing 'strings.tbl'\nError code = %i.\n", rval));
548 // make sure localization is NOT running
551 read_file_text("strings.tbl");
554 // move down to the proper section
555 memset(language_tag, 0, 512);
556 strcpy(language_tag, "#");
557 strcat(language_tag, Lcl_languages[Lcl_current_lang].lang_name);
558 if(skip_to_string(language_tag) != 1){
559 Error(LOCATION, NOX("Strings.tbl is corrupt"));
562 // parse all the strings in this section of the table
563 while (!check_for_string("#")) {
564 int num_offsets_on_this_line = 0;
567 stuff_string(buf, F_NAME, NULL, 4096);
575 if (!isspace(buf[i])) {
580 // trim unneccesary end of string
582 // Assert(buf[i] == '"');
584 // probably an offset on this entry
585 buf[i+1] = 0; // drop down a null terminator (prolly unnecessary)
586 while(!is_white_space(buf[i])) i--; // back up over the potential offset
587 while(is_white_space(buf[i])) i--; // now back up over intervening spaces
588 num_offsets_on_this_line = 1;
590 // could have a 2nd offset value (one for 640, one for 1024)
592 while(!is_white_space(buf[i])) i--; // back up over the 2nd offset
593 while(is_white_space(buf[i])) i--; // now back up over intervening spaces
594 num_offsets_on_this_line = 2;
597 p_offset = &buf[i+1]; // get ptr to string section with offset in it
599 Error(LOCATION, NOX("Strings.tbl is corrupt")); // now its an error
606 // copy string into buf
608 for (i=1; buf[i]; i++) {
614 } else if (chr == 'r') {
623 // null terminator on buf
626 // write into Xstr_table
627 if (index >= 0 && index < XSTR_SIZE) {
628 if (Xstr_table[index].str != NULL) {
629 Warning(LOCATION, "Strings table index %d used more than once", index);
631 Xstr_table[index].str = strdup(buf);
634 // read offset information, assume 0 if nonexistant
635 if (p_offset != NULL) {
636 if (sscanf(p_offset, "%d%d", &offset_lo, &offset_hi) < num_offsets_on_this_line) {
637 // whatever is in the file ain't a proper offset
638 Error(LOCATION, NOX("Strings.tbl is corrupt"));
641 Xstr_table[index].offset_x = offset_lo;
642 if (num_offsets_on_this_line == 2) {
643 Xstr_table[index].offset_x_hi = offset_hi;
645 Xstr_table[index].offset_x_hi = offset_lo;
648 // clear out our vars
652 num_offsets_on_this_line = 0;
658 Assert(XSTR_SIZE == LCL_NUM_STRINGS_FS1);
659 Assert(Lcl_current_lang < LCL_NUM_LANGUAGES_FS1);
661 for (i=0; i<XSTR_SIZE; i++) {
662 if ( !strlen(FS1_trans[Lcl_current_lang][i]) ) {
663 Xstr_table[i].str = NULL;
665 Xstr_table[i].str = FS1_trans[Lcl_current_lang][i];
668 Xstr_table[i].offset_x = 0;
669 Xstr_table[i].offset_x_hi = 0;
678 void lcl_xstr_close()
681 for (int i=0; i<XSTR_SIZE; i++){
682 if (Xstr_table[i].str != NULL) {
683 free(Xstr_table[i].str);
684 Xstr_table[i].str = NULL;
691 // set our current language
692 void lcl_set_language(int lang)
694 Lcl_current_lang = lang;
696 nprintf(("General", "Setting language to %s\n", Lcl_languages[lang].lang_name));
698 // flag the proper language as being active
706 Lcl_special_chars = LCL_SPECIAL_CHARS;
710 Lcl_special_chars = LCL_SPECIAL_CHARS_FR;
714 Lcl_special_chars = LCL_SPECIAL_CHARS_GR;
718 Lcl_special_chars = LCL_SPECIAL_CHARS_PL;
722 // set to 0, so lcl_ext_open() knows to reset file pointers
723 Lcl_pointer_count = 0;
725 // reset file pointers to the proper language-section
726 if(Lcl_current_lang != LCL_DEFAULT_LANGUAGE){
727 lcl_ext_setup_pointers();
731 // maybe add on an appropriate subdirectory when opening a localized file
732 void lcl_add_dir(char *current_path)
737 // if the disk extension is 0 length, don't add enything
738 if (strlen(Lcl_languages[Lcl_current_lang].lang_ext) <= 0) {
742 // get the length of the string so far
743 path_len = strlen(current_path);
748 // get the current last char
749 last_char = current_path[path_len - 1];
751 // if the last char is a slash, just copy in the disk extension
752 if (last_char == DIR_SEPARATOR_CHAR) {
753 strcat(current_path, Lcl_languages[Lcl_current_lang].lang_ext);
754 strcat(current_path, DIR_SEPARATOR_STR);
756 // otherwise add a slash, then copy in the disk extension
758 strcat(current_path, DIR_SEPARATOR_STR);
759 strcat(current_path, Lcl_languages[Lcl_current_lang].lang_ext);
763 // maybe add localized directory to full path with file name when opening a localized file
764 void lcl_add_dir_to_path_with_filename(char *current_path)
766 char temp[MAX_PATH_LEN];
768 // if the disk extension is 0 length, don't add enything
769 if (strlen(Lcl_languages[Lcl_current_lang].lang_ext) <= 0) {
773 // find position of last slash and copy rest of filename (not counting slash) to temp
774 // mark end of current path with '\0', so strcat will work
775 char *last_slash = strrchr(current_path, DIR_SEPARATOR_CHAR);
776 if (last_slash == NULL) {
777 strcpy(temp, current_path);
778 current_path[0] = '\0';
780 strcpy(temp, last_slash+1);
781 last_slash[1] = '\0';
785 strcat(current_path, Lcl_languages[Lcl_current_lang].lang_ext);
786 strcat(current_path, DIR_SEPARATOR_STR);
788 // copy rest of filename from temp
789 strcat(current_path, temp);
793 // externalization of table/mission files -----------------------
795 // open the externalization file for use during parsing (call before parsing a given file)
798 // if the file is already open, do nothing
799 Assert(Lcl_ext_file == NULL);
801 // if we're running in the default language, do nothing
802 if(Lcl_current_lang == LCL_DEFAULT_LANGUAGE){
807 // otherwise open the file
808 Lcl_ext_file = cfopen(Lcl_ext_filename, "rt");
809 if(Lcl_ext_file == NULL){
815 // close the externalization file (call after parsing a given file)
818 // if the file is not open, do nothing
819 if(Lcl_ext_file == NULL){
823 // if we're running in the default language, do nothing
824 if(Lcl_current_lang == LCL_DEFAULT_LANGUAGE){
828 // otherwise close it
829 cfclose(Lcl_ext_file);
833 // get the localized version of the string. if none exists, return the original string
834 // valid input to this function includes :
835 // "this is some text"
836 // XSTR("wheeee", -1)
838 // and these should cover all the externalized string cases
839 // fills in id if non-NULL. a value of -2 indicates it is not an external string
840 void lcl_ext_localize(char *in, char *out, int max_len, int *id)
843 char text_str[2048]="";
844 char lookup_str[2048]="";
851 // default (non-external string) value
856 str_len = strlen(in);
858 // if the string is < 9 chars, it can't be an XSTR("",) tag, so just copy it
860 if(str_len > max_len){
861 error_display(0, "Token too long: [%s]. Length = %i. Max is %i.\n", in, str_len, max_len);
871 // otherwise, check to see if it's an XSTR() tag
872 memset(first_four, 0, 5);
873 strncpy(first_four, in, 4);
874 if(stricmp(first_four, "XSTR")){
876 if(str_len > max_len){
877 error_display(0, "Token too long: [%s]. Length = %i. Max is %i.\n", in, str_len, max_len);
887 // at this point we _know_ its an XSTR() tag, so split off the strings and id sections
888 if(!lcl_ext_get_text(in, text_str)){
896 if(!lcl_ext_get_id(in, &str_id)){
904 // if the localization file is not open, or we're running in the default language, return the original string
905 if((Lcl_ext_file == NULL) || (str_id < 0) || (Lcl_current_lang == LCL_DEFAULT_LANGUAGE)){
906 strcpy(out, text_str);
913 // attempt to find the string
914 if(lcl_ext_lookup(lookup_str, str_id)){
915 // copy to the outgoing string
916 Assert(strlen(lookup_str) <= (unsigned int)(max_len - 1));
918 if (strlen(lookup_str) > (unsigned int)(max_len-1)) {
919 // be safe and truncate string to fit
920 strncpy(out, lookup_str, (size_t) (max_len-1));
921 out[max_len-1] = '\0'; // ensure null terminator, since strncpy(...) doesnt.
923 strcpy(out, lookup_str);
927 // otherwise use what we have - probably should Int3() or assert here
929 strcpy(out, text_str);
938 // translate the specified string based upon the current language
939 char *XSTR(char *str, int index)
946 if (index >= 0 && index < XSTR_SIZE) {
947 if (Xstr_table[index].str){
948 return Xstr_table[index].str; // return translation of string
952 // can't translate, return original english string
956 // retrieve the offset for a localized string
957 int lcl_get_xstr_offset(int index, int res)
960 return Xstr_table[index].offset_x;
962 return Xstr_table[index].offset_x_hi;
967 // ------------------------------------------------------------------------------------------------------------
968 // LOCALIZE FORWARD DEFINITIONS
971 // associate table file externalization with the specified input file
972 void lcl_ext_associate(char *filename)
975 // if the filename already exists, free it up
976 if(Lcl_ext_filename != NULL){
977 free(Lcl_ext_filename);
980 // set the new filename
981 Lcl_ext_filename = strdup(filename);
985 // given a valid XSTR() tag piece of text, extract the string portion, return it in out, nonzero on success
986 int lcl_ext_get_text(char *xstr, char *out)
988 int str_start, str_end;
992 Assert(xstr != NULL);
994 str_len = strlen(xstr);
996 // this is some crazy wack-ass code.
997 // look for the open quote
998 str_start = str_end = 0;
999 p = strstr(xstr, "\"");
1001 error_display(0, "Error parsing XSTR() tag %s\n", xstr);
1004 str_start = p - xstr + 1;
1006 // make sure we're not about to walk past the end of the string
1007 if((p - xstr) >= str_len){
1008 error_display(0, "Error parsing XSTR() tag %s\n", xstr);
1012 // look for the close quote
1013 p2 = strstr(p+1, "\"");
1015 error_display(0, "Error parsing XSTR() tag %s\n", xstr);
1018 str_end = p2 - xstr;
1021 // now that we know the boundaries of the actual string in the XSTR() tag. copy it
1022 memcpy(out, xstr + str_start, str_end - str_start);
1025 lcl_fix_polish(out);
1032 // given a valid XSTR() tag piece of text, extract the id# portion, return the value in out, nonzero on success
1033 int lcl_ext_get_id(char *xstr, int *out)
1038 Assert(xstr != NULL);
1039 Assert(out != NULL);
1041 str_len = strlen(xstr);
1043 // find the first quote
1044 p = strstr(xstr, "\"");
1046 error_display(0, "Error parsing id# in XSTR() tag %s\n", xstr);
1049 // make sure we're not about to walk off the end of the string
1050 if((p - xstr) >= str_len){
1051 error_display(0, "Error parsing id# in XSTR() tag %s\n", xstr);
1056 // continue searching until we find the close quote
1058 pnext = strstr(p, "\"");
1060 error_display(0, "Error parsing id# in XSTR() tag %s\n", xstr);
1064 // if the previous char is a \, we know its not the "end-of-string" quote
1065 if(*(pnext - 1) != '\\'){
1074 // search until we find a ,
1075 pnext = strstr(p, ",");
1077 error_display(0, "Error parsing id# in XSTR() tag %s\n", xstr);
1080 // make sure we're not about to walk off the end of the string
1081 if((pnext - xstr) >= str_len){
1082 error_display(0, "Error parsing id# in XSTR() tag %s\n", xstr);
1087 // now get the id string
1089 pnext = strtok(p, ")");
1091 error_display(0, "Error parsing id# in XSTR() tag %s\n", xstr);
1095 // get the value and we're done
1102 // given a valid XSTR() id#, lookup the string in tstrings.tbl, filling in out if found, nonzero on success
1103 int lcl_ext_lookup(char *out, int id)
1109 Assert(Lcl_pointer_count >= 0);
1110 Assert(Lcl_pointers[0] >= 0);
1111 Assert(Lcl_pointers[Lcl_pointer_count - 1] >= 0);
1112 Assert(Lcl_ext_file != NULL);
1115 // seek to the closest pointer <= the id# we're looking for
1116 pointer = id / LCL_GRANULARITY;
1117 cfseek(Lcl_ext_file, Lcl_pointers[pointer], CF_SEEK_SET);
1119 // reset parsing vars and go to town
1120 Ts_current_state = TS_SCANNING;
1121 Ts_id_text_size = 0;
1123 memset(Ts_text, 0, PARSE_TEXT_STRING_LEN);
1124 memset(Ts_id_text, 0, PARSE_ID_STRING_LEN);
1125 while((cftell(Lcl_ext_file) < Lcl_pointers[Lcl_pointer_count - 1]) && cfgets(text, 1024, Lcl_ext_file)){
1126 ret = lcl_ext_lookup_sub(text, out, id);
1128 // run the line parse function
1129 switch(ret & 0x0fffffff){
1132 Int3(); // should never get here - it means the string doens't exist in the table!!
1135 // success parsing the line - please continue
1139 // found a matching string/id pair
1143 // this is because tstrings.tbl reads in as ANSI for some reason
1144 // opening tstrings with "rb" mode didnt seem to help, so its now still "rt" like before
1145 lcl_fix_umlauts(out, LCL_TO_ASCII);
1149 // end of language found
1151 Int3(); // should never get here - it means the string doens't exist in the table!!
1156 Int3(); // should never get here - it means the string doens't exist in the table!!
1160 // sub-parse function for individual lines of tstrings.tbl (from Exstr)
1161 // returns : integer with the low bits having the following values :
1162 // 0 on fail, 1 on success, 2 if found a matching id/string pair, 3 if end of language has been found
1163 // for cases 1 and 2 : the high bit (1<<31) will be set if the parser detected the beginning of a new string id on this line
1165 int lcl_ext_lookup_sub(char *text, char *out, int id)
1167 char *front; // front of the line
1168 char *p; // current ptr
1169 int len = strlen(text);
1171 char text_copy[1024];
1173 int found_new_string_id = 0;
1179 // do something useful
1180 switch(Ts_current_state){
1181 // scanning for a line of text
1183 // if the first word is #end, we're done with the file altogether
1184 strcpy(text_copy, text);
1185 tok = strtok(text_copy, " \n");
1186 if((tok != NULL) && !stricmp(tok, "#end")){
1189 // if its a commented line, skip it
1190 else if((text[0] == ';') || (text[0] == ' ') || (text[0] == '\n')){
1193 // otherwise we should have an ID #, so stuff it and move to the proper state
1195 if(lcl_is_valid_numeric_char(*p)){
1196 memset(Ts_id_text, 0, PARSE_ID_STRING_LEN);
1197 Ts_id_text_size = 0;
1198 Ts_id_text[Ts_id_text_size++] = *p;
1199 Ts_current_state = TS_ID_STRING;
1201 found_new_string_id = 1;
1211 // scanning in an id string
1213 // if we have another valid char
1214 if(lcl_is_valid_numeric_char(*p)){
1215 Ts_id_text[Ts_id_text_size++] = *p;
1217 // if we found a comma, our id# is finished, look for the open quote
1219 Ts_current_state = TS_OPEN_QUOTE;
1227 // valid space or an open quote
1228 if((*p == ' ') || (*p == '\"')){
1230 memset(Ts_text, 0, PARSE_TEXT_STRING_LEN);
1232 Ts_current_state = TS_STRING;
1241 // if we have an end quote, we need to look for a comma
1242 if((*p == '\"') /*&& (Ts_text_size > 0)*/ && (Ts_text[Ts_text_size - 1] != '\\')){
1243 // we're now done - we have a string
1244 Ts_current_state = TS_SCANNING;
1246 // if the id#'s match, copy the string and return "string found"
1247 if((atoi(Ts_id_text) == id) && (out != NULL)){
1248 strcpy(out, Ts_text);
1250 return found_new_string_id ? (1<<1) | (1<<31) : (1<<1);
1253 // otherwise, just continue parsing
1254 return found_new_string_id ? (1<<0) | (1<<31) : (1<<0);
1256 // otherwise add to the string
1258 Ts_text[Ts_text_size++] = *p;
1263 // if we have a newline, return success, we're done with this line
1265 return found_new_string_id ? (1<<0) | (1<<31) : (1<<0);
1268 // next char in the line
1274 return found_new_string_id ? (1<<0) | (1<<31) : (1<<0);
1277 // if the char is a valid char for a signed integer value
1278 int lcl_is_valid_numeric_char(char c)
1280 return ( (c == '-') || (c == '0') || (c == '1') || (c == '2') || (c == '3') || (c == '4') ||
1281 (c == '5') || (c == '6') || (c == '7') || (c == '8') || (c == '9') ) ? 1 : 0;
1284 // initialize the pointer array into tstrings.tbl (call from lcl_ext_open() ONLY)
1285 void lcl_ext_setup_pointers()
1287 char language_string[128];
1292 int found_start = 0;
1294 // open the localization file
1296 if(Lcl_ext_file == NULL){
1297 error_display(0, "Error opening externalization file! File likely does not exist or could not be found");
1301 // seek to the currently active language
1302 memset(language_string, 0, 128);
1303 strcpy(language_string, "#");
1304 if(!stricmp(DEFAULT_LANGUAGE, Lcl_languages[Lcl_current_lang].lang_name)){
1305 strcat(language_string, "default");
1307 strcat(language_string, Lcl_languages[Lcl_current_lang].lang_name);
1309 memset(line, 0, 1024);
1311 // reset seek variables and begin
1312 Lcl_pointer_count = 0;
1313 while(cfgets(line, 1024, Lcl_ext_file)){
1314 tok = strtok(line, " \n");
1319 // if the language matches, we're good to start parsing strings
1320 if(!stricmp(language_string, tok)){
1326 // if we didn't find the language specified, error
1327 if(found_start <= 0){
1328 error_display(0, "Could not find specified langauge in tstrings.tbl!\n");
1334 while(cfgets(line, 1024, Lcl_ext_file)){
1335 ret = lcl_ext_lookup_sub(line, NULL, -1);
1338 switch(ret & 0x0fffffff){
1344 // end of language found
1346 // mark one final pointer
1347 Lcl_pointers[Lcl_pointer_count++] = cftell(Lcl_ext_file) - strlen(line) - 1;
1352 // the only other case we care about is the beginning of a new id#
1354 if((string_count % LCL_GRANULARITY) == 0){
1355 // mark the pointer down
1356 Lcl_pointers[Lcl_pointer_count++] = cftell(Lcl_ext_file) - strlen(line) - 1;
1358 // if we're out of pointer slots
1359 if(Lcl_pointer_count >= LCL_MAX_POINTERS){
1360 error_display(0, "Out of pointer for tstrings.tbl lookup. Please increment LCL_MAX_POINTERS in localize.cpp");
1365 // increment string count
1370 // should never get here. we should always be exiting through case 3 (end of language section) of the above switch
1376 void lcl_get_language_name(char *lang_name)
1378 Assert(LCL_NUM_LANGUAGES == 3);
1380 strcpy(lang_name, Lcl_languages[Lcl_current_lang].lang_name);
1383 // converts german umlauted chars from ASCII to ANSI
1384 // so they appear in the launcher
1385 // how friggin lame is this
1386 // pass in a null terminated string, foo!
1387 // returns ptr to string you sent in
1388 char* lcl_fix_umlauts(char *str, int which_way)
1392 if (which_way == LCL_TO_ANSI) {
1393 // moving to ANSI charset
1394 // run thru string and perform appropriate conversions
1395 while (str[i] != '\0') {
1422 // beta-lookin thing that means "ss"
1430 // moving to ASCII charset
1431 // run thru string and perform appropriate conversions
1432 while (str[i] != '\0') {
1459 // beta-lookin thing that means "ss"
1471 // convert some of the polish characters
1472 void lcl_fix_polish(char *str)
1475 if(*str == '\xA2') *str = '\xF3';
1476 else if(*str == '\x88') *str = '\xEA';
1480 // ------------------------------------------------------------------
1481 // lcl_translate_wep_name()
1483 // For displaying weapon names in german version
1484 // since we cant actually just change them outright.
1486 void lcl_translate_wep_name(char *name)
1488 if (!strcmp(name, "Morning Star")) {
1489 strcpy(name, "Morgenstern");
1490 } else if (!strcmp(name, "MorningStar")) {
1491 strcpy(name, "Morgenstern D");
1492 } else if (!strcmp(name, "UD-8 Kayser")) {
1493 strcpy(name, "Kayserstrahl");
1494 } else if (!strcmp(name, "UD-D Kayser")) {
1495 strcpy(name, "Kayserstrahl");
1499 // ------------------------------------------------------------------
1500 // lcl_translate_brief_icon_name()
1502 // For displaying ship names in german version
1503 // since we cant actually just change them outright.
1505 void lcl_translate_brief_icon_name(char *name)
1510 if (!stricmp(name, "Subspace Portal")) {
1511 strcpy(name, "Subraum Portal");
1513 } else if (!stricmp(name, "Alpha Wing")) {
1514 strcpy(name, "Alpha");
1516 } else if (!stricmp(name, "Beta Wing")) {
1517 strcpy(name, "Beta");
1519 } else if (!stricmp(name, "Zeta Wing")) {
1520 strcpy(name, "Zeta");
1522 } else if (!stricmp(name, "Capella Node")) {
1523 strcpy(name, "Capella");
1525 } else if (!stricmp(name, "Hostile")) {
1526 strcpy(name, "Gegner");
1528 } else if (!stricmp(name, "Hostile Craft")) {
1529 strcpy(name, "Gegner");
1531 } else if (!stricmp(name, "Rebel Wing")) {
1532 strcpy(name, "Rebellen");
1534 } else if (!stricmp(name, "Rebel Fleet")) {
1535 strcpy(name, "Rebellenflotte");
1537 } else if (!stricmp(name, "Sentry Gun")) {
1538 strcpy(name, "Gesch\x81tz");
1540 } else if (!stricmp(name, "Cargo")) {
1541 strcpy(name, "Fracht");
1543 } else if (!stricmp(name, "Knossos Device")) {
1544 strcpy(name, "Knossosger\x84t");
1546 } else if (!stricmp(name, "Support")) {
1547 strcpy(name, "Versorger");
1549 } else if (!stricmp(name, "Unknown")) {
1550 strcpy(name, "Unbekannt");
1552 } else if (!stricmp(name, "Instructor")) {
1553 strcpy(name, "Ausbilder");
1555 } else if (!stricmp(name, "Jump Node")) {
1556 strcpy(name, "Sprungknoten");
1558 } else if (!stricmp(name, "Escort")) {
1559 strcpy(name, "Geleitschutz");
1561 } else if (!stricmp(name, "Asteroid Field")) {
1562 strcpy(name, "Asteroidenfeld");
1564 } else if (!stricmp(name, "Enif Station")) {
1565 strcpy(name, "Station Enif");
1567 } else if (!stricmp(name, "Rally Point")) {
1568 strcpy(name, "Sammelpunkt");
1570 } else if ((pos = strstr(name, "Transport")) != NULL) {
1571 pos += 9; // strlen of "transport"
1572 strcpy(buf, "Transporter");
1576 } else if ((pos = strstr(name, "Jump Node")) != NULL) {
1577 pos += 9; // strlen of "jump node"
1578 strcpy(buf, "Sprungknoten");
1582 } else if (!stricmp(name, "Orion under repair")) {
1583 strcpy(name, "Orion wird repariert");
1585 // SOTY-specific ones below!
1587 } else if (!stricmp(name, "Wayfarer Station")) {
1588 strcpy(name, "Station Wayfarer");
1589 } else if (!stricmp(name, "Enemy")) {
1590 strcpy(name, "Gegner");
1591 } else if (!stricmp(name, "Supply Depot")) {
1592 strcpy(name, "Nachschubdepot");
1593 } else if (!stricmp(name, "Fighter Escort")) {
1594 strcpy(name, "Jagdschutz");
1595 } else if (!stricmp(name, "Shivans")) {
1596 strcpy(name, "Shivaner");
1597 } else if (!stricmp(name, "NTF Base of Operations")) {
1598 strcpy(name, "NTF-Operationsbasis");
1599 } else if (!stricmp(name, "NTF Bombers")) {
1600 strcpy(name, "NTF-Bomber");
1601 } else if (!stricmp(name, "NTF Fighters")) {
1602 strcpy(name, "NTF-J\x84ger");
1603 } else if (!stricmp(name, "Sentry")) {
1604 strcpy(name, "Sperrgesch\x81tz");
1605 } else if (!stricmp(name, "Cargo Containers")) {
1606 strcpy(name, "Frachtbeh\x84lter");
1607 } else if (!stricmp(name, "NTF Reinforcements")) {
1608 strcpy(name, "NTF-Verst\x84rkungen");
1609 } else if (!stricmp(name, "NTF Base")) {
1610 strcpy(name, "NTF-St\x81tzpunkt");
1611 } else if (!stricmp(name, "Refugee Convoy")) {
1612 strcpy(name, "Fl\x81""chtlingskonvoi");
1613 } else if (!stricmp(name, "Food Convoy")) {
1614 strcpy(name, "Nachschubkonvoi");
1615 } else if (!stricmp(name, "Governor's Shuttle")) {
1616 strcpy(name, "F\x84hre des Gouverneurs");
1617 } else if (!stricmp(name, "GTVA Patrol")) {
1618 strcpy(name, "GTVA-Patrouille");
1619 } else if (!stricmp(name, "Escort fighters")) {
1620 strcpy(name, "Geleitschutz");
1621 } else if (!stricmp(name, "Nagada Outpost")) {
1622 strcpy(name, "Nagada-Aussenposten");
1623 } else if (!stricmp(name, "Fighters")) {
1624 strcpy(name, "J\x84ger");
1625 } else if (!stricmp(name, "Bombers")) {
1626 strcpy(name, "Bomber");
1627 } else if (!stricmp(name, "Enemy Destroyers")) {
1628 strcpy(name, "Feindliche Zerst\x94rer");
1629 } else if (!stricmp(name, "Ross 128 System")) {
1630 strcpy(name, "System Ross 128");
1631 } else if (!stricmp(name, "Knossos Station")) {
1632 strcpy(name, "Knossos-Station");
1633 } else if (!stricmp(name, "Transporters")) {
1634 strcpy(name, "Transporter");
1635 } else if (!stricmp(name, "Pirates?")) {
1636 strcpy(name, "Piraten?");
1637 } else if (!stricmp(name, "Escorts")) {
1638 strcpy(name, "Geleitschutz");
1639 } else if (!stricmp(name, "Shivan Fighters")) {
1640 strcpy(name, "J\x84ger");
1641 } else if (!stricmp(name, "Shivan Territory")) {
1642 strcpy(name, "Shivaner");
1646 // ------------------------------------------------------------------
1647 // lcl_translate_brief_icon_name_pl()
1649 // For displaying ship names in polish version
1650 // since we cant actually just change them outright.
1652 void lcl_translate_brief_icon_name_pl(char *name)
1657 if (!stricmp(name, "Subspace Portal")) {
1658 strcpy(name, "Portal podprz.");
1660 } else if (!stricmp(name, "Alpha Wing")) {
1661 strcpy(name, "Alfa");
1663 } else if (!stricmp(name, "Beta Wing")) {
1664 strcpy(name, "Beta");
1666 } else if (!stricmp(name, "Zeta Wing")) {
1667 strcpy(name, "Zeta");
1669 } else if (!stricmp(name, "Capella Node")) {
1670 strcpy(name, "Capella");
1672 } else if (!stricmp(name, "Hostile")) {
1673 strcpy(name, "Wr\xF3g");
1675 } else if (!stricmp(name, "Hostile Craft")) {
1676 strcpy(name, "Wr\xF3g");
1678 } else if (!stricmp(name, "Rebel Wing")) {
1679 strcpy(name, "Rebelianci");
1681 } else if (!stricmp(name, "Rebel Fleet")) {
1682 strcpy(name, "Flota Rebelii");
1684 } else if (!stricmp(name, "Sentry Gun")) {
1685 strcpy(name, "Dzia\xB3o str.");
1687 } else if (!stricmp(name, "Cargo")) {
1688 strcpy(name, "\xA3\x61\x64unek");
1690 } else if (!stricmp(name, "Knossos Device")) {
1691 strcpy(name, "Urz. Knossos");
1693 } else if (!stricmp(name, "Support")) {
1694 strcpy(name, "Wsparcie");
1696 } else if (!stricmp(name, "Unknown")) {
1697 strcpy(name, "Nieznany");
1699 } else if (!stricmp(name, "Instructor")) {
1700 strcpy(name, "Instruktor");
1702 } else if (!stricmp(name, "Jump Node")) {
1703 strcpy(name, "W\xEAze\xB3 skokowy");
1705 } else if (!stricmp(name, "Escort")) {
1706 strcpy(name, "Eskorta");
1708 } else if (!stricmp(name, "Asteroid Field")) {
1709 strcpy(name, "Pole asteroid");
1711 } else if (!stricmp(name, "Enif Station")) {
1712 strcpy(name, "Stacja Enif");
1714 } else if (!stricmp(name, "Rally Point")) {
1715 strcpy(name, "Pkt zborny");
1717 } else if ((pos = strstr(name, "Transport")) != NULL) {
1718 pos += 9; // strlen of "transport"
1719 strcpy(buf, "Transporter");
1723 } else if ((pos = strstr(name, "Jump Node")) != NULL) {
1724 pos += 9; // strlen of "jump node"
1725 strcpy(buf, "W\xEAze\xB3 skokowy");
1729 } else if (!stricmp(name, "Orion under repair")) {
1730 strcpy(name, "Naprawiany Orion");
1734 // ------------------------------------------------------------------
1735 // lcl_translate_ship_name()
1737 // For displaying ship names in german version in the briefing
1738 // since we cant actually just change them outright.
1740 void lcl_translate_ship_name(char *name)
1742 if (!strcmp(name, "GTDR Amazon Advanced")) {
1743 strcpy(name, "GTDR Amazon VII");
1747 // ------------------------------------------------------------------
1748 // lcl_translate_targetbox_name()
1750 // For displaying ship names in german version in the targetbox
1751 // since we cant actually just change them outright.
1753 void lcl_translate_targetbox_name(char *name)
1758 if ((pos = strstr(name, "Sentry")) != NULL) {
1759 pos += 6; // strlen of "sentry"
1760 strcpy(buf, "Sperrgesch\x81tz");
1764 } else if ((pos = strstr(name, "Support")) != NULL) {
1765 pos += 7; // strlen of "support"
1766 strcpy(buf, "Versorger");
1770 } else if ((pos = strstr(name, "Unknown")) != NULL) {
1771 pos += 7; // strlen of "unknown"
1772 strcpy(buf, "Unbekannt");
1776 } else if ((pos = strstr(name, "Drone")) != NULL) {
1777 pos += 5; // strlen of "drone"
1778 strcpy(buf, "Drohne");
1782 } else if ((pos = strstr(name, "Jump Node")) != NULL) {
1783 pos += 9; // strlen of "jump node"
1784 strcpy(buf, "Sprungknoten");
1788 } else if (!stricmp(name, "Instructor")) {
1789 strcpy(name, "Ausbilder");
1791 } else if (!stricmp(name, "NTF Vessel")) {
1792 strcpy(name, "NTF-Schiff");
1794 } else if (!stricmp(name, "Enif Station")) {
1795 strcpy(name, "Station Enif");
1799 // ------------------------------------------------------------------
1800 // lcl_translate_targetbox_name_pl()
1802 // For displaying ship names in polish version in the targetbox
1803 // since we cant actually just change them outright.
1805 void lcl_translate_targetbox_name_pl(char *name)
1810 if ((pos = strstr(name, "Sentry")) != NULL) {
1811 pos += 6; // strlen of "sentry"
1812 strcpy(buf, "Stra\xBFnik");
1816 } else if ((pos = strstr(name, "Support")) != NULL) {
1817 pos += 7; // strlen of "support"
1818 strcpy(buf, "Wsparcie");
1822 } else if ((pos = strstr(name, "Unknown")) != NULL) {
1823 pos += 7; // strlen of "unknown"
1824 strcpy(buf, "Nieznany");
1828 } else if ((pos = strstr(name, "Drone")) != NULL) {
1829 pos += 5; // strlen of "drone"
1830 strcpy(buf, "Sonda");
1834 } else if ((pos = strstr(name, "Jump Node")) != NULL) {
1835 pos += 9; // strlen of "jump node"
1836 strcpy(buf, "W\xEAze\xB3 skokowy");
1840 } else if (!stricmp(name, "Instructor")) {
1841 strcpy(name, "Instruktor");
1843 } else if (!stricmp(name, "NTF Vessel")) {
1844 strcpy(name, "Okr\xEAt NTF");
1846 } else if (!stricmp(name, "Enif Station")) {
1847 strcpy(name, "Stacja Enif");