#76 add params for lang text string.format

This commit is contained in:
Insality 2020-07-18 22:16:24 +03:00
parent a561892fb6
commit f182439f2a
4 changed files with 17 additions and 8 deletions

View File

@ -26,6 +26,7 @@ local M = component.create("lang_text", { const.ON_LANGUAGE_CHANGE })
function M.init(self, node, locale_id, no_adjust) function M.init(self, node, locale_id, no_adjust)
self.druid = self:get_druid() self.druid = self:get_druid()
self.text = self.druid:new_text(node, locale_id, no_adjust) self.text = self.druid:new_text(node, locale_id, no_adjust)
self.last_locale_args = {}
self.on_change = Event() self.on_change = Event()
@ -37,7 +38,7 @@ end
function M.on_language_change(self) function M.on_language_change(self)
if self.last_locale then if self.last_locale then
M.translate(self) M.translate(self, self.last_locale, unpack(self.last_locale_args))
end end
end end
@ -55,9 +56,10 @@ end
--- Translate the text by locale_id --- Translate the text by locale_id
-- @function lang_text:translate -- @function lang_text:translate
-- @tparam string locale_id Locale id -- @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.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 end

View File

@ -6,7 +6,8 @@ local M = {}
M.default_style = nil M.default_style = nil
function M.get_text(name)
function M.get_text(name, ...)
return "[Druid]: locales not inited" return "[Druid]: locales not inited"
end end

View File

@ -7,8 +7,8 @@ local function setup_druid()
sound.play("kenney:/sound#" .. name) sound.play("kenney:/sound#" .. name)
end) end)
druid.set_text_function(function(lang_id) druid.set_text_function(function(lang_id, ...)
return lang.get_locale(lang_id) return lang.get_locale(lang_id, ...)
end) end)
druid.on_language_change() druid.on_language_change()

View File

@ -44,8 +44,14 @@ local ru = {
local data = en local data = en
function M.get_locale(lang_id) function M.get_locale(lang_id, ...)
return data[lang_id] or 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 end