Skip to content

Commit

Permalink
Allow to change and save the language
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumechereau committed Jan 9, 2024
1 parent 10ed103 commit 2f89d64
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
26 changes: 25 additions & 1 deletion src/gui/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
43 changes: 40 additions & 3 deletions src/i18n.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,57 @@

#include <assert.h>
#include <stdio.h>
#include <string.h>

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[] = {
Expand Down
9 changes: 9 additions & 0 deletions src/i18n.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 2f89d64

Please sign in to comment.