Crossfire Server, Branches 1.12  R18729
languages.c
Go to the documentation of this file.
1 /*
2  CrossFire, A Multiplayer game for X-windows
3 
4  Copyright (C) 2002,2006 Mark Wedel & Crossfire Development Team
5  Copyright (C) 1992 Frank Tore Johansen
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21  The authors can be reached via e-mail at crossfire-devel@real-time.com
22 */
23 
31 #include <global.h>
32 
34 const char *language_codes[] = {
35  "en",
36  "fr",
37  "nl",
38  "it",
39  "de"
40 };
41 
43 const char *language_names[] = {
44  "English",
45  "Français",
46  "Nederlands",
47  "Italiano",
48  "Deutsch"
49 };
52 
59 int get_language(object *op) {
60  if (!op->contr)
61  return 0;
62  if (op->contr->language < 0 || op->contr->language >= NUM_LANGUAGES)
63  return 0;
64  return op->contr->language;
65 }
66 
73 const char *i18n_translate(int language, int id) {
74  if (language >= NUM_LANGUAGES)
75  return NULL;
76  else if (id >= NUM_I18N_STRINGS)
77  return NULL;
78 
79  if (i18n_strings[language][id] == NULL)
80  return i18n_strings[0][id];
81  else
82  return i18n_strings[language][id];
83 }
84 
93 static void convert_newline(char *line) {
94  char *next;
95  char buf[MAX_BUF];
96 
97  while ((next = strstr(line, "\\n")) != NULL) {
98  *next = '\n';
99  *(next+1) = '\0';
100  snprintf(buf, MAX_BUF, "%s%s", line, next+2);
101  strcpy(line, buf);
102  }
103 }
104 
108 void i18n_init(void) {
109  char filename[MAX_BUF], line[HUGE_BUF];
110  int i, entry;
111  FILE *fp;
112  char *token;
113  int counter;
114  char *buffer;
115 
116  for (i = 0; i < NUM_LANGUAGES; i++) {
117  snprintf(filename, sizeof(filename), "%s/i18n/messages.%s", settings.datadir, language_codes[i]);
118  if ((fp = fopen(filename, "r")) == NULL) {
119  LOG(llevError, "Cannot open i18n file %s: %s\n", filename, strerror_local(errno, line, sizeof(line)));
120  if (i == 0)
121  exit(1);
122  } else {
123  counter = 0;
124  while (fgets(line, MAX_BUF, fp)) {
125  if (strstr(line, "#") != line) {
126  line[strlen(line)-1] = '\0'; /* erase the final newline that messes things. */
127  token = strtok(line, "|");
128  entry = atoi(token);
129  token = strtok(NULL, "|");
130  buffer = malloc(sizeof(char)*(strlen(token)+1));
131  strcpy(buffer, token);
132  convert_newline(buffer);
133  i18n_strings[i][entry] = buffer;
134  }
135  counter++;
136  }
137  LOG(llevDebug, "Read %i strings for language: %s\n", counter, language_codes[i]);
138  fclose(fp);
139  }
140  }
141 }
#define HUGE_BUF
Definition: define.h:83
int language
Definition: player.h:255
const char * language_names[]
Definition: languages.c:43
void i18n_init(void)
Definition: languages.c:108
#define NUM_I18N_STRINGS
Definition: languages.h:34
struct pl * contr
Definition: object.h:134
const char * language_codes[]
Definition: languages.c:34
static void convert_newline(char *line)
Definition: languages.c:93
#define MAX_BUF
Definition: define.h:81
const char * datadir
Definition: global.h:334
int snprintf(char *dest, int max, const char *format,...)
Definition: porting.c:498
int get_language(object *op)
Definition: languages.c:59
struct Settings settings
Definition: init.c:48
#define NUM_LANGUAGES
Definition: languages.h:33
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:63
const char * i18n_strings[NUM_LANGUAGES][NUM_I18N_STRINGS]
Definition: languages.c:51
char * strerror_local(int errnum, char *buf, size_t size)
Definition: porting.c:525
const char * i18n_translate(int language, int id)
Definition: languages.c:73