make locale_t scriptable

This commit is contained in:
xianjimli 2018-08-08 18:28:04 +08:00
parent 70c243e4d4
commit d723e09a53
13 changed files with 496 additions and 482 deletions

View File

@ -22,7 +22,7 @@
#include "base/timer.h" #include "base/timer.h"
#include "base/enums.h" #include "base/enums.h"
#include "base/button.h" #include "base/button.h"
#include "base/locale.h" #include "base/tklocale.h"
#include "base/check_button.h" #include "base/check_button.h"
#include "base/dialog.h" #include "base/dialog.h"
#include "base/image.h" #include "base/image.h"
@ -45,7 +45,7 @@ static ret_t change_locale(void* ctx, event_t* e) {
if (widget_get_value(widget)) { if (widget_get_value(widget)) {
strncpy(language, str, 2); strncpy(language, str, 2);
strncpy(country, str + 3, 2); strncpy(country, str + 3, 2);
locale_change(locale(), language, country); tklocale_change(tklocale(), language, country);
} }
return RET_OK; return RET_OK;
@ -54,7 +54,7 @@ static ret_t change_locale(void* ctx, event_t* e) {
static ret_t on_locale_changed(void* ctx, event_t* e) { static ret_t on_locale_changed(void* ctx, event_t* e) {
(void)ctx; (void)ctx;
(void)e; (void)e;
log_debug("localed change: %s_%s\n", locale()->language, locale()->country); log_debug("tklocaled change: %s_%s\n", tklocale()->language, tklocale()->country);
return RET_OK; return RET_OK;
} }

View File

@ -21,7 +21,7 @@
#include "base/mem.h" #include "base/mem.h"
#include "base/label.h" #include "base/label.h"
#include "base/locale.h" #include "base/tklocale.h"
#include "base/timer.h" #include "base/timer.h"
#include "base/button.h" #include "base/button.h"
#include "base/dialog.h" #include "base/dialog.h"
@ -161,7 +161,7 @@ static ret_t on_change_locale(void* ctx, event_t* e) {
tk_strncpy(language, str, 2); tk_strncpy(language, str, 2);
tk_strncpy(country, str + 3, 2); tk_strncpy(country, str + 3, 2);
locale_change(locale(), language, country); tklocale_change(tklocale(), language, country);
return RET_OK; return RET_OK;
} }

View File

@ -24,7 +24,7 @@
#include "base/idle.h" #include "base/idle.h"
#include "base/time.h" #include "base/time.h"
#include "base/timer.h" #include "base/timer.h"
#include "base/locale.h" #include "base/tklocale.h"
#include "base/platform.h" #include "base/platform.h"
#include "base/main_loop.h" #include "base/main_loop.h"
#include "font/font_bitmap.h" #include "font/font_bitmap.h"
@ -93,7 +93,7 @@ ret_t tk_init_internal(void) {
return_value_if_fail(input_method_set(input_method_create()) == RET_OK, RET_FAIL); return_value_if_fail(input_method_set(input_method_create()) == RET_OK, RET_FAIL);
return_value_if_fail(widget_factory_set(widget_factory_create()) == RET_OK, RET_FAIL); return_value_if_fail(widget_factory_set(widget_factory_create()) == RET_OK, RET_FAIL);
return_value_if_fail(resource_manager_set(resource_manager_create(30)) == RET_OK, RET_FAIL); return_value_if_fail(resource_manager_set(resource_manager_create(30)) == RET_OK, RET_FAIL);
return_value_if_fail(locale_set(locale_create(NULL, NULL)) == RET_OK, RET_FAIL); return_value_if_fail(tklocale_set(tklocale_create(NULL, NULL)) == RET_OK, RET_FAIL);
return_value_if_fail(font_manager_set(font_manager_create()) == RET_OK, RET_FAIL); return_value_if_fail(font_manager_set(font_manager_create()) == RET_OK, RET_FAIL);
return_value_if_fail(image_manager_set(image_manager_create(loader)) == RET_OK, RET_FAIL); return_value_if_fail(image_manager_set(image_manager_create(loader)) == RET_OK, RET_FAIL);
return_value_if_fail(window_manager_set(window_manager_create()) == RET_OK, RET_FAIL); return_value_if_fail(window_manager_set(window_manager_create()) == RET_OK, RET_FAIL);
@ -114,8 +114,8 @@ ret_t tk_deinit_internal(void) {
font_manager_destroy(font_manager()); font_manager_destroy(font_manager());
font_manager_set(NULL); font_manager_set(NULL);
locale_destroy(locale()); tklocale_destroy(tklocale());
locale_set(NULL); tklocale_set(NULL);
resource_manager_destroy(resource_manager()); resource_manager_destroy(resource_manager());
resource_manager_set(NULL); resource_manager_set(NULL);

View File

@ -24,7 +24,7 @@
#include "base/utils.h" #include "base/utils.h"
#include "base/enums.h" #include "base/enums.h"
#include "base/pages.h" #include "base/pages.h"
#include "base/locale.h" #include "base/tklocale.h"
#include "base/keyboard.h" #include "base/keyboard.h"
#include "base/input_method.h" #include "base/input_method.h"
#include "base/window_manager.h" #include "base/window_manager.h"
@ -185,7 +185,7 @@ static ret_t keyboard_on_button_click(void* ctx, event_t* e) {
} }
static ret_t keyboard_update_action_buton_info(widget_t* button, const char* text, bool_t enable) { static ret_t keyboard_update_action_buton_info(widget_t* button, const char* text, bool_t enable) {
text = locale_tr(locale(), ((text && *text) ? text : STR_RETURN)); text = tklocale_tr(tklocale(), ((text && *text) ? text : STR_RETURN));
widget_set_text_utf8(button, text); widget_set_text_utf8(button, text);
widget_set_enable(button, enable); widget_set_enable(button, enable);

View File

@ -1,168 +0,0 @@
/**
* File: locale.c
* Author: AWTK Develop Team
* Brief: locale
*
* Copyright (c) 2018 - 2018 Guangzhou ZHIYUAN Electronics Co.,Ltd.
*
* 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-02 Li XianJing <xianjimli@hotmail.com> created
*
*/
#include "base/mem.h"
#include "base/utils.h"
#include "base/locale.h"
static tklocale_t* s_locale = NULL;
tklocale_t* locale() {
return s_locale;
}
ret_t locale_set(tklocale_t* locale) {
s_locale = locale;
return RET_OK;
}
tklocale_t* locale_create(const char* language, const char* country) {
tklocale_t* locale = TKMEM_ZALLOC(tklocale_t);
return locale_init(locale, language, country);
}
tklocale_t* locale_init(tklocale_t* locale, const char* language, const char* country) {
return_value_if_fail(locale != NULL, NULL);
return_value_if_fail(emitter_init(&(locale->emitter)) != NULL, NULL);
locale_change(locale, language, country);
return locale;
}
const char* locale_tr(tklocale_t* locale, const char* text) {
str_table_t* table = NULL;
const char* tr_text = NULL;
return_value_if_fail(locale != NULL && text != NULL, text);
return_value_if_fail(locale->strs != NULL, text);
table = (str_table_t*)(locale->strs->data);
tr_text = str_table_lookup(table, text);
return tr_text != NULL ? tr_text : text;
}
#define DEFAULT_COUNTRY "US"
#define DEFAULT_LANGUAGE "en"
ret_t locale_change(tklocale_t* locale, const char* language, const char* country) {
return_value_if_fail(locale != NULL, RET_BAD_PARAMS);
if (country == NULL) {
country = DEFAULT_COUNTRY;
}
if (language == NULL) {
language = DEFAULT_LANGUAGE;
}
if (strcmp(locale->country, country) != 0 || strcmp(locale->language, language) != 0) {
char name[16];
event_t e = event_init(EVT_LOCALE_CHANGED, locale);
tk_strncpy(locale->country, country, sizeof(locale->country) - 1);
tk_strncpy(locale->language, language, sizeof(locale->language) - 1);
if (locale->strs != NULL) {
resource_manager_unref(resource_manager(), locale->strs);
locale->strs = NULL;
/*清除字符串缓存*/
resource_manager_clear_cache(resource_manager(), RESOURCE_TYPE_STRINGS);
}
tk_snprintf(name, sizeof(name) - 1, "%s_%s", locale->language, locale->country);
locale->strs = resource_manager_ref(resource_manager(), RESOURCE_TYPE_STRINGS, name);
if (locale->strs == NULL) {
locale->strs =
resource_manager_ref(resource_manager(), RESOURCE_TYPE_STRINGS, locale->language);
}
emitter_dispatch(&(locale->emitter), &e);
}
return RET_OK;
}
uint32_t locale_on(tklocale_t* locale, event_type_t type, event_func_t on_event, void* ctx) {
return_value_if_fail(locale != NULL && on_event != NULL, 0);
type = EVT_LOCALE_CHANGED;
return emitter_on(&(locale->emitter), type, on_event, ctx);
}
ret_t locale_off(tklocale_t* locale, uint32_t id) {
return_value_if_fail(locale != NULL, RET_BAD_PARAMS);
return emitter_off(&(locale->emitter), id);
}
ret_t locale_deinit(tklocale_t* locale) {
return_value_if_fail(locale != NULL, RET_OK);
emitter_deinit(&(locale->emitter));
if (locale->strs != NULL) {
resource_manager_unref(resource_manager(), locale->strs);
}
memset(locale, 0x00, sizeof(tklocale_t));
return RET_OK;
}
ret_t locale_destroy(tklocale_t* locale) {
return_value_if_fail(locale != NULL, RET_BAD_PARAMS);
locale_deinit(locale);
TKMEM_FREE(locale);
return RET_OK;
}
const char* str_table_lookup(const str_table_t* table, const char* key) {
int low = 0;
int mid = 0;
int high = 0;
int result = 0;
const char* iter = NULL;
const char* start = (const char*)table;
return_value_if_fail(table != NULL && table->nr > 0 && key != NULL, NULL);
high = table->nr - 1;
while (low <= high) {
mid = low + ((high - low) >> 1);
iter = start + table->strs[mid].key;
result = strcmp(iter, key);
if (result == 0) {
return start + table->strs[mid].value;
} else if (result < 0) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return NULL;
}

168
src/base/tklocale.c Normal file
View File

@ -0,0 +1,168 @@
/**
* File: tklocale.c
* Author: AWTK Develop Team
* Brief: tklocale
*
* Copyright (c) 2018 - 2018 Guangzhou ZHIYUAN Electronics Co.,Ltd.
*
* 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-02 Li XianJing <xianjimli@hotmail.com> created
*
*/
#include "base/mem.h"
#include "base/utils.h"
#include "base/tklocale.h"
static tklocale_t* s_locale = NULL;
tklocale_t* tklocale() {
return s_locale;
}
ret_t tklocale_set(tklocale_t* tklocale) {
s_locale = tklocale;
return RET_OK;
}
tklocale_t* tklocale_create(const char* language, const char* country) {
tklocale_t* tklocale = TKMEM_ZALLOC(tklocale_t);
return tklocale_init(tklocale, language, country);
}
tklocale_t* tklocale_init(tklocale_t* tklocale, const char* language, const char* country) {
return_value_if_fail(tklocale != NULL, NULL);
tklocale->emitter = emitter_create();
tklocale_change(tklocale, language, country);
return tklocale;
}
const char* tklocale_tr(tklocale_t* tklocale, const char* text) {
str_table_t* table = NULL;
const char* tr_text = NULL;
return_value_if_fail(tklocale != NULL && text != NULL, text);
return_value_if_fail(tklocale->strs != NULL, text);
table = (str_table_t*)(tklocale->strs->data);
tr_text = str_table_lookup(table, text);
return tr_text != NULL ? tr_text : text;
}
#define DEFAULT_COUNTRY "US"
#define DEFAULT_LANGUAGE "en"
ret_t tklocale_change(tklocale_t* tklocale, const char* language, const char* country) {
return_value_if_fail(tklocale != NULL, RET_BAD_PARAMS);
if (country == NULL) {
country = DEFAULT_COUNTRY;
}
if (language == NULL) {
language = DEFAULT_LANGUAGE;
}
if (strcmp(tklocale->country, country) != 0 || strcmp(tklocale->language, language) != 0) {
char name[16];
event_t e = event_init(EVT_LOCALE_CHANGED, tklocale);
tk_strncpy(tklocale->country, country, sizeof(tklocale->country) - 1);
tk_strncpy(tklocale->language, language, sizeof(tklocale->language) - 1);
if (tklocale->strs != NULL) {
resource_manager_unref(resource_manager(), tklocale->strs);
tklocale->strs = NULL;
/*清除字符串缓存*/
resource_manager_clear_cache(resource_manager(), RESOURCE_TYPE_STRINGS);
}
tk_snprintf(name, sizeof(name) - 1, "%s_%s", tklocale->language, tklocale->country);
tklocale->strs = resource_manager_ref(resource_manager(), RESOURCE_TYPE_STRINGS, name);
if (tklocale->strs == NULL) {
tklocale->strs =
resource_manager_ref(resource_manager(), RESOURCE_TYPE_STRINGS, tklocale->language);
}
emitter_dispatch(tklocale->emitter, &e);
}
return RET_OK;
}
uint32_t tklocale_on(tklocale_t* tklocale, event_type_t type, event_func_t on_event, void* ctx) {
return_value_if_fail(tklocale != NULL && on_event != NULL, 0);
type = EVT_LOCALE_CHANGED;
return emitter_on(tklocale->emitter, type, on_event, ctx);
}
ret_t tklocale_off(tklocale_t* tklocale, uint32_t id) {
return_value_if_fail(tklocale != NULL, RET_BAD_PARAMS);
return emitter_off(tklocale->emitter, id);
}
ret_t tklocale_deinit(tklocale_t* tklocale) {
return_value_if_fail(tklocale != NULL, RET_OK);
emitter_destroy(tklocale->emitter);
if (tklocale->strs != NULL) {
resource_manager_unref(resource_manager(), tklocale->strs);
}
memset(tklocale, 0x00, sizeof(tklocale_t));
return RET_OK;
}
ret_t tklocale_destroy(tklocale_t* tklocale) {
return_value_if_fail(tklocale != NULL, RET_BAD_PARAMS);
tklocale_deinit(tklocale);
TKMEM_FREE(tklocale);
return RET_OK;
}
const char* str_table_lookup(const str_table_t* table, const char* key) {
int low = 0;
int mid = 0;
int high = 0;
int result = 0;
const char* iter = NULL;
const char* start = (const char*)table;
return_value_if_fail(table != NULL && table->nr > 0 && key != NULL, NULL);
high = table->nr - 1;
while (low <= high) {
mid = low + ((high - low) >> 1);
iter = start + table->strs[mid].key;
result = strcmp(iter, key);
if (result == 0) {
return start + table->strs[mid].value;
} else if (result < 0) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return NULL;
}

View File

@ -1,7 +1,7 @@
/** /**
* File: locale.h * File: tklocale.h
* Author: AWTK Develop Team * Author: AWTK Develop Team
* Brief: locale * Brief: tklocale
* *
* Copyright (c) 2018 - 2018 Guangzhou ZHIYUAN Electronics Co.,Ltd. * Copyright (c) 2018 - 2018 Guangzhou ZHIYUAN Electronics Co.,Ltd.
* *
@ -28,8 +28,9 @@
BEGIN_C_DECLS BEGIN_C_DECLS
/** /**
* @class locale_t * @class tklocale_t
* locale * @annotation ["scriptable"]
* tklocale
*/ */
typedef struct _tklocale_t { typedef struct _tklocale_t {
/** /**
@ -46,112 +47,116 @@ typedef struct _tklocale_t {
char language[3]; char language[3];
const resource_info_t* strs; const resource_info_t* strs;
emitter_t emitter;
emitter_t* emitter;
} tklocale_t; } tklocale_t;
/** /**
* @method locale * @method tklocale
* locale * tklocale
* @annotation ["constructor"] * @alias tklocale_instance
* @annotation ["constructor", "scriptable"]
* *
* @return {tklocale_t*} locale对象 * @return {tklocale_t*} tklocale对象
*/ */
tklocale_t* locale(void); tklocale_t* tklocale(void);
/** /**
* @method locale_set * @method tklocale_set
* locale * tklocale
* @param {tklocale_t*} locale locale对象 * @param {tklocale_t*} tklocale tklocale对象
* *
* @return {ret_t} RET_OK表示成功 * @return {ret_t} RET_OK表示成功
*/ */
ret_t locale_set(tklocale_t* locale); ret_t tklocale_set(tklocale_t* tklocale);
/** /**
* @method locale_create * @method tklocale_create
* locale * tklocale
* @annotation ["constructor"] * @annotation ["constructor"]
* @param {char*} language * @param {char*} language
* @param {char*} country * @param {char*} country
* *
* @return {tklocale_t*} locale对象 * @return {tklocale_t*} tklocale对象
*/ */
tklocale_t* locale_create(const char* language, const char* country); tklocale_t* tklocale_create(const char* language, const char* country);
/** /**
* @method locale_init * @method tklocale_init
* locale * tklocale
* @annotation ["constructor"] * @annotation ["constructor"]
* @param {tklocale_t*} locale locale对象 * @param {tklocale_t*} tklocale tklocale对象
* @param {char*} language * @param {char*} language
* @param {char*} country * @param {char*} country
* *
* @return {tklocale_t*} locale对象 * @return {tklocale_t*} tklocale对象
*/ */
tklocale_t* locale_init(tklocale_t* locale, const char* language, const char* country); tklocale_t* tklocale_init(tklocale_t* tklocale, const char* language, const char* country);
/** /**
* @method locale_tr * @method tklocale_tr
* *
* @param {tklocale_t*} locale locale对象 * @annotation ["scriptable"]
* @param {tklocale_t*} tklocale tklocale对象
* @param {char*} text * @param {char*} text
* *
* @return {char*} * @return {char*}
*/ */
const char* locale_tr(tklocale_t* locale, const char* text); const char* tklocale_tr(tklocale_t* tklocale, const char* text);
/** /**
* @method locale_change * @method tklocale_change
* *
* @param {tklocale_t*} locale locale对象 * @annotation ["scriptable"]
* @param {tklocale_t*} tklocale tklocale对象
* @param {char*} language * @param {char*} language
* @param {char*} country * @param {char*} country
* *
* @return {ret_t} RET_OK表示成功 * @return {ret_t} RET_OK表示成功
*/ */
ret_t locale_change(tklocale_t* locale, const char* language, const char* country); ret_t tklocale_change(tklocale_t* tklocale, const char* language, const char* country);
/** /**
* @method locale_on * @method tklocale_on
* *
* @annotation ["scriptable:custom"] * @annotation ["scriptable:custom"]
* @param {tklocale_t*} locale * @param {tklocale_t*} tklocale
* @param {event_type_t} type EVT_LOCALE_CHANGED * @param {event_type_t} type EVT_LOCALE_CHANGED
* @param {event_func_t} on_event * @param {event_func_t} on_event
* @param {void*} ctx * @param {void*} ctx
* *
* @return {uint32_t} idlocale_off * @return {uint32_t} idtklocale_off
*/ */
uint32_t locale_on(tklocale_t* locale, event_type_t type, event_func_t on_event, void* ctx); uint32_t tklocale_on(tklocale_t* tklocale, event_type_t type, event_func_t on_event, void* ctx);
/** /**
* @method locale_off * @method tklocale_off
* *
* @annotation ["scriptable:custom"] * @annotation ["scriptable:custom"]
* @param {tklocale_t*} locale * @param {tklocale_t*} tklocale
* @param {uint32_t} id locale_on返回的ID * @param {uint32_t} id tklocale_on返回的ID
* *
* @return {ret_t} RET_OK表示成功 * @return {ret_t} RET_OK表示成功
*/ */
ret_t locale_off(tklocale_t* locale, uint32_t id); ret_t tklocale_off(tklocale_t* tklocale, uint32_t id);
/** /**
* @method locale_deinit * @method tklocale_deinit
* @param {tklocale_t*} locale locale对象 * @param {tklocale_t*} tklocale tklocale对象
* *
* *
* @return {ret_t} RET_OK表示成功 * @return {ret_t} RET_OK表示成功
*/ */
ret_t locale_deinit(tklocale_t* locale); ret_t tklocale_deinit(tklocale_t* tklocale);
/** /**
* @method locale_destroy * @method tklocale_destroy
* @param {tklocale_t*} locale locale对象 * @param {tklocale_t*} tklocale tklocale对象
* locale对象 * tklocale对象
* *
* @return {ret_t} RET_OK表示成功 * @return {ret_t} RET_OK表示成功
*/ */
ret_t locale_destroy(tklocale_t* locale); ret_t tklocale_destroy(tklocale_t* tklocale);
typedef struct _str_pair_t { typedef struct _str_pair_t {
uint32_t key; uint32_t key;

View File

@ -24,7 +24,7 @@
#include "base/utf8.h" #include "base/utf8.h"
#include "base/utils.h" #include "base/utils.h"
#include "base/enums.h" #include "base/enums.h"
#include "base/locale.h" #include "base/tklocale.h"
#include "base/widget.h" #include "base/widget.h"
#include "base/layout.h" #include "base/layout.h"
#include "base/main_loop.h" #include "base/main_loop.h"
@ -129,7 +129,7 @@ ret_t widget_set_tr_text(widget_t* widget, const char* text) {
const char* tr_text = NULL; const char* tr_text = NULL;
return_value_if_fail(widget != NULL && text != NULL, RET_OK); return_value_if_fail(widget != NULL && text != NULL, RET_OK);
tr_text = locale_tr(locale(), text); tr_text = tklocale_tr(tklocale(), text);
TKMEM_FREE(widget->tr_text); TKMEM_FREE(widget->tr_text);
widget->tr_text = tk_strdup(text); widget->tr_text = tk_strdup(text);
@ -139,7 +139,7 @@ ret_t widget_set_tr_text(widget_t* widget, const char* text) {
ret_t widget_re_translate_text(widget_t* widget) { ret_t widget_re_translate_text(widget_t* widget) {
if (widget->tr_text != NULL) { if (widget->tr_text != NULL) {
value_t v; value_t v;
const char* tr_text = locale_tr(locale(), widget->tr_text); const char* tr_text = tklocale_tr(tklocale(), widget->tr_text);
widget_set_prop(widget, WIDGET_PROP_TEXT, value_set_str(&v, tr_text)); widget_set_prop(widget, WIDGET_PROP_TEXT, value_set_str(&v, tr_text));
widget_invalidate(widget, NULL); widget_invalidate(widget, NULL);
} }

View File

@ -26,7 +26,7 @@
#include "base/utils.h" #include "base/utils.h"
#include "base/timer.h" #include "base/timer.h"
#include "base/layout.h" #include "base/layout.h"
#include "base/locale.h" #include "base/tklocale.h"
#include "base/system_info.h" #include "base/system_info.h"
#include "base/window_manager.h" #include "base/window_manager.h"
@ -440,7 +440,7 @@ widget_t* window_manager_init(window_manager_t* wm) {
widget_init(w, NULL, &s_window_manager_vtable, 0, 0, 0, 0); widget_init(w, NULL, &s_window_manager_vtable, 0, 0, 0, 0);
locale_on(locale(), EVT_LOCALE_CHANGED, wm_on_locale_changed, wm); tklocale_on(tklocale(), EVT_LOCALE_CHANGED, wm_on_locale_changed, wm);
return w; return w;
} }

View File

@ -1,4 +1,4 @@
#include "base/locale.h" #include "base/tklocale.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <string> #include <string>
@ -8,10 +8,10 @@ using std::string;
static string s_locale; static string s_locale;
static ret_t test_on_locale_changed(void* ctx, event_t* e) { static ret_t test_on_locale_changed(void* ctx, event_t* e) {
tklocale_t* locale = (tklocale_t*)ctx; tklocale_t* tklocale = (tklocale_t*)ctx;
(void)e; (void)e;
s_locale = string(locale->language) + "_" + string(locale->country); s_locale = string(tklocale->language) + "_" + string(tklocale->country);
return RET_OK; return RET_OK;
} }
@ -19,19 +19,19 @@ static ret_t test_on_locale_changed(void* ctx, event_t* e) {
TEST(Locale, basic) { TEST(Locale, basic) {
const char* str = "ok"; const char* str = "ok";
uint32_t id = 0; uint32_t id = 0;
tklocale_t* locale = locale_create("en", "US"); tklocale_t* tklocale = tklocale_create("en", "US");
ASSERT_EQ(string("OK"), string(locale_tr(locale, str))); ASSERT_EQ(string("OK"), string(tklocale_tr(tklocale, str)));
id = locale_on(locale, EVT_LOCALE_CHANGED, test_on_locale_changed, locale); id = tklocale_on(tklocale, EVT_LOCALE_CHANGED, test_on_locale_changed, tklocale);
ASSERT_EQ(id, 1); ASSERT_EQ(id, 1);
ASSERT_EQ(locale_change(locale, "zh", "CN"), RET_OK); ASSERT_EQ(tklocale_change(tklocale, "zh", "CN"), RET_OK);
ASSERT_EQ(s_locale, "zh_CN"); ASSERT_EQ(s_locale, "zh_CN");
ASSERT_EQ(string("确定"), string(locale_tr(locale, str))); ASSERT_EQ(string("确定"), string(tklocale_tr(tklocale, str)));
ASSERT_EQ(locale_off(locale, id), RET_OK); ASSERT_EQ(tklocale_off(tklocale, id), RET_OK);
ASSERT_EQ(locale_change(locale, "en", "US"), RET_OK); ASSERT_EQ(tklocale_change(tklocale, "en", "US"), RET_OK);
ASSERT_EQ(s_locale, "zh_CN"); ASSERT_EQ(s_locale, "zh_CN");
ASSERT_EQ(string("OK"), string(locale_tr(locale, str))); ASSERT_EQ(string("OK"), string(tklocale_tr(tklocale, str)));
locale_destroy(locale); tklocale_destroy(tklocale);
} }

View File

@ -1,5 +1,5 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "base/locale.h" #include "base/tklocale.h"
#include "tools/str_gen/str_gen.h" #include "tools/str_gen/str_gen.h"
#include "tools/str_gen/xml_str_gen.h" #include "tools/str_gen/xml_str_gen.h"

View File

@ -6696,246 +6696,6 @@
"scriptable": true "scriptable": true
} }
}, },
{
"type": "class",
"methods": [
{
"params": [],
"annotation": {
"constructor": true
},
"desc": "获取缺省locale。",
"name": "locale",
"return": {
"type": "tklocale_t*",
"desc": "返回locale对象。"
}
},
{
"params": [
{
"type": "tklocale_t*",
"name": "locale",
"desc": "locale对象。"
}
],
"annotation": {},
"desc": "设置缺省locale。",
"name": "locale_set",
"return": {
"type": "ret_t",
"desc": "返回RET_OK表示成功否则表示失败。"
}
},
{
"params": [
{
"type": "char*",
"name": "language",
"desc": "语言。"
},
{
"type": "char*",
"name": "country",
"desc": "国家或地区。"
}
],
"annotation": {
"constructor": true
},
"desc": "创建locale。",
"name": "locale_create",
"return": {
"type": "tklocale_t*",
"desc": "返回locale对象。"
}
},
{
"params": [
{
"type": "tklocale_t*",
"name": "locale",
"desc": "locale对象。"
},
{
"type": "char*",
"name": "language",
"desc": "语言。"
},
{
"type": "char*",
"name": "country",
"desc": "国家或地区。"
}
],
"annotation": {
"constructor": true
},
"desc": "初始化locale。",
"name": "locale_init",
"return": {
"type": "tklocale_t*",
"desc": "返回locale对象。"
}
},
{
"params": [
{
"type": "tklocale_t*",
"name": "locale",
"desc": "locale对象。"
},
{
"type": "char*",
"name": "text",
"desc": "待翻译的文本。"
}
],
"annotation": {},
"desc": "翻译字符串。",
"name": "locale_tr",
"return": {
"type": "char*",
"desc": "返回翻译之后的字符串。"
}
},
{
"params": [
{
"type": "tklocale_t*",
"name": "locale",
"desc": "locale对象。"
},
{
"type": "char*",
"name": "language",
"desc": "语言。"
},
{
"type": "char*",
"name": "country",
"desc": "国家或地区。"
}
],
"annotation": {},
"desc": "设置当前的国家和语言。",
"name": "locale_change",
"return": {
"type": "ret_t",
"desc": "返回RET_OK表示成功否则表示失败。"
}
},
{
"params": [
{
"type": "tklocale_t*",
"name": "locale",
"desc": "控件对象。"
},
{
"type": "event_type_t",
"name": "type",
"desc": "事件类型目前固定为EVT_LOCALE_CHANGED。"
},
{
"type": "event_func_t",
"name": "on_event",
"desc": "事件处理函数。"
},
{
"type": "void*",
"name": "ctx",
"desc": "事件处理函数上下文。"
}
],
"annotation": {
"scriptable": "custom"
},
"desc": "注册指定事件的处理函数。",
"name": "locale_on",
"return": {
"type": "uint32_t",
"desc": "返回id用于locale_off。"
}
},
{
"params": [
{
"type": "tklocale_t*",
"name": "locale",
"desc": "控件对象。"
},
{
"type": "uint32_t",
"name": "id",
"desc": "locale_on返回的ID。"
}
],
"annotation": {
"scriptable": "custom"
},
"desc": "注销指定事件的处理函数。",
"name": "locale_off",
"return": {
"type": "ret_t",
"desc": "返回RET_OK表示成功否则表示失败。"
}
},
{
"params": [
{
"type": "tklocale_t*",
"name": "locale",
"desc": "locale对象。"
}
],
"annotation": {},
"desc": "释放全部资源。",
"name": "locale_deinit",
"return": {
"type": "ret_t",
"desc": "返回RET_OK表示成功否则表示失败。"
}
},
{
"params": [
{
"type": "tklocale_t*",
"name": "locale",
"desc": "locale对象。"
}
],
"annotation": {},
"desc": "释放全部资源并销毁locale对象。",
"name": "locale_destroy",
"return": {
"type": "ret_t",
"desc": "返回RET_OK表示成功否则表示失败。"
}
}
],
"properties": [
{
"name": "country;",
"desc": "国家或地区。如CN",
"type": "char*",
"annotation": {
"readable": true
}
},
{
"name": "language",
"desc": "语言。如zh",
"type": "char*",
"annotation": {
"readable": true
}
}
],
"header": "base/locale.h",
"desc": "locale",
"name": "locale_t"
},
{ {
"type": "class", "type": "class",
"methods": [ "methods": [
@ -10347,6 +10107,255 @@
"fake": true "fake": true
} }
}, },
{
"type": "class",
"methods": [
{
"params": [],
"annotation": {
"constructor": true,
"scriptable": true
},
"desc": "获取缺省tklocale。",
"name": "tklocale",
"alias": "tklocale_instance",
"return": {
"type": "tklocale_t*",
"desc": "返回tklocale对象。"
}
},
{
"params": [
{
"type": "tklocale_t*",
"name": "tklocale",
"desc": "tklocale对象。"
}
],
"annotation": {},
"desc": "设置缺省tklocale。",
"name": "tklocale_set",
"return": {
"type": "ret_t",
"desc": "返回RET_OK表示成功否则表示失败。"
}
},
{
"params": [
{
"type": "char*",
"name": "language",
"desc": "语言。"
},
{
"type": "char*",
"name": "country",
"desc": "国家或地区。"
}
],
"annotation": {
"constructor": true
},
"desc": "创建tklocale。",
"name": "tklocale_create",
"return": {
"type": "tklocale_t*",
"desc": "返回tklocale对象。"
}
},
{
"params": [
{
"type": "tklocale_t*",
"name": "tklocale",
"desc": "tklocale对象。"
},
{
"type": "char*",
"name": "language",
"desc": "语言。"
},
{
"type": "char*",
"name": "country",
"desc": "国家或地区。"
}
],
"annotation": {
"constructor": true
},
"desc": "初始化tklocale。",
"name": "tklocale_init",
"return": {
"type": "tklocale_t*",
"desc": "返回tklocale对象。"
}
},
{
"params": [
{
"type": "tklocale_t*",
"name": "tklocale",
"desc": "tklocale对象。"
},
{
"type": "char*",
"name": "text",
"desc": "待翻译的文本。"
}
],
"annotation": {
"scriptable": true
},
"desc": "翻译字符串。",
"name": "tklocale_tr",
"return": {
"type": "char*",
"desc": "返回翻译之后的字符串。"
}
},
{
"params": [
{
"type": "tklocale_t*",
"name": "tklocale",
"desc": "tklocale对象。"
},
{
"type": "char*",
"name": "language",
"desc": "语言。"
},
{
"type": "char*",
"name": "country",
"desc": "国家或地区。"
}
],
"annotation": {
"scriptable": true
},
"desc": "设置当前的国家和语言。",
"name": "tklocale_change",
"return": {
"type": "ret_t",
"desc": "返回RET_OK表示成功否则表示失败。"
}
},
{
"params": [
{
"type": "tklocale_t*",
"name": "tklocale",
"desc": "控件对象。"
},
{
"type": "event_type_t",
"name": "type",
"desc": "事件类型目前固定为EVT_LOCALE_CHANGED。"
},
{
"type": "event_func_t",
"name": "on_event",
"desc": "事件处理函数。"
},
{
"type": "void*",
"name": "ctx",
"desc": "事件处理函数上下文。"
}
],
"annotation": {
"scriptable": "custom"
},
"desc": "注册指定事件的处理函数。",
"name": "tklocale_on",
"return": {
"type": "uint32_t",
"desc": "返回id用于tklocale_off。"
}
},
{
"params": [
{
"type": "tklocale_t*",
"name": "tklocale",
"desc": "控件对象。"
},
{
"type": "uint32_t",
"name": "id",
"desc": "tklocale_on返回的ID。"
}
],
"annotation": {
"scriptable": "custom"
},
"desc": "注销指定事件的处理函数。",
"name": "tklocale_off",
"return": {
"type": "ret_t",
"desc": "返回RET_OK表示成功否则表示失败。"
}
},
{
"params": [
{
"type": "tklocale_t*",
"name": "tklocale",
"desc": "tklocale对象。"
}
],
"annotation": {},
"desc": "释放全部资源。",
"name": "tklocale_deinit",
"return": {
"type": "ret_t",
"desc": "返回RET_OK表示成功否则表示失败。"
}
},
{
"params": [
{
"type": "tklocale_t*",
"name": "tklocale",
"desc": "tklocale对象。"
}
],
"annotation": {},
"desc": "释放全部资源并销毁tklocale对象。",
"name": "tklocale_destroy",
"return": {
"type": "ret_t",
"desc": "返回RET_OK表示成功否则表示失败。"
}
}
],
"properties": [
{
"name": "country;",
"desc": "国家或地区。如CN",
"type": "char*",
"annotation": {
"readable": true
}
},
{
"name": "language",
"desc": "语言。如zh",
"type": "char*",
"annotation": {
"readable": true
}
}
],
"header": "base/tklocale.h",
"desc": "tklocale",
"name": "tklocale_t",
"annotation": {
"scriptable": true
}
},
{ {
"type": "class", "type": "class",
"methods": [ "methods": [

View File

@ -22,7 +22,7 @@
#include "str_gen.h" #include "str_gen.h"
#include "base/wstr.h" #include "base/wstr.h"
#include "base/buffer.h" #include "base/buffer.h"
#include "base/locale.h" #include "base/tklocale.h"
void StrGen::Add(const string& language, const Sentence& sentence) { void StrGen::Add(const string& language, const Sentence& sentence) {
StrTable::iterator iter = this->str_table.find(language); StrTable::iterator iter = this->str_table.find(language);