Skip to content

Commit

Permalink
feat: Allow Lua widget translations (#2275)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavBfr authored Sep 7, 2022
1 parent 3b2f07a commit 4ed4f21
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/widget_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ WidgetSettings::WidgetSettings(Window* parent, Widget* widget) :
auto line = form->newLine(&grid);

auto option = *optPtr;
new StaticText(line, rect_t{}, option.name, 0, COLOR_THEME_PRIMARY1);
new StaticText(line, rect_t{}, option.displayName ? option.displayName : option.name, 0, COLOR_THEME_PRIMARY1);

switch (option.type) {
case ZoneOption::Integer:
Expand Down
1 change: 1 addition & 0 deletions radio/src/gui/colorlcd/zone.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ struct ZoneOption
ZoneOptionValue deflt;
ZoneOptionValue min;
ZoneOptionValue max;
const char * displayName;
};

struct ZoneOptionValueTyped
Expand Down
56 changes: 54 additions & 2 deletions radio/src/lua/lua_widget_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,26 @@ LuaWidgetFactory::LuaWidgetFactory(const char* name, ZoneOption* widgetOptions,
createFunction(createFunction),
updateFunction(0),
refreshFunction(0),
backgroundFunction(0)
backgroundFunction(0),
translateFunction(0)
{
}

LuaWidgetFactory::~LuaWidgetFactory() { unregisterWidget(this); }
LuaWidgetFactory::~LuaWidgetFactory() {
unregisterWidget(this);

if (displayName) {
delete displayName;
}

auto option = getOptions();
while (option && option->name != nullptr) {
if (option->displayName) {
delete option->displayName;
}
option++;
}
}

Widget* LuaWidgetFactory::create(Window* parent, const rect_t& rect,
Widget::PersistentData* persistentData,
Expand Down Expand Up @@ -90,3 +105,40 @@ Widget* LuaWidgetFactory::create(Window* parent, const rect_t& rect,
if (err) lw->setErrorMessage("create()");
return lw;
}


void LuaWidgetFactory::translateOptions(ZoneOption * options)
{
if (lsWidgets == 0) return;

// No translations provided
if (!translateFunction) return;

auto lang = TRANSLATIONS;

auto option = options;
while (option && option->name != nullptr) {
lua_rawgeti(lsWidgets, LUA_REGISTRYINDEX, translateFunction);
lua_pushstring(lsWidgets, option->name);
lua_pushstring(lsWidgets, lang);
bool err = lua_pcall(lsWidgets, 2, 1, 0);
if (!err) {
auto dn = lua_tostring(lsWidgets, -1);
if (dn) option->displayName = strdup(dn);
}
lua_pop(lsWidgets, 1);

option++;
}

// Widget display name
lua_rawgeti(lsWidgets, LUA_REGISTRYINDEX, translateFunction);
lua_pushstring(lsWidgets, name);
lua_pushstring(lsWidgets, lang);
bool err = lua_pcall(lsWidgets, 2, 1, 0);
if (!err) {
auto dn = lua_tostring(lsWidgets, -1);
if (dn) displayName = strdup(dn);
}
lua_pop(lsWidgets, 1);
}
3 changes: 3 additions & 0 deletions radio/src/lua/lua_widget_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ class LuaWidgetFactory : public WidgetFactory
bool init = true) const override;

protected:
void translateOptions(ZoneOption * options);

int createFunction;
int updateFunction;
int refreshFunction;
int backgroundFunction;
int translateFunction;
};
9 changes: 8 additions & 1 deletion radio/src/lua/widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ ZoneOption *createOptionsArray(int reference, uint8_t maxOptions)
luaL_checktype(lsWidgets, -2, LUA_TNUMBER); // key is number
luaL_checktype(lsWidgets, -1, LUA_TSTRING); // value is string
option->name = lua_tostring(lsWidgets, -1);
option->displayName = nullptr;
// TRACE("name = %s", option->name);
break;
case 1:
Expand Down Expand Up @@ -224,7 +225,7 @@ void luaLoadWidgetCallback()
const char * name=NULL;

int widgetOptions = 0, createFunction = 0, updateFunction = 0,
refreshFunction = 0, backgroundFunction = 0;
refreshFunction = 0, backgroundFunction = 0, translateFunction = 0;

luaL_checktype(lsWidgets, -1, LUA_TTABLE);

Expand Down Expand Up @@ -253,6 +254,10 @@ void luaLoadWidgetCallback()
backgroundFunction = luaL_ref(lsWidgets, LUA_REGISTRYINDEX);
lua_pushnil(lsWidgets);
}
else if (!strcmp(key, "translate")) {
translateFunction = luaL_ref(lsWidgets, LUA_REGISTRYINDEX);
lua_pushnil(lsWidgets);
}
}

if (name && createFunction) {
Expand All @@ -262,6 +267,8 @@ void luaLoadWidgetCallback()
factory->updateFunction = updateFunction;
factory->refreshFunction = refreshFunction;
factory->backgroundFunction = backgroundFunction; // NOSONAR
factory->translateFunction = translateFunction;
factory->translateOptions(options);
TRACE("Loaded Lua widget %s", name);
}
}
Expand Down

0 comments on commit 4ed4f21

Please sign in to comment.