feat: add-intl-directory
APi for scripts to extend localization
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
local intl_directories = {'~~/scripts/uosc_shared/intl/'}
|
||||
local locale = {}
|
||||
local cache = {}
|
||||
local reload_timer = nil
|
||||
|
||||
-- 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()
|
||||
@@ -48,13 +50,30 @@ function make_locale()
|
||||
elseif (lang == 'en') then
|
||||
translations = {}
|
||||
else
|
||||
table_assign(translations, get_locale_from_json('~~/scripts/uosc_shared/intl/' .. lang:lower() .. '.json'))
|
||||
for _, path in ipairs(intl_directories) do
|
||||
table_assign(translations, get_locale_from_json(path .. lang:lower() .. '.json'))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return translations
|
||||
end
|
||||
|
||||
function reload()
|
||||
reload_timer, cache = nil, {}
|
||||
locale = make_locale()
|
||||
end
|
||||
|
||||
---@param path string
|
||||
function add_directory(path)
|
||||
path = trim_end(trim_end(path, '\\'), '/') .. '/'
|
||||
if itable_index_of(intl_directories, path) then return end
|
||||
intl_directories[#intl_directories + 1] = path
|
||||
if not reload_timer then
|
||||
reload_timer = mp.add_timeout(0.1, reload)
|
||||
end
|
||||
end
|
||||
|
||||
---@param text string
|
||||
function t(text, ...)
|
||||
if not text then return '' end
|
||||
@@ -63,6 +82,6 @@ function t(text, ...)
|
||||
return cache[text]
|
||||
end
|
||||
|
||||
locale = make_locale()
|
||||
reload()
|
||||
|
||||
return t
|
||||
return {t = t, add_directory = add_directory}
|
||||
|
Reference in New Issue
Block a user