From 2f89d64837b74ed4d3f84f9a547024fdf864ba14 Mon Sep 17 00:00:00 2001 From: Guillaume Chereau Date: Tue, 9 Jan 2024 23:10:23 +0800 Subject: [PATCH] Allow to change and save the language --- src/gui/settings.c | 26 +++++++++++++++++++++++++- src/i18n.c | 43 ++++++++++++++++++++++++++++++++++++++++--- src/i18n.h | 9 +++++++++ 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/src/gui/settings.c b/src/gui/settings.c index f4bcae79d..011322745 100644 --- a/src/gui/settings.c +++ b/src/gui/settings.c @@ -42,8 +42,24 @@ int gui_settings_popup(void *data) int i, nb, current; theme_t *themes = theme_get_list(); int ret = 0; + const tr_lang_t *language; + const tr_lang_t *languages; + + if (gui_section_begin("Language", GUI_SECTION_COLLAPSABLE)) { + language = tr_get_language(); + if (gui_combo_begin("##lang", language->name)) { + languages = tr_get_supported_languages(); + for (i = 0; languages[i].id; i++) { + if (gui_combo_item(languages[i].name, + &languages[i] == language)) { + tr_set_language(languages[i].id); + } + } + gui_combo_end(); + } + } gui_section_end(); - if (gui_section_begin("Theme", GUI_SECTION_COLLAPSABLE_CLOSED)) { + if (gui_section_begin("Theme", GUI_SECTION_COLLAPSABLE)) { DL_COUNT(themes, theme, nb); names = (const char**)calloc(nb, sizeof(*names)); i = 0; @@ -88,6 +104,9 @@ static int settings_ini_handler(void *user, const char *section, if (strcmp(name, "theme") == 0) { theme_set(value); } + if (strcmp(name, "language") == 0) { + tr_set_language(value); + } } if (strcmp(section, "shortcuts") == 0) { if ((a = action_get_by_name(name))) { @@ -118,7 +137,11 @@ void settings_save(void) { char path[1024]; FILE *file; + const tr_lang_t *lang; + + lang = tr_get_language(); snprintf(path, sizeof(path), "%s/settings.ini", sys_get_user_dir()); + LOG_I("Save settings to %s", path); sys_make_dir(path); file = fopen(path, "w"); if (!file) { @@ -127,6 +150,7 @@ void settings_save(void) } fprintf(file, "[ui]\n"); fprintf(file, "theme=%s\n", theme_get()->name); + fprintf(file, "language=%s\n", lang->id); fprintf(file, "[shortcuts]\n"); actions_iter(shortcut_save_callback, file); diff --git a/src/i18n.c b/src/i18n.c index 65d0b441f..b2793d22c 100644 --- a/src/i18n.c +++ b/src/i18n.c @@ -20,20 +20,57 @@ #include #include +#include + +static const tr_lang_t LANGUAGES[] = { + {"en", "English"}, + {"fr", "Français"}, + {} +}; typedef struct { - const char *en; - const char *fr; + union { + struct { + const char *en; + const char *fr; + }; + const char *values[2]; + }; } string_t; static const string_t STRINGS[]; +static int current_lang_idx = 0; + +#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) + const char *tr(int id) { // Only return the English for now. if (!STRINGS[id].en) fprintf(stderr, "Str %d not defined.\n", id); assert(STRINGS[id].en); - return STRINGS[id].en; + return STRINGS[id].values[current_lang_idx]; +} + +void tr_set_language(const char *id) +{ + int i; + for (i = 0; i < ARRAY_SIZE(LANGUAGES); i++) { + if (strcmp(LANGUAGES[i].id, id) == 0) { + current_lang_idx = i; + break; + } + } +} + +const tr_lang_t *tr_get_language(void) +{ + return &LANGUAGES[current_lang_idx]; +} + +const tr_lang_t *tr_get_supported_languages(void) +{ + return LANGUAGES; } static const string_t STRINGS[] = { diff --git a/src/i18n.h b/src/i18n.h index bfabebfac..8321df622 100644 --- a/src/i18n.h +++ b/src/i18n.h @@ -140,3 +140,12 @@ enum { const char *tr(int str); #define _(x) tr(STR_##x) + +typedef struct { + const char *id; + const char *name; +} tr_lang_t; + +void tr_set_language(const char *id); +const tr_lang_t *tr_get_language(void); +const tr_lang_t *tr_get_supported_languages(void);