Skip to content

Commit

Permalink
Set the main menu item name to Chinese.
Browse files Browse the repository at this point in the history
  • Loading branch information
ligenxxxx committed Nov 6, 2024
1 parent 4b4f846 commit df9871a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 12 deletions.
44 changes: 44 additions & 0 deletions src/fonts/language.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "language.h"

lang_e LANGUAGE = lang_simplified_chinese;

translate_t simplified_chinese[] = {
{"Scan Now", "扫频"},
{"Source", "信号源"},
{"Image Setting", "图像设置"},
{"OSD", "OSD"},
{"Power", "电源"},
{"Fans", "风扇"},
{"Record Option", "录像设置"},
{"Auto Scan", "自动扫频"},
{"ELRS", "ELRS"},
{"WiFi Module", "WiFi模块"},
{"Head Tracker", "头部追踪"},
{"Playback", "录像回放"},
{"Storage", "存储"},
{"Firmware ", "固件"},
{"Focus Chart", "焦点图"},
{"Clock", "时间"},
{"Input", "按键"},
{"Go Sleep!", "睡眠"},
};

char *translate_string(const char *str, lang_e lang) {
int i;
int string_num = sizeof(simplified_chinese) / sizeof(translate_t);

if (lang == 0)
return (char *)str;
else {
for (i = 0; i < string_num; i++) {
if (strcmp(str, simplified_chinese[i].in_english) == 0)
return simplified_chinese[i].translate;
}
return (char *)str;
}
}

void lv_label_set_text_lang(lv_obj_t *obj, const char *text, lang_e lang) {
char *text_lang = _str(text, lang);
lv_label_set_text(obj, text_lang);
}
23 changes: 23 additions & 0 deletions src/fonts/language.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef _LANGUAGE_H_
#define _LANGUAGE_H_

#include <lvgl/lvgl.h>

typedef enum {
lang_english = 0,
lang_simplified_chinese = 1,
} lang_e;

typedef struct {
char *in_english;
char *translate;
} translate_t;

char *translate_string(const char *str, lang_e lang);

#define _str(string, lang) translate_string(string, lang)

void lv_label_set_text_lang(lv_obj_t *obj, const char *text, lang_e lang);

extern lang_e LANGUAGE;
#endif
20 changes: 11 additions & 9 deletions src/ui/ui_main_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "driver/hardware.h"
#include "driver/mcp3021.h"
#include "driver/oled.h"
#include "fonts/language.h"
#include "ui/page_autoscan.h"
#include "ui/page_clock.h"
#include "ui/page_common.h"
Expand Down Expand Up @@ -67,7 +68,7 @@ static page_pack_t *page_packs[] = {

#define PAGE_COUNT (sizeof(page_packs) / sizeof(page_packs[0]))

static page_pack_t* post_bootup_actions[PAGE_COUNT + 1];
static page_pack_t *post_bootup_actions[PAGE_COUNT + 1];

static page_pack_t *find_pp(lv_obj_t *page) {
for (uint32_t i = 0; i < PAGE_COUNT; i++) {
Expand All @@ -78,15 +79,15 @@ static page_pack_t *find_pp(lv_obj_t *page) {
return NULL;
}

static void select_menu_tab(page_pack_t* pp) {
static void select_menu_tab(page_pack_t *pp) {
lv_obj_clear_flag(pp->icon, LV_OBJ_FLAG_HIDDEN);
lv_obj_set_style_bg_opa(((lv_menu_t*)menu)->selected_tab, LV_OPA_50, LV_STATE_CHECKED);
lv_obj_set_style_bg_opa(((lv_menu_t *)menu)->selected_tab, LV_OPA_50, LV_STATE_CHECKED);
}

static void deselect_menu_tab(page_pack_t* pp) {
static void deselect_menu_tab(page_pack_t *pp) {
// LV_OPA_20 is the default for pressed menu
// see lv_theme_default.c styles->menu_pressed
lv_obj_set_style_bg_opa(((lv_menu_t*)menu)->selected_tab, LV_OPA_20, LV_STATE_CHECKED);
lv_obj_set_style_bg_opa(((lv_menu_t *)menu)->selected_tab, LV_OPA_20, LV_STATE_CHECKED);
lv_obj_add_flag(pp->icon, LV_OBJ_FLAG_HIDDEN);
}

Expand All @@ -100,7 +101,8 @@ void submenu_enter(void) {

if (pp->p_arr.max) {
// if we have selectable entries, select the first selectable one
for (pp->p_arr.cur = 0; !lv_obj_has_flag(pp->p_arr.panel[pp->p_arr.cur], FLAG_SELECTABLE); ++pp->p_arr.cur);
for (pp->p_arr.cur = 0; !lv_obj_has_flag(pp->p_arr.panel[pp->p_arr.cur], FLAG_SELECTABLE); ++pp->p_arr.cur)
;
set_select_item(&pp->p_arr, pp->p_arr.cur);
}

Expand Down Expand Up @@ -270,7 +272,7 @@ static void main_menu_create_entry(lv_obj_t *menu, lv_obj_t *section, page_pack_
lv_obj_t *cont = lv_menu_cont_create(section);

pp->label = lv_label_create(cont);
lv_label_set_text(pp->label, pp->name);
lv_label_set_text_lang(pp->label, pp->name, LANGUAGE);
lv_obj_set_style_text_font(pp->label, &lv_font_montserrat_26, 0);
lv_label_set_long_mode(pp->label, LV_LABEL_LONG_SCROLL_CIRCULAR);

Expand Down Expand Up @@ -319,7 +321,7 @@ void main_menu_init(void) {
post_bootup_actions[PAGE_COUNT] = post_bootup_actions[i];
post_bootup_actions[i] = post_bootup_actions[j];
post_bootup_actions[j] = post_bootup_actions[PAGE_COUNT];
}
}
}
}
}
Expand Down Expand Up @@ -371,7 +373,7 @@ void main_menu_update() {
++last_bootup_action;
}
} else { // Thread invokation
if (post_bootup_actions[i]->post_bootup_run_function){
if (post_bootup_actions[i]->post_bootup_run_function) {
post_bootup_actions[i]->post_bootup_run_function();
post_bootup_actions[i]->post_bootup_run_function = NULL;
} else if (post_bootup_actions[i]->post_bootup_run_complete()) {
Expand Down
6 changes: 3 additions & 3 deletions src/ui/ui_main_menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extern "C" {
#include "ui/page_common.h"

typedef struct {
const char *name;
char *name;
panel_arr_t p_arr;

lv_obj_t *page;
Expand All @@ -29,8 +29,8 @@ typedef struct {
void (*on_right_button)(bool is_short);

int32_t post_bootup_run_priority;
void(*post_bootup_run_function)();
bool(*post_bootup_run_complete)();
void (*post_bootup_run_function)();
bool (*post_bootup_run_complete)();
} page_pack_t;

typedef struct {
Expand Down

0 comments on commit df9871a

Please sign in to comment.