Skip to content

Commit

Permalink
#76 add params for lang text string.format
Browse files Browse the repository at this point in the history
  • Loading branch information
Insality committed Jul 18, 2020
1 parent a561892 commit f182439
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
8 changes: 5 additions & 3 deletions druid/base/lang_text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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

Expand All @@ -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


Expand Down
3 changes: 2 additions & 1 deletion druid/system/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions example/init.script
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 8 additions & 2 deletions example/lang.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit f182439

Please sign in to comment.