Module:LC zh

Permanently protected module
From mediawiki.org
Module documentation

Description

Implements {{LC zh }} for zh language conversion.

Test cases

All tests passed.

test_main:

Text Expected Actual
Yes {{#invoke:LC zh|main}}
Yes {{#invoke:LC zh|main|type=N}}
Yes {{#invoke:LC zh|main|type=N|ns=User}} User User
Yes {{#invoke:LC zh|main|type=N|ns=Help}} Help Help
Yes {{#invoke:LC zh|main|type=T}}
Yes {{#invoke:LC zh|main|type=T|ns=User}} User: User:
Yes {{#invoke:LC zh|main|type=T|ns=Help}} Help: Help:
Yes {{#invoke:LC zh|main|type=T|ns=User|zh=Example}} User:Example User:Example
Yes {{#invoke:LC zh|main|type=T|ns=Help|zh=Example|zh-hans=示例|zh-hant=範例}} Help:Example Help:Example
Yes {{#invoke:LC zh|main|type=T|hide_ns=1|ns=Help}}
Yes {{#invoke:LC zh|main|type=T|hide_ns=1|ns=Help|zh=Example|zh-hans=示例|zh-hant=範例}} Example Example
Yes {{#invoke:LC zh|main|type=H}}
Yes {{#invoke:LC zh|main|type=C}}
Yes {{#invoke:LC zh|main|type=C|zh=Example|zh-hans=示例|zh-hant=範例}} Example Example

test_template:

Text Expected Actual
Yes {{LC zh}}
Yes {{LC zh|type=N}}
Yes {{LC zh|type=N|ns=User}} User User
Yes {{LC zh|type=N|ns=Help}} Help Help
Yes {{LC zh|type=T}}
Yes {{LC zh|type=T|ns=User}} User: User:
Yes {{LC zh|type=T|ns=Help}} Help: Help:
Yes {{LC zh|type=T|ns=User|zh=Example}} User:Example User:Example
Yes {{LC zh|type=T|ns=Help|zh=Example|zh-hans=示例|zh-hant=範例}} Help:Example Help:Example
Yes {{LC zh|type=T|hide_ns=1|ns=Help}}
Yes {{LC zh|type=T|hide_ns=1|ns=Help|zh=Example|zh-hans=示例|zh-hant=範例}} Example Example
Yes {{LC zh|type=H}}
Yes {{LC zh|type=C}}
Yes {{LC zh|type=C|zh=Example|zh-hans=示例|zh-hant=範例}} Example Example

-- @var table
local p = {}

-- @param frame frame
-- @return table Merged argument table
function p.getMergedArgs( frame )
	local args = {}
	-- {{#invoke:Module name|function name|key=value}}
	local origArgs = frame.args
	-- {{Template name|key=value}}
	local parentArgs = frame:getParent().args

	for k, v in pairs( origArgs ) do
		v = mw.text.trim( tostring( v ) )
		args[k] = v
	end

	for k, v in pairs( parentArgs ) do
		v = mw.text.trim( v )
		args[k] = v
	end

	return args
end

-- @param langCode MediaWiki internal language code
-- @param mode
--   - LC: Convertable (language converter syntax)
--   - LL: Unconvertable (was referring to Template:Localized_link which was
--     unconvertable)
-- @param namespace en canonical namespace name, use whitespace instead of
--   underscore
-- @return string Localized namespace name
function p.getNamespaceName( langCode, mode, namespace )
	local namespaceNames = {
		-- Prevent fallback
		["zh"] = {
			-- 0 (Main)
			[""] = "",
			-- 1
			["Talk"] = "Talk",
			-- 2
			["User"] = "User",
			-- 3
			["User talk"] = "User talk",
			-- 4
			["Project"] = "Project",
			-- 5
			["Project talk"] = "Project talk",
			-- 6
			["File"] = "File",
			-- 7
			["File talk"] = "File talk",
			-- 8
			["MediaWiki"] = "MediaWiki",
			-- 9
			["MediaWiki talk"] = "MediaWiki talk",
			-- 10
			["Template"] = "Template",
			-- 11
			["Template talk"] = "Template talk",
			-- 12
			["Help"] = "Help",
			-- 13
			["Help talk"] = "Help talk",
			-- 14
			["Category"] = "Category",
			-- 15
			["Category talk"] = "Category talk",
			-- 90
			["Thread"] = "Thread",
			-- 91
			["Thread talk"] = "Thread talk",
			-- 92
			["Summary"] = "Summary",
			-- 93
			["Summary talk"] = "Summary talk",
			-- 100
			["Manual"] = "Manual",
			-- 101
			["Manual talk"] = "Manual talk",
			-- 102
			["Extension"] = "Extension",
			-- 103
			["Extension talk"] = "Extension talk",
			-- 104
			["API"] = "API",
			-- 105
			["API talk"] = "API talk",
			-- 106
			["Skin"] = "Skin",
			-- 107
			["Skin talk"] = "Skin talk",
			-- 486
			["Data"] = "Data",
			-- 487
			["Data talk"] = "Data talk",
			-- 828
			["Module"] = "Module",
			-- 829
			["Module talk"] = "Module talk",
			-- 1198
			["Translations"] = "Translations",
			-- 1199
			["Translations talk"] = "Translations talk",
			-- 2300
			["Gadget"] = "Gadget",
			-- 2301
			["Gadget talk"] = "Gadget talk",
			-- 2302
			["Gadget definition"] = "Gadget definition",
			-- 2303
			["Gadget definition talk"] = "Gadget definition talk",
			-- 2600
			["Topic"] = "Topic",
			-- 5500
			["Newsletter"] = "Newsletter",
			-- 5501
			["Newsletter talk"] = "Newsletter talk",
		},
		["zh-hans"] = {
			["Talk"] = "讨论",
			["User"] = "用户",
			["User talk"] = "用户讨论",
			["Project"] = "项目",
			["Project talk"] = "项目讨论",
			["File"] = "文件",
			["File talk"] = "文件讨论",
			["MediaWiki talk"] = "MediaWiki讨论",
			["Template"] = "模板",
			["Template talk"] = "模板讨论",
			["Help"] = "帮助",
			["Help talk"] = "帮助讨论",
			["Category"] = "分类",
			["Category talk"] = "分类讨论",
			["Thread"] = "帖子",
			["Thread talk"] = "帖子讨论",
			["Summary"] = "摘要",
			["Summary talk"] = "摘要讨论",
			["Manual"] = "手册",
			["Manual talk"] = "手册讨论",
			["Extension"] = "扩展",
			["Extension talk"] = "扩展讨论",
			["API talk"] = "API讨论",
			["Skin"] = "皮肤",
			["Skin talk"] = "皮肤讨论",
			["Data"] = "数据",
			["Data talk"] = "数据讨论",
			["Module"] = "模块",
			["Module talk"] = "模块讨论",
			["Translations"] = "翻译",
			["Translations talk"] = "翻译讨论",
			["Gadget"] = "小工具",
			["Gadget talk"] = "小工具讨论",
			["Gadget definition"] = "小工具定义",
			["Gadget definition talk"] = "小工具定义讨论",
			["Topic"] = "话题",
			["Newsletter"] = "电子报",
			["Newsletter talk"] = "电子报讨论",
		},
		["zh-hant"] = {
			["Talk"] = "討論",
			["User"] = "使用者",
			["User talk"] = "使用者討論",
			["Project"] = "專案",
			["Project talk"] = "專案討論",
			["File"] = "檔案",
			["File talk"] = "檔案討論",
			["MediaWiki talk"] = "MediaWiki討論",
			["Template"] = "模板",
			["Template talk"] = "模板討論",
			["Help"] = "說明",
			["Help talk"] = "說明討論",
			["Category"] = "分類",
			["Category talk"] = "分類討論",
			["Thread"] = "討論串",
			["Thread talk"] = "討論串討論",
			["Summary"] = "摘要",
			["Summary talk"] = "摘要討論",
			["Manual"] = "手冊",
			["Manual talk"] = "手冊討論",
			["Extension"] = "擴充功能",
			["Extension talk"] = "擴充功能討論",
			["API talk"] = "API討論",
			["Skin"] = "外觀",
			["Skin talk"] = "外觀討論",
			["Data"] = "資料",
			["Data talk"] = "資料討論",
			["Module"] = "模組",
			["Module talk"] = "模組討論",
			["Translations"] = "翻譯",
			["Translations talk"] = "翻譯討論",
			["Gadget"] = "小工具",
			["Gadget talk"] = "小工具討論",
			["Gadget definition"] = "小工具定義",
			["Gadget definition talk"] = "小工具定義討論",
			["Topic"] = "話題",
			["Newsletter"] = "電子報",
			["Newsletter talk"] = "電子報討論",
		},
		["zh-hk"] = {
			["User"] = "用戶",
			["User talk"] = "用戶討論",
		},
	}

	if mode == "LC" then
		local t = {}
		local tv = {}
		local variants = p.getVariants()
		for _, variant in ipairs(variants) do
			if
				namespaceNames[variant] and
				namespaceNames[variant][namespace] ~= nil and
				namespaceNames[variant][namespace] ~= ""
			then
				tv[#tv+1] = { variant, namespaceNames[variant][namespace] }
			end
		end
		if tv[2] ~= nil then
			for _, nskv in pairs(tv) do
				t[#t+1] = nskv[1] .. ":"
				t[#t+1] = nskv[2]
				t[#t+1] = ";"
				t[#t+1] = " "
			end
			t[#t] = nil
			return "-{" .. table.concat( t ) .. "}-"
		end
		return (tv[1] and tv[1][2]) or namespace
	end

	if mode == "LL" then
		if
			namespaceNames[langCode] and
			namespaceNames[langCode][namespace] ~= nil
		then
			return namespaceNames[langCode][namespace]
		end
		local variantFallbacks = p.getVariantFallbacks()[langCode]
		for _, variant in ipairs(variantFallbacks) do
			if
				namespaceNames[variant] and
				namespaceNames[variant][namespace] ~= nil
			then
				return namespaceNames[variant][namespace]
			end
		end
	end

	return namespace
end

-- @param string langCode MediaWiki internal language code
-- @param string content_type_code
--   - C: Content wikitext
--   - T: Full display title text
-- @param string convMode
--   - LC: Convertable (language converter syntax)
--   - LL: Unconvertable (was referring to Template:Localized_link which was
--     unconvertable)
-- @param table args Merged argument table
-- @return string Wikitext
function p.getWikitext( langCode, content_type_code, convMode, args )
	local t = {}
	if convMode == "LC" then
		local variants = p.getVariants()
		for _, variant in ipairs(variants) do
			if args[variant] ~= nil then
				t[#t+1] = variant .. ":"
				t[#t+1] = args[variant]
				t[#t+1] = ";"
				t[#t+1] = " "
			end
		end
		if t[5] ~= nil then
			t[#t] = nil
			return "-{" .. table.concat( t ) .. "}-"
		end
		if content_type_code == "T" then
			return t[2] or args[4] or ""
		end
		if content_type_code == "C" then
			return t[2] or args[2] or ""
		end
		return t[2] or ""
	end

	if convMode == "LL" then
		if args[langCode] ~= nil then
			return args[langCode]
		end
		local variantFallbacks = p.getVariantFallbacks()[langCode]
		for _, variant in ipairs(variantFallbacks) do
			if args[variant] ~= nil then
				return args[variant]
			end
		end
	end

	return ""
end

-- @return table Array of language variants in MediaWiki internal language codes
function p.getVariants()
	return {
		"zh",
		"zh-hans",
		"zh-hant",
		"zh-cn",
		"zh-tw",
		"zh-hk",
		"zh-mo",
		"zh-sg",
		"zh-my",
	}
end

-- @param string MediaWiki internal language code
-- @return bool Whether is one of zh language variant
function p.hasVariant( langCode )
	local variants = p.getVariants()
    for _, variant in ipairs(variants) do
        if langCode == variant then
            return true
        end
    end

    return false
end

-- LanguageConverter
-- * ZhConverter
--
-- @return table
function p.getVariantFallbacks()
	return {
		["zh"] = {
			"zh-hans",
			"zh-hant",
			"zh-cn",
			"zh-tw",
			"zh-hk",
			"zh-mo",
			"zh-sg",
			"zh-my",
		},
		["zh-hans"] = { "zh-cn", "zh-sg", "zh-my", "zh" },
		["zh-hant"] = { "zh-tw", "zh-hk", "zh-mo", "zh" },
		["zh-cn"] = { "zh-hans", "zh-my", "zh-sg", "zh" },
		["zh-sg"] = { "zh-my", "zh-hans", "zh-cn", "zh" },
		["zh-my"] = { "zh-sg", "zh-hans", "zh-cn", "zh" },
		["zh-tw"] = { "zh-hant", "zh-hk", "zh-mo", "zh" },
		["zh-hk"] = { "zh-mo", "zh-hant", "zh-tw", "zh" },
		["zh-mo"] = { "zh-hk", "zh-hant", "zh-tw", "zh" },
	}
end

-- @param frame frame
-- @param table args
-- @return string
function p._main( frame, args )
	-- @var string
	local ret_lang_code = "zh"

	-- @var title
	local page_title = mw.title.getCurrentTitle()

	-- Title.pageLang.code is an expensive function in Scribunto, thus not using
	--  pageLang.code here
	--
	-- @var string
	local page_lang_code = frame:preprocess( "{{PAGELANGUAGE}}" )

	-- Used for /variant-code subpage text detection
	--
	-- @var string
	local page_title_subpage_text = page_title.subpageText

	-- @var string
	local ui_lang_code = frame:callParserFunction{
		name = "int", args = { "lang" }
	}

	-- @var string|null
	local arg_lang_code = args.variant

	-- Content types:
	-- * N: namespace name
	-- * T: page title display text (with/without namespace)
	-- * H: headings
	-- * C: normal content
	--
	-- @var string
	local content_type_code = args[ 'type' ] or args[ 'mode' ] or args[1]

	-- Whether to hide namespace text
	-- @var bool Used as bool
	local hide_ns_text = args.hide_ns or args[2] == 0 or args[2] == "0" or args.nsp

	-- @var string
	local ns_text_en = args.ns or args.namespace or args[3]

	-- @var string
	local ns_text_2 = args.ns2

	-- begin WIP

	-- @var table
	local variants = p.getVariants()

	-- @var int|bool|string Whether used in non-lannguage-conversion-supported area and hide nsp
	local isLLnNsp = args.nons == 0 or args.nons == "0"

	if not p.hasVariant( ui_lang_code ) then
		ui_lang_code = "zh"
	end

	local useLC = args.lc or args.nolc ~= nil or page_lang_code == 'zh'

	if not useLC or isLLnNsp then
		if content_type_code == "N" then
			if p.getNamespaceName( ui_lang_code, "LL", ns_text_en ) ~= nil then
				return p.getNamespaceName( ui_lang_code, "LL", ns_text_en )
			else
				return ""
			end
		elseif content_type_code == "T" then
			if isLLnNsp or hide_ns_text then
				return p.getWikitext( ui_lang_code, content_type_code, "LL", args )
			end
			if
				p.getNamespaceName( ui_lang_code, "LL", ns_text_en ) ~= nil and
				p.getNamespaceName( ui_lang_code, "LL", ns_text_en ) ~= ""
			then
				return p.getNamespaceName( ui_lang_code, "LL", ns_text_en ) ..
					":" .. p.getWikitext( ui_lang_code, content_type_code, "LL", args )
			end
			return p.getWikitext( ui_lang_code, content_type_code, "LL", args )
		elseif content_type_code == "H" then
			return p.getWikitext( ui_lang_code, content_type_code, "LL", args )
		elseif content_type_code == "C" then
			return p.getWikitext( ui_lang_code, content_type_code, "LL", args )
		end
	end

	if content_type_code == "N" then
		if p.getNamespaceName( page_lang_code, "LC", ns_text_en ) ~= nil then
			return p.getNamespaceName( page_lang_code, "LC", ns_text_en )
		else
			return ""
		end
	elseif content_type_code == "T" then
		if hide_ns_text then
			return p.getWikitext( page_lang_code, content_type_code, "LC", args )
			-- return p.getWikitext( ui_lang_code, content_type_code, "LL", args )
		end
		if
			p.getNamespaceName( page_lang_code, "LC", ns_text_en ) ~= nil and
			p.getNamespaceName( page_lang_code, "LC", ns_text_en ) ~= ""
		then
			return p.getNamespaceName( page_lang_code, "LC", ns_text_en ) .. ":" ..
				p.getWikitext( page_lang_code, content_type_code, "LC", args )
		end
		return p.getWikitext( page_lang_code, content_type_code, "LC", args )
	elseif content_type_code == "H" then
		return p.getWikitext( page_lang_code, content_type_code, "LC", args )
	elseif content_type_code == "C" then
		return p.getWikitext( page_lang_code, content_type_code, "LC", args )
	end

	return ""
end

-- @param frame frame
-- @return string
function p.main( frame )
	local args = p.getMergedArgs( frame )
	return p._main( frame, args )
end

return p