42 const char *
i18n(
const object *who,
const char *
code) {
50 auto found = file->
messages.find(scode);
52 return found == file->
messages.end() ?
code : found->second;
62 if (strcmp(code, language->code) == 0)
88 return i18n_default->
code;
118 while ((next = strstr(line,
"\\n")) != NULL) {
121 snprintf(buf,
MAX_BUF,
"%s%s", line, next+2);
148 while ((file =
readdir(dir)) != NULL) {
149 if (strncmp(file->d_name,
"messages.", 9) != 0)
152 snprintf(filename,
sizeof(filename),
"%s%s", dirname, file->d_name);
167 if (line[0] !=
'#' && line[0] !=
'\0') {
169 token = strtok(line,
"|");
172 token = strtok(NULL,
"|");
179 language->
messages[scode] = smessage;
184 auto found = language->
messages.find(code);
185 if (found == language->
messages.end()) {
186 LOG(
llevError,
"i18n: no language set in %s\n", filename);
190 language->
name = found->second;
194 if (strcmp(language->
code,
"en") == 0)
195 i18n_default = language;
201 if (i18n_default ==
nullptr) {
202 LOG(
llevError,
"i18n: couldn't find default language (en)\n");
213 for (
auto message : language->messages) {
void bufferreader_destroy(BufferReader *br)
Destroy a BufferReader.
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
void i18n_init(void)
Initializes the i18n subsystem.
void draw_ext_info_format(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *format,...) PRINTF_ARGS(6
void * language_t
Strings that should be manipulated through add_string() and free_string().
const char * i18n(const object *who, const char *code)
Translate a message in the appropriate language.
sstring name
Language's name, in its native version.
sstring add_string(const char *str)
Share a string.
static void convert_newline(char *line)
Replaces ' ' by a newline char.
sstring find_string(const char *str)
Searches a string in the shared strings.
DIR * opendir(const char *)
sstring add_refcount(sstring str)
Like add_string(), but the string is already a shared string.
Global type definitions and header inclusions.
struct player * contr
Pointer to the player which control this object.
std::map< sstring, sstring > messages
Available messages for this language.
void free_string(sstring str)
This will reduce the refcount, and if it has reached 0, str will be freed.
#define MSG_TYPE_COMMAND
Responses to commands, eg, who.
BufferReader * bufferreader_init_from_file(BufferReader *br, const char *filepath, const char *failureMessage, LogLevel failureLevel)
Initialize or create a BufferReader from a file path.
language_t i18n_get_language_by_code(const char *code)
Find the identifier of a language from its code.
language_t language
The language the player wishes to use.
struct Settings settings
Global settings.
void i18n_list_languages(object *who)
List all languages for who.
sstring code
Language code, "message." extension.
void fatal(enum fatal_error err)
fatal() is meant to be called whenever a fatal signal is intercepted.
#define MAX_BUF
Used for all kinds of things.
const char * datadir
Read only data files.
Only for debugging purposes.
struct dirent * readdir(DIR *)
language_t i18n_find_language_by_code(const char *code)
Attempt to find the identifier of a language from its code.
#define NDI_UNIQUE
Print immediately, don't buffer.
static i18n_file * i18n_default
"English" language.
char * bufferreader_next_line(BufferReader *br)
Return the next line in the buffer, as separated by a newline.
static std::vector< i18n_file * > i18n_files
Defined languages.
sstring i18n_get_language_code(language_t language)
Return the code of a specified language.
void i18n_free(void)
Clears all i18n-related data.