| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#include "global.h" |
| 15 |
|
| 16 |
#include <errno.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
typedef struct i18n_message { |
| 24 |
sstring code; |
| 25 |
sstring message; |
| 26 |
} i18n_message; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
typedef struct i18n_file { |
| 32 |
sstring code; |
| 33 |
sstring name; |
| 34 |
int count; |
| 35 |
struct i18n_message *messages; |
| 36 |
} i18n_file; |
| 37 |
|
| 38 |
|
| 39 |
static int i18n_count = 0; |
| 40 |
|
| 41 |
static struct i18n_file *i18n_files = NULL; |
| 42 |
|
| 43 |
static int i18n_default = -1; |
| 44 |
|
| 45 |
static int i18n_message_compare_code(const i18n_message *a, const i18n_message *b) { |
| 46 |
return strcmp(a->code, b->code); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
int get_language(object *op) { |
| 56 |
if (!op->contr) |
| 57 |
return 0; |
| 58 |
if (op->contr->language < 0 || op->contr->language >= i18n_count) |
| 59 |
return 0; |
| 60 |
return op->contr->language; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
const char *i18n(const object *who, const char *code) { |
| 70 |
i18n_message search, *found; |
| 71 |
|
| 72 |
if (!who || !who->contr) |
| 73 |
return code; |
| 74 |
|
| 75 |
if (who->contr->language < 0 || who->contr->language >= i18n_count) |
| 76 |
return code; |
| 77 |
|
| 78 |
search.code = add_string(code); |
| 79 |
|
| 80 |
found = bsearch(&search, i18n_files[who->contr->language].messages, i18n_files[who->contr->language].count, sizeof(i18n_message), (int (*)(const void *, const void *))i18n_message_compare_code); |
| 81 |
|
| 82 |
free_string(search.code); |
| 83 |
|
| 84 |
if (found) |
| 85 |
return found->message; |
| 86 |
|
| 87 |
return code; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
int i18n_find_language_by_code(const char *code) { |
| 96 |
int index; |
| 97 |
for (index = 0; index < i18n_count; index++) { |
| 98 |
if (strcmp(code, i18n_files[index].code) == 0) |
| 99 |
return index; |
| 100 |
} |
| 101 |
|
| 102 |
return -1; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
int i18n_get_language_by_code(const char *code) { |
| 111 |
int try = i18n_find_language_by_code(code); |
| 112 |
if (try != -1) |
| 113 |
return try; |
| 114 |
return i18n_default; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
sstring i18n_get_language_code(int language) { |
| 123 |
if (language < 0 || language >= i18n_count) |
| 124 |
return i18n_files[i18n_default].code; |
| 125 |
return i18n_files[language].code; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
void i18n_list_languages(object *who) { |
| 133 |
int index; |
| 134 |
for (index = 0; index < i18n_count; index++) { |
| 135 |
draw_ext_info_format(NDI_UNIQUE, 0, who, MSG_TYPE_COMMAND, MSG_SUBTYPE_NONE, |
| 136 |
"[fixed]%s: %s", |
| 137 |
i18n_files[index].code, |
| 138 |
i18n_files[index].name |
| 139 |
); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
static void convert_newline(char *line) { |
| 152 |
char *next; |
| 153 |
char buf[MAX_BUF]; |
| 154 |
|
| 155 |
while ((next = strstr(line, "\\n")) != NULL) { |
| 156 |
*next = '\n'; |
| 157 |
*(next+1) = '\0'; |
| 158 |
snprintf(buf, MAX_BUF, "%s%s", line, next+2); |
| 159 |
strcpy(line, buf); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
void i18n_init(void) { |
| 169 |
char dirname[MAX_BUF], filename[MAX_BUF*2], line[HUGE_BUF]; |
| 170 |
FILE *fp; |
| 171 |
char *token; |
| 172 |
DIR *dir; |
| 173 |
struct dirent *file; |
| 174 |
i18n_message code, *found; |
| 175 |
|
| 176 |
snprintf(dirname, sizeof(dirname), "%s/i18n/", settings.datadir); |
| 177 |
|
| 178 |
dir = opendir(dirname); |
| 179 |
if (dir == NULL) { |
| 180 |
LOG(llevError, "i18n: couldn't open %s\n", dirname); |
| 181 |
fatal(SEE_LAST_ERROR); |
| 182 |
} |
| 183 |
|
| 184 |
code.code = add_string("LN"); |
| 185 |
|
| 186 |
while ((file = readdir(dir)) != NULL) { |
| 187 |
if (strncmp(file->d_name, "messages.", 9) != 0) |
| 188 |
continue; |
| 189 |
|
| 190 |
snprintf(filename, sizeof(filename), "%s%s", dirname, file->d_name); |
| 191 |
if ((fp = fopen(filename, "r")) == NULL) { |
| 192 |
LOG(llevError, "i18n: couldn't open %s\n", |
| 193 |
filename, strerror(errno)); |
| 194 |
fatal(SEE_LAST_ERROR); |
| 195 |
} |
| 196 |
|
| 197 |
i18n_files = realloc(i18n_files, (i18n_count + 1) * sizeof(i18n_file)); |
| 198 |
i18n_files[i18n_count].code = add_string(file->d_name + 9); |
| 199 |
i18n_files[i18n_count].count = 0; |
| 200 |
i18n_files[i18n_count].messages = NULL; |
| 201 |
|
| 202 |
while (fgets(line, MAX_BUF, fp)) { |
| 203 |
if (line[0] != '#') { |
| 204 |
line[strlen(line)-1] = '\0'; |
| 205 |
|
| 206 |
i18n_files[i18n_count].messages = realloc(i18n_files[i18n_count].messages, (i18n_files[i18n_count].count + 1) * sizeof(i18n_message)); |
| 207 |
|
| 208 |
token = strtok(line, "|"); |
| 209 |
convert_newline(token); |
| 210 |
i18n_files[i18n_count].messages[i18n_files[i18n_count].count].code = add_string(token); |
| 211 |
token = strtok(NULL, "|"); |
| 212 |
if (token != NULL) { |
| 213 |
convert_newline(token); |
| 214 |
i18n_files[i18n_count].messages[i18n_files[i18n_count].count].message = add_string(token); |
| 215 |
} else { |
| 216 |
i18n_files[i18n_count].messages[i18n_files[i18n_count].count].message = add_refcount(i18n_files[i18n_count].messages[i18n_files[i18n_count].count].code); |
| 217 |
} |
| 218 |
i18n_files[i18n_count].count++; |
| 219 |
} |
| 220 |
} |
| 221 |
fclose(fp); |
| 222 |
|
| 223 |
qsort(i18n_files[i18n_count].messages, i18n_files[i18n_count].count, sizeof(i18n_message), (int (*)(const void *, const void *))i18n_message_compare_code); |
| 224 |
found = bsearch(&code, i18n_files[i18n_count].messages, i18n_files[i18n_count].count, sizeof(i18n_message), (int (*)(const void *, const void *))i18n_message_compare_code); |
| 225 |
if (found == NULL) { |
| 226 |
LOG(llevError, "i18n: no language set in %s\n", filename); |
| 227 |
fatal(SEE_LAST_ERROR); |
| 228 |
} |
| 229 |
|
| 230 |
i18n_files[i18n_count].name = found->message; |
| 231 |
LOG(llevDebug, "i18n: %d strings for %s\n", |
| 232 |
i18n_files[i18n_count].count, found->message); |
| 233 |
|
| 234 |
if (strcmp(i18n_files[i18n_count].code, "en") == 0) |
| 235 |
i18n_default = i18n_count; |
| 236 |
|
| 237 |
i18n_count++; |
| 238 |
} |
| 239 |
closedir(dir); |
| 240 |
|
| 241 |
free_string(code.code); |
| 242 |
|
| 243 |
if (i18n_default == -1) { |
| 244 |
LOG(llevError, "i18n: couldn't find default language (en)\n"); |
| 245 |
fatal(SEE_LAST_ERROR); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
void i18n_free(void) { |
| 253 |
int file, message; |
| 254 |
|
| 255 |
for (file = 0; file < i18n_count; file++) { |
| 256 |
free_string(i18n_files[file].code); |
| 257 |
for (message = 0; message < i18n_files[file].count; message++) { |
| 258 |
free_string(i18n_files[file].messages[message].code); |
| 259 |
free_string(i18n_files[file].messages[message].message); |
| 260 |
} |
| 261 |
free(i18n_files[file].messages); |
| 262 |
} |
| 263 |
free(i18n_files); |
| 264 |
i18n_files = NULL; |
| 265 |
i18n_count = 0; |
| 266 |
} |