diff --git a/docs/src/details/widgets/tabview.rst b/docs/src/details/widgets/tabview.rst index b22f36bd03..5baf6521d6 100644 --- a/docs/src/details/widgets/tabview.rst +++ b/docs/src/details/widgets/tabview.rst @@ -56,7 +56,7 @@ Renaming tabs ------------- A tab can be renamed with -:cpp:expr:`lv_tabview_rename_tab(tabview, tab_id, "New Name")`. +:cpp:expr:`lv_tabview_set_tab_text(tabview, tab_id, "New Name")`. Navigating to a new tab ----------------------- diff --git a/lvgl.h b/lvgl.h index 41b1ec99bd..140a934e1f 100644 --- a/lvgl.h +++ b/lvgl.h @@ -141,6 +141,7 @@ extern "C" { #include "src/lv_api_map_v9_1.h" #include "src/lv_api_map_v9_2.h" #include "src/lv_api_map_v9_3.h" + #include "src/lv_api_map_v9_4.h" #endif /*LV_DISABLE_API_MAPPING*/ #if LV_USE_PRIVATE_API diff --git a/src/lv_api_map_v9_4.h b/src/lv_api_map_v9_4.h new file mode 100644 index 0000000000..9978e90245 --- /dev/null +++ b/src/lv_api_map_v9_4.h @@ -0,0 +1,40 @@ +/** + * @file lv_api_map_v9_4.h + * + */ + +#ifndef LV_API_MAP_V9_4_H +#define LV_API_MAP_V9_4_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#include "misc/lv_types.h" + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/********************** + * MACROS + **********************/ + +#define lv_tabview_rename_tab lv_tabview_set_tab_text + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /* LV_API_MAP_V9_4_H */ diff --git a/src/widgets/list/lv_list.c b/src/widgets/list/lv_list.c index e30bb5f838..3da8be76b6 100644 --- a/src/widgets/list/lv_list.c +++ b/src/widgets/list/lv_list.c @@ -134,6 +134,42 @@ void lv_list_set_button_text(lv_obj_t * list, lv_obj_t * btn, const char * txt) } } +#if LV_USE_TRANSLATION + +lv_obj_t * lv_list_add_translation_tag(lv_obj_t * list, const char * tag) +{ + LV_LOG_INFO("begin"); + + lv_obj_t * obj = lv_list_add_text(list, NULL); + lv_label_set_translation_tag(obj, tag); + return obj; +} + +lv_obj_t * lv_list_add_button_translation_tag(lv_obj_t * list, const void * icon, const char * tag) +{ + LV_LOG_INFO("begin"); + + lv_obj_t * obj = lv_list_add_button(list, icon, ""); + lv_list_set_button_translation_tag(list, obj, tag); + + return obj; +} + +void lv_list_set_button_translation_tag(lv_obj_t * list, lv_obj_t * btn, const char * tag) +{ + LV_UNUSED(list); + uint32_t i; + for(i = 0; i < lv_obj_get_child_count(btn); i++) { + lv_obj_t * child = lv_obj_get_child(btn, i); + if(lv_obj_check_type(child, &lv_label_class)) { + lv_label_set_translation_tag(child, tag); + return; + } + } +} + +#endif + /********************** * STATIC FUNCTIONS **********************/ diff --git a/src/widgets/list/lv_list.h b/src/widgets/list/lv_list.h index 0c4ffed1e4..19de8945b9 100644 --- a/src/widgets/list/lv_list.h +++ b/src/widgets/list/lv_list.h @@ -76,6 +76,35 @@ const char * lv_list_get_button_text(lv_obj_t * list, lv_obj_t * btn); */ void lv_list_set_button_text(lv_obj_t * list, lv_obj_t * btn, const char * txt); +#if LV_USE_TRANSLATION + +/** + * Add translation tag text to a list + * @param list pointer to a list, it will be the parent of the new label + * @param tag translation tag of the new label + * @return pointer to the created label + */ +lv_obj_t * lv_list_add_translation_tag(lv_obj_t * list, const char * tag); + +/** + * Add translation tag button to a list + * @param list pointer to a list, it will be the parent of the new button + * @param icon icon for the button, when NULL it will have no icon + * @param tag translation tag of the new button, when NULL no translation tag will be added + * @return pointer to the created button + */ +lv_obj_t * lv_list_add_button_translation_tag(lv_obj_t * list, const void * icon, const char * tag); + +/** + * Set translation tag text of a given list button + * @param list pointer to a list + * @param btn pointer to the button + * @param tag pointer to the translation tag + */ +void lv_list_set_button_translation_tag(lv_obj_t * list, lv_obj_t * btn, const char * tag); + +#endif + /********************** * MACROS **********************/ diff --git a/src/widgets/tabview/lv_tabview.c b/src/widgets/tabview/lv_tabview.c index 94676ebf27..d95c03eee8 100644 --- a/src/widgets/tabview/lv_tabview.c +++ b/src/widgets/tabview/lv_tabview.c @@ -97,7 +97,7 @@ lv_obj_t * lv_tabview_add_tab(lv_obj_t * obj, const char * name) return page; } -void lv_tabview_rename_tab(lv_obj_t * obj, uint32_t idx, const char * new_name) +void lv_tabview_set_tab_text(lv_obj_t * obj, uint32_t idx, const char * new_name) { LV_ASSERT_OBJ(obj, MY_CLASS); @@ -107,6 +107,22 @@ void lv_tabview_rename_tab(lv_obj_t * obj, uint32_t idx, const char * new_name) lv_label_set_text(label, new_name); } +#if LV_USE_TRANSLATION + +lv_obj_t * lv_tabview_set_tab_translation_tag(lv_obj_t * obj, const char * tag) +{ + LV_ASSERT_OBJ(obj, MY_CLASS); + + lv_obj_t * page = lv_tabview_add_tab(obj, NULL); + lv_obj_t * button = lv_tabview_get_tab_button(obj, -1); + lv_obj_t * label = lv_obj_get_child_by_type(button, 0, &lv_label_class); + lv_label_set_translation_tag(label, tag); + + return page; +} + +#endif + void lv_tabview_set_active(lv_obj_t * obj, uint32_t idx, lv_anim_enable_t anim_en) { LV_ASSERT_OBJ(obj, MY_CLASS); diff --git a/src/widgets/tabview/lv_tabview.h b/src/widgets/tabview/lv_tabview.h index 911c886a96..cb343647fa 100644 --- a/src/widgets/tabview/lv_tabview.h +++ b/src/widgets/tabview/lv_tabview.h @@ -49,7 +49,19 @@ lv_obj_t * lv_tabview_add_tab(lv_obj_t * obj, const char * name); * @param idx the index of the tab to rename * @param new_name the new name as a string */ -void lv_tabview_rename_tab(lv_obj_t * obj, uint32_t idx, const char * new_name); +void lv_tabview_set_tab_text(lv_obj_t * obj, uint32_t idx, const char * new_name); + +#if LV_USE_TRANSLATION + +/** + * Add a tab with a translation tag to the tabview. + * @param obj pointer to a tabview widget + * @param tag translation key used for the tab label; will be displayed on the tab bar + * @return the widget where the content of the tab can be created + */ +lv_obj_t * lv_tabview_set_tab_translation_tag(lv_obj_t * obj, const char * tag); + +#endif /** * Show a tab diff --git a/tests/src/test_cases/widgets/test_list.c b/tests/src/test_cases/widgets/test_list.c index 64d01d9d6e..31edc9de2f 100644 --- a/tests/src/test_cases/widgets/test_list.c +++ b/tests/src/test_cases/widgets/test_list.c @@ -62,4 +62,62 @@ void test_list_snapshot(void) TEST_ASSERT_EQUAL_SCREENSHOT("widgets/list_1.png"); } +void test_list_translation_tag(void) +{ + static const char * languages[] = {"en", "de", "es", NULL}; + static const char * tags[] = {"tiger", "lion", "rabbit", NULL}; + static const char * translations[] = { + "The Tiger", "Der Tiger", "El Tigre", + "The Lion", "Der Löwe", "El León", + "The Rabbit", "Das Kaninchen", "El Conejo" + }; + lv_translation_add_static(languages, tags, translations); + + lv_obj_t * list_text = lv_list_add_translation_tag(list, "tiger"); + + lv_translation_set_language("en"); + TEST_ASSERT_EQUAL_STRING(lv_label_get_text(list_text), "The Tiger"); + + lv_translation_set_language("de"); + TEST_ASSERT_EQUAL_STRING(lv_label_get_text(list_text), "Der Tiger"); + + lv_translation_set_language("es"); + TEST_ASSERT_EQUAL_STRING(lv_label_get_text(list_text), "El Tigre"); + + /* Unknown language translates to the tag */ + lv_translation_set_language("fr"); + TEST_ASSERT_EQUAL_STRING(lv_label_get_text(list_text), "tiger"); + + lv_obj_t * list_button = lv_list_add_button_translation_tag(list, NULL, "lion"); + + lv_translation_set_language("en"); + TEST_ASSERT_EQUAL_STRING(lv_list_get_button_text(list, list_button), "The Lion"); + + lv_translation_set_language("de"); + TEST_ASSERT_EQUAL_STRING(lv_list_get_button_text(list, list_button), "Der Löwe"); + + lv_translation_set_language("es"); + TEST_ASSERT_EQUAL_STRING(lv_list_get_button_text(list, list_button), "El León"); + + /* Unknown language translates to the tag */ + lv_translation_set_language("fr"); + TEST_ASSERT_EQUAL_STRING(lv_list_get_button_text(list, list_button), "lion"); + + lv_list_set_button_translation_tag(list, list_button, "rabbit"); + + lv_translation_set_language("en"); + TEST_ASSERT_EQUAL_STRING(lv_list_get_button_text(list, list_button), "The Rabbit"); + + lv_translation_set_language("de"); + TEST_ASSERT_EQUAL_STRING(lv_list_get_button_text(list, list_button), "Das Kaninchen"); + + lv_translation_set_language("es"); + TEST_ASSERT_EQUAL_STRING(lv_list_get_button_text(list, list_button), "El Conejo"); + + /* Unknown language translates to the tag */ + lv_translation_set_language("fr"); + TEST_ASSERT_EQUAL_STRING(lv_list_get_button_text(list, list_button), "rabbit"); + +} + #endif diff --git a/tests/src/test_cases/widgets/test_tabview.c b/tests/src/test_cases/widgets/test_tabview.c index 69517b9eaf..96758408e9 100644 --- a/tests/src/test_cases/widgets/test_tabview.c +++ b/tests/src/test_cases/widgets/test_tabview.c @@ -22,6 +22,7 @@ void test_tabview_get_tab_bar(void); void test_tabview_set_act_non_existent(void); void test_tabview_tab2_selected_event(void); void test_tabview_update_on_external_scroll(void); +void test_tabview_translation_tag(void); static lv_obj_t * active_screen = NULL; static lv_obj_t * tabview = NULL; @@ -180,7 +181,7 @@ void test_tabview_rename_tab(void) LV_UNUSED(tab3); /*rename 2nd tab (0-based index)*/ - lv_tabview_rename_tab(tabview, 1, "2nd Tab"); + lv_tabview_set_tab_text(tabview, 1, "2nd Tab"); TEST_ASSERT_EQUAL_SCREENSHOT("widgets/tabview_08.png"); } @@ -285,4 +286,32 @@ void test_tabview_update_on_external_scroll(void) TEST_ASSERT_EQUAL_UINT16(2, lv_tabview_get_tab_active(tabview)); } +void test_tabview_translation_tag(void) +{ + static const char * tags[] = {"tiger", NULL}; + static const char * languages[] = {"en", "de", "es", NULL}; + static const char * translations[] = { + "The Tiger", "Der Tiger", "El Tigre" + }; + + lv_translation_add_static(languages, tags, translations); + tabview = lv_tabview_create(active_screen); + lv_tabview_set_tab_translation_tag(tabview, "tiger"); + + lv_obj_t * label = lv_obj_get_child_by_type(lv_tabview_get_tab_button(tabview, 0), 0, &lv_label_class); + + lv_translation_set_language("en"); + TEST_ASSERT_EQUAL_STRING(lv_label_get_text(label), "The Tiger"); + + lv_translation_set_language("de"); + TEST_ASSERT_EQUAL_STRING(lv_label_get_text(label), "Der Tiger"); + + lv_translation_set_language("es"); + TEST_ASSERT_EQUAL_STRING(lv_label_get_text(label), "El Tigre"); + + /* Unknown language translates to the tag */ + lv_translation_set_language("fr"); + TEST_ASSERT_EQUAL_STRING(lv_label_get_text(label), "tiger"); +} + #endif