Module documentation

Description

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

Test cases

1 tests failed.

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

test_template_localized_link:

Text Expected Actual
Yes {{Localized link}} [[Special:MyLanguage/{{{1}}}|{{{1}}}]][[:{{{1}}}| ]] [[Special:MyLanguage/{{{1}}}|{{{1}}}]][[:{{{1}}}| ]]

test_template_localized_link_sandbox:

Text Expected Actual
N {{Localized link/sandbox}} [[Special:MyLanguage/{{{1}}}|{{{1}}}]][[:{{{1}}}| ]] [[Special:MyLanguage/{{{1}}}|{{{1}}}]][[:{{{1}}}| ]]

-- @var table
local p = {}

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

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

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

	return args
end

-- @param lang_code 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.get_namespace_name( lang_code, mode, namespace )
	local namespace_names = {
		-- 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.get_variants()
		for _, variant in ipairs(variants) do
			if
				namespace_names[variant] and
				namespace_names[variant][namespace] ~= nil and
				namespace_names[variant][namespace] ~= ""
			then
				tv[#tv+1] = { variant, namespace_names[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
			namespace_names[lang_code] and
			namespace_names[lang_code][namespace] ~= nil
		then
			return namespace_names[lang_code][namespace]
		end
		local variantFallbacks = p.get_variant_fallbacks()[lang_code]
		for _, variant in ipairs(variantFallbacks) do
			if
				namespace_names[variant] and
				namespace_names[variant][namespace] ~= nil
			then
				return namespace_names[variant][namespace]
			end
		end
	end

	return namespace
end

-- @param string lang_code MediaWiki internal language code
-- @param string content_type_code
--   - C: Content wikitext
--   - T: Full display title text
-- @param string conv_mode
--   - 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.get_wikitext( lang_code, content_type_code, conv_mode, args )
	local t = {}
	if conv_mode == "LC" then
		local variants = p.get_variants()
		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 conv_mode == "LL" then
		if args[lang_code] ~= nil then
			return args[lang_code]
		end
		local variantFallbacks = p.get_variant_fallbacks()[lang_code]
		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.get_variants()
	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.has_variant( lang_code )
	local variants = p.get_variants()
    for _, variant in ipairs(variants) do
        if lang_code == variant then
            return true
        end
    end

    return false
end

-- LanguageConverter
-- * ZhConverter
--
-- @return table
function p.get_variant_fallbacks()
	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.get_variants()

	-- @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.has_variant( 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.get_namespace_name( ui_lang_code, "LL", ns_text_en ) ~= nil then
				return p.get_namespace_name( 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.get_wikitext( ui_lang_code, content_type_code, "LL", args )
			end
			if
				p.get_namespace_name( ui_lang_code, "LL", ns_text_en ) ~= nil and
				p.get_namespace_name( ui_lang_code, "LL", ns_text_en ) ~= ""
			then
				return p.get_namespace_name( ui_lang_code, "LL", ns_text_en ) ..
					":" .. p.get_wikitext( ui_lang_code, content_type_code, "LL", args )
			end
			return p.get_wikitext( ui_lang_code, content_type_code, "LL", args )
		elseif content_type_code == "H" then
			return p.get_wikitext( ui_lang_code, content_type_code, "LL", args )
		elseif content_type_code == "C" then
			return p.get_wikitext( ui_lang_code, content_type_code, "LL", args )
		end
	end

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

	return ""
end

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

return p