mirror of
https://github.com/lvgl/lvgl.git
synced 2026-05-26 02:37:01 +08:00
feat(label): implement api to bind a translation tag to a label (#8948)
Co-authored-by: Liam Howatt <30486941+liamHowatt@users.noreply.github.com>
This commit is contained in:
@@ -789,5 +789,65 @@ void test_label_wrap_mode_clip(void)
|
||||
lv_snprintf(buf, sizeof(buf), "widgets/label_wrap_clip.png");
|
||||
TEST_ASSERT_EQUAL_SCREENSHOT(buf);
|
||||
}
|
||||
void test_label_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);
|
||||
label = lv_label_create(NULL);
|
||||
lv_label_set_translation_tag(label, "tiger");
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
void test_label_setting_text_disables_translation(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);
|
||||
label = lv_label_create(NULL);
|
||||
lv_label_set_translation_tag(label, "tiger");
|
||||
|
||||
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");
|
||||
|
||||
/* Using set text should unbind the translation tag*/
|
||||
lv_label_set_text(label, "Hello world");
|
||||
lv_translation_set_language("de");
|
||||
TEST_ASSERT_EQUAL_STRING(lv_label_get_text(label), "Hello world");
|
||||
|
||||
lv_label_set_translation_tag(label, "tiger");
|
||||
TEST_ASSERT_EQUAL_STRING(lv_label_get_text(label), "Der Tiger");
|
||||
|
||||
/* Using set text static should unbind the translation tag*/
|
||||
lv_label_set_text_static(label, "Hello world");
|
||||
lv_translation_set_language("en");
|
||||
TEST_ASSERT_EQUAL_STRING(lv_label_get_text(label), "Hello world");
|
||||
lv_label_set_translation_tag(label, "tiger");
|
||||
TEST_ASSERT_EQUAL_STRING(lv_label_get_text(label), "The Tiger");
|
||||
|
||||
/* Using set text fmt should unbind the translation tag*/
|
||||
lv_label_set_text_fmt(label, "Hello world %d", 1);
|
||||
lv_translation_set_language("de");
|
||||
TEST_ASSERT_EQUAL_STRING(lv_label_get_text(label), "Hello world 1");
|
||||
lv_label_set_translation_tag(label, "tiger");
|
||||
TEST_ASSERT_EQUAL_STRING(lv_label_get_text(label), "Der Tiger");
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -62,5 +62,21 @@ void test_xml_label_with_attrs(void)
|
||||
|
||||
TEST_ASSERT_EQUAL_SCREENSHOT("xml/lv_label.png");
|
||||
}
|
||||
void test_xml_label_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);
|
||||
|
||||
lv_obj_t * scr = lv_screen_active();
|
||||
const char * label1_attrs[] = {
|
||||
"translation_tag", "tiger",
|
||||
NULL, NULL,
|
||||
};
|
||||
lv_obj_t * label = lv_xml_create(scr, "lv_label", label1_attrs);
|
||||
lv_translation_set_language("de");
|
||||
TEST_ASSERT_EQUAL_STRING(lv_label_get_text(label), "Der Tiger");
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user