DungeonCrawl
|
Implements localization for the game. More...
#include "local_handler.h"
#include "../logging/logger.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Go to the source code of this file.
Data Structures | |
struct | observer_node |
Macros | |
#define | PATH_SEP "/" |
#define | LOCAL_DIRECTORY "resources/local" |
Typedefs | |
typedef struct observer_node | observer_node_t |
Functions | |
int | init_local_handler (const local_lang_t lang) |
Initialize the local language handler by setting up the language and opening the corresponding resource file. | |
char * | get_local_string (const char *key) |
Get the localized string for the given key. | |
int | set_language (const local_lang_t lang) |
Sets the current language for the local handler and updates all registered observers. | |
local_lang_t | get_language (void) |
Retrieve the currently set language of the local handler. | |
void | observe_local (void(*update_func)(void)) |
Registers an observer function to be notified of updates from the local handler. | |
void | shutdown_local_handler (void) |
Shut down the local language handler by releasing resources and closing the resource file. |
Variables | |
observer_node_t * | observer_list = NULL |
FILE * | local_file = NULL |
local_lang_t | current_lang |
Implements localization for the game.
Definition in file local_handler.c.
#define LOCAL_DIRECTORY "resources/local" |
Definition at line 18 of file local_handler.c.
#define PATH_SEP "/" |
Definition at line 17 of file local_handler.c.
typedef struct observer_node observer_node_t |
Definition at line 21 of file local_handler.c.
local_lang_t get_language | ( | void | ) |
Retrieve the currently set language of the local handler.
This function returns the current language configured for the local handler. If the local handler is not initialized, it logs a warning message and defaults the language to English.
Definition at line 116 of file local_handler.c.
char * get_local_string | ( | const char * | key | ) |
Get the localized string for the given key.
key | the key for the localized string |
Definition at line 54 of file local_handler.c.
int init_local_handler | ( | local_lang_t | lang | ) |
Initialize the local language handler by setting up the language and opening the corresponding resource file.
This function initializes the handler to use the specified language. It opens the associated language file based on a predefined mapping and prepares the observer list for updates. If the handler is already initialized, a warning is logged and initialization is skipped.
lang | the language to be set for the local handler |
Definition at line 32 of file local_handler.c.
void observe_local | ( | void(* | update_func )(void) | ) |
Registers an observer function to be notified of updates from the local handler.
This function adds a new observer to the observer list maintained by the local handler. The observer function will be invoked when relevant updates occur. The observer handler must be initialized before calling this function. If the observer function or handler is invalid, the registration is aborted, and an error is logged.
update_func | the observer function to be registered for receiving updates |
Definition at line 124 of file local_handler.c.
int set_language | ( | local_lang_t | lang | ) |
Sets the current language for the local handler and updates all registered observers.
This function updates the current language setting which determines the active language file to be used. It attempts to close the current language file, opens the new associated language resource file, and subsequently notifies the observer list by invoking their respective update functions. The function ensures the validity of the input language and handles errors related to file operations or uninitialized states.
lang | the language to be set (e.g., LANGE_EN, LANGE_DE) |
Definition at line 89 of file local_handler.c.
void shutdown_local_handler | ( | void | ) |
Shut down the local language handler by releasing resources and closing the resource file.
This function deallocates all memory used by the observer list tied to the local handler. It ensures that all dynamically allocated observer nodes are freed to prevent memory leaks. Additionally, it closes the language resource file if it was previously opened.
Proper usage of this function ensures a clean shutdown of the local language handler by releasing occupied resources and leaving no lingering operations.
Definition at line 140 of file local_handler.c.
local_lang_t current_lang |
Definition at line 30 of file local_handler.c.
FILE* local_file = NULL |
Definition at line 29 of file local_handler.c.
observer_node_t* observer_list = NULL |
Definition at line 28 of file local_handler.c.