From ec4e51e9546d7c22ed7e4265987c9dca9c3b6552 Mon Sep 17 00:00:00 2001 From: tomasklaen Date: Tue, 25 Apr 2023 10:01:57 +0200 Subject: [PATCH] feat: dynamic localization strings and caching MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `t()` now works as `string.format()` → `t('Foo %d %s', 42, 'bar')` --- scripts/uosc_shared/lib/intl.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/scripts/uosc_shared/lib/intl.lua b/scripts/uosc_shared/lib/intl.lua index 486bc48..78ac4ba 100644 --- a/scripts/uosc_shared/lib/intl.lua +++ b/scripts/uosc_shared/lib/intl.lua @@ -1,4 +1,5 @@ local locale = {} +local cache = {} -- https://learn.microsoft.com/en-us/windows/apps/publish/publish-your-app/supported-languages?pivots=store-installer-msix#list-of-supported-languages function get_languages() @@ -55,8 +56,11 @@ function make_locale() end ---@param text string -function t(text) - return locale[text] or text +function t(text, ...) + if not text then return '' end + if cache[text] then return cache[text] end + cache[text] = string.format(locale[text] or text, ...) + return cache[text] end locale = make_locale()