From 2f40b9804022cafeb41a53a031051bfda9ee194d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Costa?= Date: Fri, 26 Sep 2025 13:07:28 +0200 Subject: [PATCH] feat(label): implement api to bind a translation tag to a label (#8948) Co-authored-by: Liam Howatt <30486941+liamHowatt@users.noreply.github.com> --- .../details/auxiliary-modules/translation.rst | 9 ++ docs/src/details/widgets/label.rst | 7 + .../src/details/xml/features/translations.rst | 18 +-- examples/others/xml/my_card.xml | 2 +- examples/widgets/label/index.rst | 7 + examples/widgets/label/lv_example_label_7.c | 59 ++++++++ examples/widgets/lv_example_widgets.h | 1 + src/others/xml/parsers/lv_xml_label_parser.c | 3 +- src/widgets/label/lv_label.c | 137 +++++++++++++----- src/widgets/label/lv_label.h | 13 ++ src/widgets/label/lv_label_private.h | 3 + tests/src/test_cases/widgets/test_label.c | 60 ++++++++ tests/src/test_cases/xml/test_xml_label.c | 16 ++ xmls/lv_label.xml | 2 +- 14 files changed, 276 insertions(+), 61 deletions(-) create mode 100644 examples/widgets/label/lv_example_label_7.c diff --git a/docs/src/details/auxiliary-modules/translation.rst b/docs/src/details/auxiliary-modules/translation.rst index 2503226d4e..0f0391d22f 100644 --- a/docs/src/details/auxiliary-modules/translation.rst +++ b/docs/src/details/auxiliary-modules/translation.rst @@ -76,6 +76,8 @@ These return a translated string which can be used with widgets: lv_dropdown_set_options(dd, lv_tr("color_list")); +.. _translation_fallbacks: + Fallbacks --------- @@ -89,6 +91,8 @@ Dynamically Updating UI Text When :cpp:expr:`lv_translation_set_language("language")` is called, LVGL sends ``LV_EVENT_TRANSLATION_LANGUAGE_CHANGED`` to every widget, allowing you to update text automatically. +The new language can be retrieved by either calling :cpp:expr:`lv_translation_get_language()` or by getting the event parameter in the event callback with :cpp:expr:`lv_event_get_param(e)` + Basic Example ------------- @@ -99,6 +103,11 @@ Basic Example lv_obj_t * label = lv_event_get_target_obj(e); const char * tag = (const char *) lv_event_get_user_data(e); lv_label_set_text(label, lv_tr(tag)); + + /* You can get the new language with `lv_event_get_param`*/ + /* const char * language = (const char *) lv_event_get_param(e); */ + /* or with `lv_translation_get_language` */ + /* const char * language = lv_translation_get_language(); */ } lv_obj_t * label = lv_label_create(lv_screen_active()); diff --git a/docs/src/details/widgets/label.rst b/docs/src/details/widgets/label.rst index b72881c7ec..1874718281 100644 --- a/docs/src/details/widgets/label.rst +++ b/docs/src/details/widgets/label.rst @@ -76,6 +76,13 @@ they are stored in ROM memory, which is always accessible. realloc() will be forced every time the length of the string changes. That MCU overhead can be avoided by doing the above. +Set translation tag +------------------- + +When using LVGL's translation module, you can bind a translation tag to a label directly with :cpp:expr:`lv_label_set_translation_tag(label, tag)`. +After this function is called, future changes to the language will automatically update the label's text to display the corresponding translation +for that tag in the new language. + .. _lv_label_newline: Newline diff --git a/docs/src/details/xml/features/translations.rst b/docs/src/details/xml/features/translations.rst index 99dd82310a..c8ceebdb8b 100644 --- a/docs/src/details/xml/features/translations.rst +++ b/docs/src/details/xml/features/translations.rst @@ -34,9 +34,7 @@ e.g., ``languages="en de hu"``. Language codes are free-form, but ISO-style code Each ```` defines a ``tag``, which acts as the lookup key, and attributes for each language. -Translations may be omitted --- fallbacks will be applied when needed. - -TODO: link to more information about translation fallbacks: xml_translations_fallbacks +Translations may be omitted --- fallbacks will be applied when needed. See :ref:`translation_fallbacks` for more details. To register XML translations: @@ -44,17 +42,3 @@ To register XML translations: - :cpp:expr:`lv_xml_translation_register_from_data(xml_string)` Multiple XML sources can be registered; they will be merged and searched collectively. - - - -Usage in XML -************ - -Some widget properties support a ``*-translated`` suffix to refer to translation tags. For example: - -.. code-block:: xml - - - -This sets the label's text to the translated string for ``"dog"``. - diff --git a/examples/others/xml/my_card.xml b/examples/others/xml/my_card.xml index 3a158b996c..1c6c9c83d3 100644 --- a/examples/others/xml/my_card.xml +++ b/examples/others/xml/my_card.xml @@ -25,7 +25,7 @@ - +