From 24f602663878f211143bf892159588aa5b7294e9 Mon Sep 17 00:00:00 2001 From: frosty Date: Wed, 1 Apr 2026 00:31:44 +0300 Subject: feat: beginning work on localisation api --- beaker.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'beaker.h') diff --git a/beaker.h b/beaker.h index 96125d3..a5f374e 100644 --- a/beaker.h +++ b/beaker.h @@ -20,6 +20,14 @@ #define TEMPLATES_DIR "templates/" #define STATIC_DIR "static/" +#define LOCALES_DIR "locales/" + +#define INITIAL_LOCALES_CAPACITY 8 +#define INITIAL_LOCALE_KEYS_CAPACITY 16 +#define MAX_LOCALES_HARD 1024 +#define MAX_LOCALE_KEYS_HARD 65536 +#define MAX_LOCALE_ID_LEN 64 +#define MAX_LOCALE_VALUE_LEN 512 typedef enum { CONTEXT_TYPE_STRING, @@ -68,6 +76,28 @@ typedef struct { bool secure; } Cookie; +typedef struct { + char id[MAX_LOCALE_ID_LEN]; + char name[MAX_VALUE_LEN]; + char direction[16]; +} LocaleMeta; + +typedef struct { + char key[MAX_KEY_LEN]; + char value[MAX_LOCALE_VALUE_LEN]; +} LocaleKV; + +typedef struct { + LocaleMeta meta; + LocaleKV *keys; + int key_count; + int key_capacity; +} Locale; + +typedef struct { + LocaleMeta meta; +} LocaleInfo; + typedef int (*RequestHandler)(UrlParams *params); typedef struct { @@ -85,6 +115,12 @@ void context_set_array_of_arrays(TemplateContext *ctx, const char *key, void free_context(TemplateContext *ctx); char *render_template(const char *template_file, TemplateContext *ctx); +int beaker_load_locales(void); +void beaker_set_locale(TemplateContext *ctx, const char *locale_id); +int beaker_get_all_locales(LocaleInfo *out, int max_count); +const LocaleMeta *beaker_get_locale_meta(const char *locale_id); +void beaker_free_locales(void); + void send_response(const char *html); void send_redirect(const char *location); void set_cookie(const char *name, const char *value, const char *expires, -- cgit v1.2.3 From 3f1ab2576c31da36e7fc3f69694d4d76c8243842 Mon Sep 17 00:00:00 2001 From: frosty Date: Wed, 1 Apr 2026 04:00:34 +0300 Subject: feat: function for fetching content of locale keys --- beaker.h | 1 + src/l10n.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) (limited to 'beaker.h') diff --git a/beaker.h b/beaker.h index a5f374e..cc87634 100644 --- a/beaker.h +++ b/beaker.h @@ -119,6 +119,7 @@ int beaker_load_locales(void); void beaker_set_locale(TemplateContext *ctx, const char *locale_id); int beaker_get_all_locales(LocaleInfo *out, int max_count); const LocaleMeta *beaker_get_locale_meta(const char *locale_id); +const char *beaker_get_locale_value(const char *locale_id, const char *key); void beaker_free_locales(void); void send_response(const char *html); diff --git a/src/l10n.c b/src/l10n.c index f6e748f..4d8ac30 100644 --- a/src/l10n.c +++ b/src/l10n.c @@ -247,6 +247,23 @@ const LocaleMeta *beaker_get_locale_meta(const char *locale_id) { return NULL; } +const char *beaker_get_locale_value(const char *locale_id, const char *key) { + if (locale_id == NULL || key == NULL) { + return NULL; + } + for (int i = 0; i < locale_count; i++) { + if (strcmp(locales[i].meta.id, locale_id) == 0) { + for (int j = 0; j < locales[i].key_count; j++) { + if (strcmp(locales[i].keys[j].key, key) == 0) { + return locales[i].keys[j].value; + } + } + return NULL; + } + } + return NULL; +} + void beaker_free_locales(void) { for (int i = 0; i < locale_count; i++) { free(locales[i].keys); -- cgit v1.2.3