Skip to content

Commit

Permalink
rg_localization: Got rid of the switch, made GUI dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
ducalex committed Nov 10, 2024
1 parent 2a90c21 commit 4bfeb36
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 63 deletions.
25 changes: 5 additions & 20 deletions LOCALIZATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ If you want to add your own language :
First, this is what the struct definition should looks like in rg_localization.h :
````c
typedef struct {
char *msg; // Original message in english
char *fr; // FR Translated message
const char *msg; // Original message in english
const char *fr; // FR Translated message

char *es; // to add spanish translation
const char *es; // to add spanish translation
} Translation;
````

Expand All @@ -31,26 +31,11 @@ enum languages

RG_LANG_ES, // to add spanish translation

RG_LANGUAGE_MAX
RG_LANG_MAX
};
````

Lastly, you have to update the switch statement in rg_localization.c :
````c
switch (rg_language)
{
case RG_LANG_FR:
return translations[i].fr; // Return the french string
break;


// to add spanish translation
case RG_LANG_ES:
return translations[i].es; // Return the spanish string
break;
}
````

# Python tool
`rg_locate_str.py` is a simple python tool that locate every string preceded by `_("` pattern in each file of Retro-go project
Then the tool compare theses strings to the ones in `rg_localization.h` and put the missing ones in a .txt file called missing_translation.txt
Then the tool compare theses strings to the ones in `translations.h` and put the missing ones in a .txt file called missing_translation.txt
23 changes: 12 additions & 11 deletions components/retro-go/rg_gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ void rg_gui_init(void)
gui.screen_width = rg_display_get_info()->screen.width;
gui.screen_height = rg_display_get_info()->screen.height;
gui.draw_buffer = get_draw_buffer(gui.screen_width, 18, C_BLACK);
rg_gui_set_language_id(rg_settings_get_number(NS_GLOBAL, SETTING_LANGUAGE, RG_LANG_EN));
rg_gui_set_font(rg_settings_get_number(NS_GLOBAL, SETTING_FONTTYPE, RG_FONT_VERA_12));
rg_gui_set_theme(rg_settings_get_string(NS_GLOBAL, SETTING_THEME, NULL));
gui.show_clock = rg_settings_get_number(NS_GLOBAL, SETTING_CLOCK, 0);
rg_gui_set_language_id(rg_settings_get_number(NS_GLOBAL, SETTING_LANGUAGE, 0));
gui.initialized = true;
}

Expand Down Expand Up @@ -1352,29 +1352,30 @@ static rg_gui_event_t theme_cb(rg_gui_option_t *option, rg_gui_event_t event)

static rg_gui_event_t language_cb(rg_gui_option_t *option, rg_gui_event_t event)
{
const char *languages[] = {"English", "Francais"};
int slot = rg_localization_get_language_id();
int language_id = rg_localization_get_language_id();
const char *language_name = rg_localization_get_language_name(language_id);

if (event == RG_DIALOG_ENTER)
{
// to do : make this dynamic
const rg_gui_option_t options[] = {
{0, _(languages[0]), NULL, RG_DIALOG_FLAG_NORMAL, NULL},
{1, _(languages[1]), NULL, RG_DIALOG_FLAG_NORMAL, NULL},
RG_DIALOG_END,
};
int sel = rg_gui_dialog(_("Language"), options, slot);
rg_gui_option_t options[RG_LANG_MAX];
for (int i = 0; i < RG_LANG_MAX; i++)
options[i] = (rg_gui_option_t){i, rg_localization_get_language_name(i), NULL, RG_DIALOG_FLAG_NORMAL, NULL};
options[RG_LANG_MAX - 1] = (rg_gui_option_t)RG_DIALOG_END;

int sel = rg_gui_dialog(_("Language"), options, language_id);
if (sel != RG_DIALOG_CANCELLED)
{
rg_gui_set_language_id(sel);
if (rg_gui_confirm(_("Language changed!"), _("For these changes to take effect you must restart your device.\nrestart now?"), true))
{
rg_system_exit();
}
language_id = sel;
language_name = rg_localization_get_language_name(sel);
}
return RG_DIALOG_REDRAW;
}
sprintf(option->value, "%s", languages[slot]);
sprintf(option->value, "%s", language_name ? language_name : "???");
return RG_DIALOG_VOID;
}

Expand Down
39 changes: 20 additions & 19 deletions components/retro-go/rg_localization.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "rg_localization.h"
#include "translations.h"

int rg_language = RG_LANG_EN;
static int rg_language = RG_LANG_EN;

int rg_localization_get_language_id(void)
{
Expand All @@ -10,36 +10,37 @@ int rg_localization_get_language_id(void)

bool rg_localization_set_language_id(int language_id)
{
if (language_id < 0 || language_id > RG_LANGUAGE_MAX - 1)
if (language_id < 0 || language_id > RG_LANG_MAX - 1)
return false;

rg_language = language_id;
return true;
}

const char* rg_gettext(const char *text)
const char *rg_gettext(const char *text)
{
if (rg_language == RG_LANG_EN)
return text; // in case language == 0 == english -> return the original string
if (text == NULL)
return NULL;

if (rg_language <= 0 || rg_language >= RG_LANG_MAX)
return text; // If rg_language is english or invalid, we return the original string

for (int i = 0; translations[i].msg != NULL; i++)
{
if (strcmp(translations[i].msg, text) == 0)
{
switch (rg_language)
{
case RG_LANG_FR:
return translations[i].fr; // Return the french string
break;

/*
case RG_LANG_ES:
return translations[i].es; // Return the spanish string
break;
*/

}
const char *msg = translations[i].msgs[rg_language];
// If the translation is missing, we return the original string
return msg ? msg : text;
}
}

return text; // if no translation found
}
}

const char *rg_localization_get_language_name(int language_id)
{
if (language_id < 0 || language_id > RG_LANG_MAX - 1)
return NULL;
return language_names[language_id];
}
28 changes: 16 additions & 12 deletions components/retro-go/rg_localization.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@

#define _(String) rg_gettext(String)

// Define a struct for holding message translations
typedef struct
{
char *msg; // Original message in english
char *fr; // FR Translated message
// char *de; // for adding DE translation for example
} Translation;

enum languages
{
RG_LANG_EN,
RG_LANG_EN = 0,
RG_LANG_FR,
//RG_LANG_ES,

RG_LANGUAGE_MAX
RG_LANG_MAX
};

// Define a struct for holding message translations
typedef union
{
struct {
const char *msg; // Original message in english
const char *fr; // FR Translated message
// const char *es; // for adding ES translation for example
};
const char *msgs[RG_LANG_MAX];
} Translation;

// Lookup function
const char* rg_gettext(const char *msg);
const char *rg_gettext(const char *msg);
int rg_localization_get_language_id(void);
bool rg_localization_set_language_id(int language_id);
bool rg_localization_set_language_id(int language_id);
const char *rg_localization_get_language_name(int language_id);
4 changes: 3 additions & 1 deletion components/retro-go/translations.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

#include "rg_localization.h"

Translation translations[] =
static const char *language_names[RG_LANG_MAX] = {"English", "Francais"};

static const Translation translations[] =
{
{
.msg = "Yes",
Expand Down

0 comments on commit 4bfeb36

Please sign in to comment.