Crossfire Server, Branch 1.12  R12190
languages.c
Go to the documentation of this file.
00001 /*
00002     CrossFire, A Multiplayer game for X-windows
00003 
00004     Copyright (C) 2002,2006 Mark Wedel & Crossfire Development Team
00005     Copyright (C) 1992 Frank Tore Johansen
00006 
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020 
00021     The authors can be reached via e-mail at crossfire-devel@real-time.com
00022 */
00023 
00031 #include <global.h>
00032 
00034 const char *language_codes[] = {
00035     "en",
00036     "fr",
00037     "nl",
00038     "it",
00039     "de"
00040 };
00041 
00043 const char *language_names[] = {
00044     "English",
00045     "Français",
00046     "Nederlands",
00047     "Italiano",
00048     "Deutsch"
00049 };
00051 const char *i18n_strings[NUM_LANGUAGES][NUM_I18N_STRINGS];
00052 
00059 int get_language(object *op) {
00060     if (!op->contr)
00061         return 0;
00062     if (op->contr->language < 0 || op->contr->language >= NUM_LANGUAGES)
00063         return 0;
00064     return op->contr->language;
00065 }
00066 
00073 const char *i18n_translate(int language, int id) {
00074     if (language >= NUM_LANGUAGES)
00075         return NULL;
00076     else if (id >= NUM_I18N_STRINGS)
00077         return NULL;
00078 
00079     if (i18n_strings[language][id] == NULL)
00080         return i18n_strings[0][id];
00081     else
00082         return i18n_strings[language][id];
00083 }
00084 
00093 static void convert_newline(char *line) {
00094     char *next;
00095     char buf[MAX_BUF];
00096 
00097     while ((next = strstr(line, "\\n")) != NULL) {
00098         *next = '\n';
00099         *(next+1) = '\0';
00100         snprintf(buf, MAX_BUF, "%s%s", line, next+2);
00101         strcpy(line, buf);
00102     }
00103 }
00104 
00108 void i18n_init(void) {
00109     char filename[MAX_BUF], line[HUGE_BUF];
00110     int i, entry;
00111     FILE *fp;
00112     char *token;
00113     int counter;
00114     char *buffer;
00115 
00116     for (i = 0; i < NUM_LANGUAGES; i++) {
00117         snprintf(filename, sizeof(filename), "%s/i18n/messages.%s", settings.datadir, language_codes[i]);
00118         if ((fp = fopen(filename, "r")) == NULL) {
00119             LOG(llevError, "Cannot open i18n file %s: %s\n", filename, strerror_local(errno, line, sizeof(line)));
00120             if (i == 0)
00121                 exit(1);
00122         } else {
00123             counter = 0;
00124             while (fgets(line, MAX_BUF, fp)) {
00125                 if (strstr(line, "#") != line) {
00126                     line[strlen(line)-1] = '\0'; /* erase the final newline that messes things. */
00127                     token = strtok(line, "|");
00128                     entry = atoi(token);
00129                     token = strtok(NULL, "|");
00130                     buffer = malloc(sizeof(char)*(strlen(token)+1));
00131                     strcpy(buffer, token);
00132                     convert_newline(buffer);
00133                     i18n_strings[i][entry] = buffer;
00134                 }
00135                 counter++;
00136             }
00137             LOG(llevDebug, "Read %i strings for language: %s\n", counter, language_codes[i]);
00138             fclose(fp);
00139         }
00140     }
00141 }