mirror of
https://gitee.com/zlgopen/awtk.git
synced 2024-12-02 03:58:33 +08:00
add some locale function
This commit is contained in:
parent
872dac66c1
commit
2b0d781589
@ -56,15 +56,29 @@ locale_info_t* locale_info_create(const char* language, const char* country) {
|
||||
return locale_info_create_internal(language, country, NULL);
|
||||
}
|
||||
|
||||
locale_info_t* locale_info_create_ex(const char* language, const char* country,
|
||||
assets_manager_t* am) {
|
||||
return locale_info_create_internal(language, country, am);
|
||||
}
|
||||
|
||||
const char* locale_info_tr(locale_info_t* locale_info, const char* text) {
|
||||
str_table_t* table = NULL;
|
||||
const char* tr_text = NULL;
|
||||
return_value_if_fail(locale_info != NULL && text != NULL, text);
|
||||
return_value_if_fail(locale_info->strs != NULL, text);
|
||||
|
||||
table = (str_table_t*)(locale_info->strs->data);
|
||||
if (locale_info->custom_tr != NULL) {
|
||||
tr_text = locale_info->custom_tr(locale_info->custom_tr_ctx, text);
|
||||
}
|
||||
|
||||
if (tr_text == NULL && locale_info->strs != NULL) {
|
||||
table = (str_table_t*)(locale_info->strs->data);
|
||||
tr_text = str_table_lookup(table, text);
|
||||
}
|
||||
|
||||
if (tr_text == NULL && locale_info->fallback_tr2 != NULL) {
|
||||
tr_text = locale_info->fallback_tr2(locale_info->fallback_tr_ctx, text);
|
||||
}
|
||||
|
||||
tr_text = str_table_lookup(table, text);
|
||||
if (tr_text == NULL && locale_info->fallback_tr != NULL) {
|
||||
tr_text = locale_info->fallback_tr(text);
|
||||
}
|
||||
@ -158,6 +172,26 @@ ret_t locale_info_set_fallback_tr(locale_info_t* locale_info, locale_info_tr_t t
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
ret_t locale_info_set_fallback_tr2(locale_info_t* locale_info, locale_info_tr_with_context_t tr,
|
||||
void* ctx) {
|
||||
return_value_if_fail(locale_info != NULL, RET_BAD_PARAMS);
|
||||
|
||||
locale_info->fallback_tr2 = tr;
|
||||
locale_info->fallback_tr_ctx = ctx;
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
ret_t locale_info_set_custom_tr(locale_info_t* locale_info, locale_info_tr_with_context_t tr,
|
||||
void* ctx) {
|
||||
return_value_if_fail(locale_info != NULL, RET_BAD_PARAMS);
|
||||
|
||||
locale_info->custom_tr = tr;
|
||||
locale_info->custom_tr_ctx = ctx;
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
static ret_t locale_info_deinit(locale_info_t* locale_info) {
|
||||
assets_manager_t* am = locale_info_get_assets_manager(locale_info);
|
||||
return_value_if_fail(locale_info != NULL, RET_OK);
|
||||
@ -202,6 +236,7 @@ static locale_infos_t* locale_infos_init(void) {
|
||||
if (s_locale_infos == NULL) {
|
||||
s_locale_infos = TKMEM_ZALLOC(locale_infos_t);
|
||||
return_value_if_fail(s_locale_infos != NULL, NULL);
|
||||
emitter_init(&(s_locale_infos->emitter));
|
||||
darray_init(&(s_locale_infos->infos), 3, (tk_destroy_t)locale_info_destroy,
|
||||
(tk_compare_t)locale_info_cmp_by_name);
|
||||
}
|
||||
|
@ -29,6 +29,7 @@
|
||||
|
||||
BEGIN_C_DECLS
|
||||
|
||||
typedef const char* (*locale_info_tr_with_context_t)(void* ctx, const char* text);
|
||||
typedef const char* (*locale_info_tr_t)(const char* text);
|
||||
|
||||
/**
|
||||
@ -63,6 +64,10 @@ struct _locale_info_t {
|
||||
const asset_info_t* strs;
|
||||
emitter_t* emitter;
|
||||
locale_info_tr_t fallback_tr;
|
||||
void* fallback_tr_ctx;
|
||||
locale_info_tr_with_context_t fallback_tr2;
|
||||
void* custom_tr_ctx;
|
||||
locale_info_tr_with_context_t custom_tr;
|
||||
};
|
||||
|
||||
/**
|
||||
@ -95,6 +100,19 @@ ret_t locale_info_set(locale_info_t* locale_info);
|
||||
*/
|
||||
locale_info_t* locale_info_create(const char* language, const char* country);
|
||||
|
||||
/**
|
||||
* @method locale_info_create_ex
|
||||
* 创建locale_info。
|
||||
* @annotation ["constructor"]
|
||||
* @param {const char*} language 语言。
|
||||
* @param {const char*} country 国家或地区。
|
||||
* @param {assets_manager_t*} am 资源管理器。
|
||||
*
|
||||
* @return {locale_info_t*} 返回locale_info对象。
|
||||
*/
|
||||
locale_info_t* locale_info_create_ex(const char* language, const char* country,
|
||||
assets_manager_t* am);
|
||||
|
||||
/**
|
||||
* @method locale_info_tr
|
||||
* 翻译字符串。
|
||||
@ -173,6 +191,29 @@ ret_t locale_info_reload(locale_info_t* locale_info);
|
||||
*/
|
||||
ret_t locale_info_set_fallback_tr(locale_info_t* locale_info, locale_info_tr_t tr);
|
||||
|
||||
/**
|
||||
* @method locale_info_set_fallback_tr2
|
||||
* 设置候补翻译函数。
|
||||
* @param {locale_info_t*} locale_info locale_info对象。
|
||||
* @param {locale_info_tr_with_context_t} tr fallback翻译函数。
|
||||
* @param {void*} ctx 翻译函数的上下文。
|
||||
*
|
||||
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
|
||||
*/
|
||||
ret_t locale_info_set_fallback_tr2(locale_info_t* locale_info, locale_info_tr_with_context_t tr, void* ctx);
|
||||
|
||||
/**
|
||||
* @method locale_info_set_custom_tr
|
||||
* 设置自定义的候补翻译函数。
|
||||
* > 有时我们需要优先加载用户自定义的翻译,加载失败才加载系统缺省的,可用设置一个函数去实现这类功能。
|
||||
* @param {locale_info_t*} locale_info locale_info对象。
|
||||
* @param {locale_info_tr_with_context_t} tr 自定义的翻译函数。
|
||||
* @param {void*} ctx 翻译函数的上下文。
|
||||
*
|
||||
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
|
||||
*/
|
||||
ret_t locale_info_set_custom_tr(locale_info_t* locale_info, locale_info_tr_with_context_t tr, void* ctx);
|
||||
|
||||
/**
|
||||
* @method locale_info_destroy
|
||||
* 释放全部资源并销毁locale_info对象。
|
||||
|
Loading…
Reference in New Issue
Block a user