152 lines
4.3 KiB
Lua
152 lines
4.3 KiB
Lua
local state = runtime.import("state")
|
||
|
||
---@class ShowcaseI18n
|
||
local i18n = {}
|
||
|
||
local fallback_locale = "zh-Hans"
|
||
|
||
local messages = {
|
||
["zh-Hans"] = {
|
||
app_title = "Lua Runtime Showcase",
|
||
app_subtitle = "仿 Cocos Showcase:左侧管理所有示例,右侧查看说明并直接运行。",
|
||
examples_title = "Examples",
|
||
preview_title = "Preview / Runtime Stage",
|
||
tab_code = "代码",
|
||
tab_params = "参数",
|
||
copy_code = "复制代码",
|
||
copy_params = "复制参数",
|
||
selected_prefix = "已选择示例:",
|
||
status_ready = "点击按钮查看 RuntimeEvent -> Lua -> Diff / Command 示例",
|
||
i18n_current = "当前语言",
|
||
i18n_zh = "中文文案:运行时只传 locale,翻译由 Lua 包管理。",
|
||
i18n_en = "English copy: Lua owns text resources and fallback policy.",
|
||
i18n_switched = "已切换语言:",
|
||
responsive_title = "分辨率适配",
|
||
responsive_design = "设计分辨率",
|
||
responsive_screen = "屏幕",
|
||
responsive_viewport = "视口",
|
||
responsive_phone = "模拟手机宽度",
|
||
responsive_tablet = "模拟平板宽度",
|
||
responsive_desktop = "模拟桌面宽度"
|
||
},
|
||
en = {
|
||
app_title = "Lua Runtime Showcase",
|
||
app_subtitle = "Cocos-style showcase: select examples on the left, inspect and run them on the right.",
|
||
examples_title = "Examples",
|
||
preview_title = "Preview / Runtime Stage",
|
||
tab_code = "Code",
|
||
tab_params = "Params",
|
||
copy_code = "Copy code",
|
||
copy_params = "Copy params",
|
||
selected_prefix = "Selected example: ",
|
||
status_ready = "Tap actions to inspect RuntimeEvent -> Lua -> Diff / Command.",
|
||
i18n_current = "Current locale",
|
||
i18n_zh = "中文文案:运行时只传 locale,翻译由 Lua 包管理。",
|
||
i18n_en = "English copy: Lua owns text resources and fallback policy.",
|
||
i18n_switched = "Locale switched: ",
|
||
responsive_title = "Responsive Layout",
|
||
responsive_design = "Design",
|
||
responsive_screen = "Screen",
|
||
responsive_viewport = "Viewport",
|
||
responsive_phone = "Simulate phone width",
|
||
responsive_tablet = "Simulate tablet width",
|
||
responsive_desktop = "Simulate desktop width"
|
||
}
|
||
}
|
||
|
||
---@param locale? string
|
||
---@return string
|
||
local function normalize(locale)
|
||
if locale == nil or locale == "" then
|
||
return fallback_locale
|
||
end
|
||
if messages[locale] ~= nil then
|
||
return locale
|
||
end
|
||
|
||
local language = string.match(locale, "^([A-Za-z]+)")
|
||
if language ~= nil and messages[language] ~= nil then
|
||
return language
|
||
end
|
||
return fallback_locale
|
||
end
|
||
|
||
---@param ctx? RuntimeContext
|
||
function i18n.apply_context(ctx)
|
||
local locale = fallback_locale
|
||
if ctx ~= nil and ctx.locale ~= nil and ctx.locale.resolved ~= nil then
|
||
locale = ctx.locale.resolved
|
||
end
|
||
state.locale = normalize(locale)
|
||
end
|
||
|
||
---@param locale string
|
||
function i18n.set_locale(locale)
|
||
state.locale = normalize(locale)
|
||
end
|
||
|
||
---@return string
|
||
function i18n.toggle_locale()
|
||
if state.locale == "en" then
|
||
state.locale = "zh-Hans"
|
||
else
|
||
state.locale = "en"
|
||
end
|
||
return state.locale
|
||
end
|
||
|
||
---@return string
|
||
function i18n.current_locale()
|
||
return normalize(state.locale)
|
||
end
|
||
|
||
---@param key string
|
||
---@return string
|
||
function i18n.t(key)
|
||
local locale = normalize(state.locale)
|
||
local bundle = messages[locale] or messages[fallback_locale]
|
||
return bundle[key] or messages[fallback_locale][key] or key
|
||
end
|
||
|
||
---@param example ShowcaseExample
|
||
---@param field string
|
||
---@return string
|
||
function i18n.example_field(example, field)
|
||
if i18n.current_locale() == "en" then
|
||
local en_value = example[field .. "_en"]
|
||
if en_value ~= nil and en_value ~= "" then
|
||
return en_value
|
||
end
|
||
end
|
||
return example[field]
|
||
end
|
||
|
||
---@param example ShowcaseExample
|
||
---@return string
|
||
function i18n.example_label(example)
|
||
return i18n.example_field(example, "menu")
|
||
end
|
||
|
||
---@param example ShowcaseExample
|
||
---@return string
|
||
function i18n.example_params(example)
|
||
return i18n.example_field(example, "params")
|
||
end
|
||
|
||
---@param example ShowcaseExample
|
||
---@return string
|
||
function i18n.example_group(example)
|
||
return i18n.example_field(example, "group")
|
||
end
|
||
|
||
---@param action ShowcaseAction
|
||
---@return string
|
||
function i18n.action_text(action)
|
||
if i18n.current_locale() == "en" and action.text_en ~= nil and action.text_en ~= "" then
|
||
return action.text_en
|
||
end
|
||
return action.text
|
||
end
|
||
|
||
return i18n
|