Module:I18n

From Lethal Company Wiki

Translations are maintained on Module:I18n/TranslationsSubmit.


local p = {}

---@type table<string, table<string, table<string, string>>>
local translations = mw.loadJsonData('Module:I18n/TranslationsSubmit/result.json')
---@type table<string, boolean>
local categories = {}
---@type table<string, table<string, string>>
local translationsFlattened = {}

-- storing available categories and creating flattened translation table
for language, categoryDefinitionTranslations in pairs(translations) do
    translationsFlattened[language] = {}
    for category, definition_translations in pairs(categoryDefinitionTranslations) do
        categories[category] = true
        for definition, translation in pairs(definition_translations) do
            translationsFlattened[language][definition] = translation
        end
    end
end

-- debug purpose
--p.categories = categories
--mw.logObject(categories)
--p.translationsFlattened = translationsFlattened

local currentLang = mw.message.getDefaultLanguage().code
local wikiDefaultLanguage = mw.language.getContentLanguage().code

---==================================================
--- Functions that need loaded translations
---==================================================

---Get translation of the word `def` in language `lang`, category `cat`.
---@param lang string, e.g. zh-cn | de | ru | el
---@param def string, e.g. Rainy | TZP-Inhalant
---@param cat string?, e.g. WeatherTypes | Equipments
---@return string
function p.getTranslation(lang, def, cat)
    if translations[lang] == nil then
        return def
    end

    if cat == nil or translations[lang][cat] == nil then
        -- category not found, look flattened translations
        local translation = translationsFlattened[lang][def]
        if translation == nil then
            return def
        else
            return translation
        end
    end

    local translation = translations[lang][cat][def]
    if translation == nil then
        return def
    else
        return translation
    end
end

function p.getTranslationLink(lang, def, cat)
    if lang == wikiDefaultLanguage then
        return '[[' .. def .. ']]'
    end

    local translation = p.getTranslation(lang, def, cat)
    local translatedPageTitle = def .. '/' .. lang
    local translatedPageTitleObject = mw.title.new(translatedPageTitle)
    if translatedPageTitleObject.exists then
        return '[[' .. translatedPageTitle .. '|' .. translation .. ']]'
    else
        return '[[' .. def .. '|' .. translation .. ']]'
    end
end

---for [[Template:I18n]]
function p.tmplI18n(frame)
    local word = frame.args['text'] or frame.args[1]
    if word == nil then
        error('Please specify a word to translate.')
    end
    local lang = frame.args['lang'] or currentLang
    local cat = frame.args['category'] or frame.args['cat']
    local link = frame.args['link'] or false

    if link then
        return p.getTranslationLink(lang, word, cat)
    else
        return p.getTranslation(lang, word, cat)
    end
end

---==================================================
--- Functions that don't need loaded translations
---==================================================

--[[
Returns the "real" root page name of a localized page.

Examples:
* `getLocalizedPageRootPageName('Page Title/zh-cn')` -> `'Page Title'`
* `getLocalizedPageRootPageName('Page Title')` -> `'Page Title'`
* `getLocalizedPageRootPageName('Page/Title/zh-cn')` -> `'Page/Title'`
* `getLocalizedPageRootPageName('Page/Title')` -> `'Page/Title'`
* `getLocalizedPageRootPageName('Page/Title/')` -> `'Page/Title/'`
]]
function p.getLocalizedPageRootPageName(pageName)
    local slash = string.byte("/")
    local slashLastIndex = -1

    for i = 1, #pageName do
        if pageName:byte(i) == slash then
            slashLastIndex = i
        end
    end

    if slashLastIndex == -1 then
        return pageName
    end

    local rootPageName = pageName:sub(1, slashLastIndex - 1)
    local langCode = pageName:sub(slashLastIndex + 1, #pageName)

    if mw.language.isKnownLanguageTag(langCode) then
        return rootPageName
    else
        return pageName
    end
end

---Quickly insert a localized page with its title.
---@param pageTitle string e.g. Experimentation
---@param lang string, e.g. zh-cn
function p.getLocalizedPageLink(pageTitle, lang)
    if lang == wikiDefaultLanguage then
        return '[[' .. pageTitle .. ']]'
    end

    local localizedPageTitle = pageTitle .. '/' .. lang
    local localizedPageTitleObject = mw.title.new(localizedPageTitle)

    local link
    if localizedPageTitleObject.exists then
        link = localizedPageTitle
    else
        link = pageTitle
    end

    local displayText = pageTitle
    local localizedPageDisplayTitleTitleObject = mw.title.new(
        'Translations:' .. pageTitle .. '/Page display title/' .. lang
    )
    if localizedPageDisplayTitleTitleObject.exists then
        displayText = localizedPageDisplayTitleTitleObject:getContent()
    end


    if link == displayText then
        return '[[' .. link .. ']]'
    else
        return '[[' .. link .. '|' .. displayText .. ']]'
    end
end

---for [[Template:I18nPage]]
function p.tmplI18nPage(frame)
    local pageTitle = frame.args['page'] or frame.args[1]
    local lang = frame.args['lang'] or currentLang
    return p.getLocalizedPageLink(pageTitle, lang)
end

return p