awtk/demos/demo_tr_app.c

105 lines
2.8 KiB
C
Raw Normal View History

2018-05-04 14:52:15 +08:00
/**
* File: demo_tr_app.c
2018-05-15 09:31:58 +08:00
* Author: AWTK Develop Team
2018-05-04 14:52:15 +08:00
* Brief: text translation
*
2018-05-08 10:22:32 +08:00
* Copyright (c) 2018 - 2018 Guangzhou ZHIYUAN Electronics Co.,Ltd.
2018-05-04 14:52:15 +08:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* License file for more details.
*
*/
/**
* History:
* ================================================================
* 2018-05-03 Li XianJing <xianjimli@hotmail.com> created
*
*/
#include "base/timer.h"
#include "base/enums.h"
#include "base/button.h"
2018-08-24 07:45:37 +08:00
#include "base/locale_info.h"
2018-05-04 14:52:15 +08:00
#include "base/check_button.h"
#include "base/dialog.h"
#include "base/image.h"
#include "base/image_manager.h"
#include "base/label.h"
#include "base/mem.h"
#include "base/progress_bar.h"
#include "base/utils.h"
#include "base/utf8.h"
#include "base/window.h"
#include "base/slider.h"
#include "base/group_box.h"
2018-05-05 19:08:32 +08:00
static ret_t change_locale(void* ctx, event_t* e) {
2018-05-04 14:52:15 +08:00
char country[3];
char language[3];
const char* str = (const char*)ctx;
2018-06-16 11:40:13 +08:00
widget_t* widget = WIDGET(e->target);
2018-05-04 14:52:15 +08:00
if (widget_get_value(widget)) {
strncpy(language, str, 2);
2018-05-05 19:08:32 +08:00
strncpy(country, str + 3, 2);
2018-08-24 07:45:37 +08:00
locale_info_change(locale_info(), language, country);
2018-05-04 14:52:15 +08:00
}
return RET_OK;
}
2018-11-28 09:09:59 +08:00
static ret_t set_locale_value(widget_t* widget, int32_t value) {
char str[64];
const char* format = locale_info_tr(locale_info(), "value is %d");
tk_snprintf(str, sizeof(str), format, value);
widget_set_text_utf8(widget, str);
return RET_OK;
}
2018-05-05 19:08:32 +08:00
static ret_t on_locale_changed(void* ctx, event_t* e) {
(void)ctx;
(void)e;
2018-11-28 09:09:59 +08:00
widget_t* win = WIDGET(ctx);
widget_t* value = widget_lookup(win, "value", TRUE);
set_locale_value(value, 100);
2018-08-24 07:45:37 +08:00
log_debug("locale_infod change: %s_%s\n", locale_info()->language, locale_info()->country);
2018-05-05 19:08:32 +08:00
return RET_OK;
}
2018-05-04 14:52:15 +08:00
ret_t application_init() {
widget_t* ok = NULL;
widget_t* cancel = NULL;
2018-11-28 09:09:59 +08:00
widget_t* value = NULL;
2018-05-04 14:52:15 +08:00
widget_t* radio_button = NULL;
widget_t* win = window_create(NULL, 0, 0, 0, 0);
2018-05-05 19:08:32 +08:00
widget_on(win, EVT_LOCALE_CHANGED, on_locale_changed, win);
2018-05-04 14:52:15 +08:00
ok = button_create(win, 10, 5, 80, 30);
widget_set_tr_text(ok, "ok");
cancel = button_create(win, 100, 5, 80, 30);
widget_set_tr_text(cancel, "cancel");
2018-11-28 09:09:59 +08:00
value = label_create(win, 200, 5, 80, 30);
widget_set_name(value, "value");
set_locale_value(value, 100);
2018-05-04 14:52:15 +08:00
radio_button = check_button_create_radio(win, 10, 200, 80, 30);
widget_set_tr_text(radio_button, "English");
2018-05-05 19:08:32 +08:00
widget_on(radio_button, EVT_VALUE_CHANGED, change_locale, "en_US");
2018-05-04 14:52:15 +08:00
widget_set_value(radio_button, 1);
radio_button = check_button_create_radio(win, 100, 200, 80, 30);
widget_set_tr_text(radio_button, "Chinese");
2018-05-05 19:08:32 +08:00
widget_on(radio_button, EVT_VALUE_CHANGED, change_locale, "zh_CN");
2018-05-04 14:52:15 +08:00
return RET_OK;
}