add a lua binding to convert user-language strings to directions

This commit is contained in:
Enno Rehling 2015-11-27 15:14:25 +01:00
parent 24f559b4ad
commit 55c15c1905
5 changed files with 49 additions and 1 deletions

View File

@ -34,6 +34,14 @@ function setup()
eressea.settings.set("study.random_progress", "0") eressea.settings.set("study.random_progress", "0")
end end
function test_locales()
assert_equal(2, eressea.locale.direction("de", "Ost"))
assert_equal(5, eressea.locale.direction("de", "westen"))
assert_equal(4, eressea.locale.direction("de", "sw"))
assert_equal(-1, eressea.locale.direction("de", "foo"))
assert_equal(-1, eressea.locale.direction("foo", "sw"))
end
function test_flags() function test_flags()
local r = region.create(0, 0, "plain") local r = region.create(0, 0, "plain")
local f = faction.create("flags@eressea.de", "halfling", "de") local f = faction.create("flags@eressea.de", "halfling", "de")

View File

@ -1,5 +1,6 @@
#include "bind_locale.h" #include "bind_locale.h"
#include "util/language.h" #include "util/language.h"
#include "direction.h"
void locale_create(const char *lang) { void locale_create(const char *lang) {
get_or_create_locale(lang); get_or_create_locale(lang);
@ -19,3 +20,11 @@ const char * locale_get(const char *lang, const char *key) {
} }
return 0; return 0;
} }
int locale_direction(const char *lang, const char *str) {
struct locale *loc = get_locale(lang);
if (loc) {
return get_direction(str, loc);
}
return NODIRECTION;
}

View File

@ -7,6 +7,7 @@ extern "C" {
void locale_create(const char *lang); void locale_create(const char *lang);
void locale_set(const char *lang, const char *key, const char *str); void locale_set(const char *lang, const char *key, const char *str);
const char * locale_get(const char *lang, const char *key); const char * locale_get(const char *lang, const char *key);
int locale_direction(const char *lang, const char *str);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -7,5 +7,6 @@ module eressea {
void locale_create @ create(const char *lang); void locale_create @ create(const char *lang);
void locale_set @ set(const char *lang, const char *key, const char *str); void locale_set @ set(const char *lang, const char *key, const char *str);
const char * locale_get @ get(const char *lang, const char *key); const char * locale_get @ get(const char *lang, const char *key);
int locale_direction @ direction(const char *lang, const char *str);
} }
} }

View File

@ -20,7 +20,6 @@ LUALIB_API int luaopen_locale (lua_State* tolua_S);
#undef tolua_reg_types #undef tolua_reg_types
#define tolua_reg_types tolua_reg_types_locale #define tolua_reg_types tolua_reg_types_locale
#include "bind_tolua.h"
#include "bind_locale.h" #include "bind_locale.h"
/* function to register type */ /* function to register type */
@ -113,6 +112,35 @@ static int tolua_locale_eressea_locale_get00(lua_State* tolua_S)
#endif #endif
} }
/* function: locale_direction */
static int tolua_locale_eressea_locale_direction00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isstring(tolua_S,1,0,&tolua_err) ||
!tolua_isstring(tolua_S,2,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,3,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
const char* lang = ((const char*) tolua_tostring(tolua_S,1,0));
const char* str = ((const char*) tolua_tostring(tolua_S,2,0));
{
int tolua_ret = (int) locale_direction(lang,str);
tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'direction'.",&tolua_err);
return 0;
#endif
}
/* Open lib function */ /* Open lib function */
LUALIB_API int luaopen_locale (lua_State* tolua_S) LUALIB_API int luaopen_locale (lua_State* tolua_S)
{ {
@ -127,6 +155,7 @@ LUALIB_API int luaopen_locale (lua_State* tolua_S)
tolua_function(tolua_S,"create",tolua_locale_eressea_locale_create00); tolua_function(tolua_S,"create",tolua_locale_eressea_locale_create00);
tolua_function(tolua_S,"set",tolua_locale_eressea_locale_set00); tolua_function(tolua_S,"set",tolua_locale_eressea_locale_set00);
tolua_function(tolua_S,"get",tolua_locale_eressea_locale_get00); tolua_function(tolua_S,"get",tolua_locale_eressea_locale_get00);
tolua_function(tolua_S,"direction",tolua_locale_eressea_locale_direction00);
tolua_endmodule(tolua_S); tolua_endmodule(tolua_S);
tolua_endmodule(tolua_S); tolua_endmodule(tolua_S);
tolua_endmodule(tolua_S); tolua_endmodule(tolua_S);