From f182439f2aef09c72acc23a1466122ec610799a8 Mon Sep 17 00:00:00 2001 From: Insality Date: Sat, 18 Jul 2020 22:16:24 +0300 Subject: [PATCH] #76 add params for lang text string.format --- druid/base/lang_text.lua | 8 +++++--- druid/system/settings.lua | 3 ++- example/init.script | 4 ++-- example/lang.lua | 10 ++++++++-- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/druid/base/lang_text.lua b/druid/base/lang_text.lua index f6a90ce8..e12aff8f 100644 --- a/druid/base/lang_text.lua +++ b/druid/base/lang_text.lua @@ -26,6 +26,7 @@ local M = component.create("lang_text", { const.ON_LANGUAGE_CHANGE }) function M.init(self, node, locale_id, no_adjust) self.druid = self:get_druid() self.text = self.druid:new_text(node, locale_id, no_adjust) + self.last_locale_args = {} self.on_change = Event() @@ -37,7 +38,7 @@ end function M.on_language_change(self) if self.last_locale then - M.translate(self) + M.translate(self, self.last_locale, unpack(self.last_locale_args)) end end @@ -55,9 +56,10 @@ end --- Translate the text by locale_id -- @function lang_text:translate -- @tparam string locale_id Locale id -function M.translate(self, locale_id) +function M.translate(self, locale_id, ...) + self.last_locale_args = {...} self.last_locale = locale_id or self.last_locale - self.text:set_to(settings.get_text(self.last_locale)) + self.text:set_to(settings.get_text(self.last_locale, ...)) end diff --git a/druid/system/settings.lua b/druid/system/settings.lua index 0dfe1a12..039dd601 100644 --- a/druid/system/settings.lua +++ b/druid/system/settings.lua @@ -6,7 +6,8 @@ local M = {} M.default_style = nil -function M.get_text(name) + +function M.get_text(name, ...) return "[Druid]: locales not inited" end diff --git a/example/init.script b/example/init.script index b2bfc47a..4b097cda 100644 --- a/example/init.script +++ b/example/init.script @@ -7,8 +7,8 @@ local function setup_druid() sound.play("kenney:/sound#" .. name) end) - druid.set_text_function(function(lang_id) - return lang.get_locale(lang_id) + druid.set_text_function(function(lang_id, ...) + return lang.get_locale(lang_id, ...) end) druid.on_language_change() diff --git a/example/lang.lua b/example/lang.lua index c75dcbb4..245f2117 100644 --- a/example/lang.lua +++ b/example/lang.lua @@ -44,8 +44,14 @@ local ru = { local data = en -function M.get_locale(lang_id) - return data[lang_id] or lang_id +function M.get_locale(lang_id, ...) + local localized_text = data[lang_id] or lang_id + + if #{...} > 0 then + localized_text = string.format(localized_text, ...) + end + + return localized_text end