feat(translate): support translation tags in tabview and list (#9079)

Signed-off-by: lhdjply <lhdjply@126.com>
This commit is contained in:
Liu Yi
2025-11-04 22:23:19 +08:00
committed by GitHub
parent cd59da95e1
commit 10234d0cb4
9 changed files with 225 additions and 4 deletions

View File

@@ -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
-----------------------

1
lvgl.h
View File

@@ -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

40
src/lv_api_map_v9_4.h Normal file
View File

@@ -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 */

View File

@@ -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
**********************/

View File

@@ -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
**********************/

View File

@@ -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);

View File

@@ -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

View File

@@ -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

View File

@@ -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