モジュール:Wikipedia言語名/testcases

local p = {}
local sortedPairs = require('モジュール:TableTools').sortedPairs
local langMappingTitle = 'モジュール:Wikipedia言語名/lang mapping'
local wikipediaLangMap = mw.loadData(langMappingTitle).long
local mwLanguageNames = mw.language.fetchLanguageNames('ja', 'all')

local function makeLangAndWikipediaLink(langName)
	if not langName or langName == '' then return '' end
	local f = '[[%s]]<small> ([[%s版ウィキペディア|wiki]])</small>'
	return mw.ustring.format(f, langName, langName)
end

function p.compareWithCLDR()
	local res = mw.html.create('table')
		:addClass('wikitable sortable')
		:tag('tr')
			:tag('th')
				:wikitext('言語コード')
				:done()
			:tag('th')
				:wikitext('[[', langMappingTitle, '|モジュール]]')
				:done()
			:tag('th')
				:wikitext('[[mw:Extension:CLDR/ja|CLDR]]による日本語名')
				:done()
			:tag('th')
				:wikitext('等しいか')
				:allDone()
	
	local td_yes = mw.html.create('td')
			:cssText('background-color:#9f9;vertical-align:middle;text-align:center;')
			:wikitext('Yes')
	local td_no = mw.html.create('td')
		:cssText('background-color:#f99;vertical-align:middle;text-align:center;')
		:wikitext('No')
	
	for code, name in sortedPairs(wikipediaLangMap) do
		local cldr = mwLanguageNames[code]
		
		res:tag('tr')
			:tag('td')
				:wikitext('[[:w:', code, ':|', code, ']]')
				:done()
			:tag('td')
				:wikitext(makeLangAndWikipediaLink(name))
				:done()
			:tag('td')
				:wikitext(makeLangAndWikipediaLink(cldr))
				:done()
			:node((name == cldr) and td_yes or td_no)
			:allDone()
	end
	return tostring(res)
end

function p.differenceWithISO()
	local langMappingTitle2 = 'モジュール:ISO639言語名/data'
	local isoLangMap = mw.loadData(langMappingTitle2).full_name
	local res = mw.html.create('table')
		:addClass('wikitable sortable')
		:tag('caption')
			:wikitext('[[モジュール:ISO639言語名/data]]と異なる言語名の一覧')
			:done()
		:tag('tr')
			:tag('th')
				:wikitext('言語コード')
				:done()
			:tag('th')
				:wikitext(langMappingTitle)
				:done()
			:tag('th')
				:wikitext(langMappingTitle2)
				:done()
			:tag('th')
				:wikitext('CLDRによる日本語名')
				:allDone()
	
	for code, name in sortedPairs(wikipediaLangMap) do
		local isoName = isoLangMap[code]
		if isoName ~= name then
			res:tag('tr')
				:tag('td')
					:wikitext('[[:w:', code, ':|', code, ']]')
					:done()
				:tag('td')
					:wikitext(makeLangAndWikipediaLink(name))
					:done()
				:tag('td')
					:wikitext(makeLangAndWikipediaLink(isoName))
					:done()
				:tag('td')
					:wikitext(makeLangAndWikipediaLink(mwLanguageNames[code]))
					:allDone()
		end
	end
	return tostring(res)
end

return p