模块:Phrases

来自「荏苒之境」
Sicusa留言 | 贡献2025年8月30日 (六) 11:35的版本
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

此模块的文档可以在模块:Phrases/doc创建

local translation = require("Module:Template translation")
local utils_str = require("Module:Utils/String")

local cargo = mw.ext.cargo
local trim = utils_str.trim
local escape_sql = utils_str.escape_sql

local PHRASE_THEMES_TABLE = "PhraseThemes"

local phrases = {}

phrases.show = function(frame)
	local args = frame.args
	local table_name = args.table
	
	local theme_text = args.theme_text
	if theme_text == "" then theme_text = nil end
	
	local parent_frame = frame:getParent()
	local theme = trim(parent_frame.args[1])
	
	local language = translation.getCurrentLanguageSubpage()
	if language ~= "zh-cn" then
		if language == nil or language == "" then
			language = "zh-cn"
		end
		
		local currentTitle = mw.title.getCurrentTitle()
		local existingPhrases = cargo.query(table_name, "Phrase", {
			where = "_pageName = '"..escape_sql(currentTitle.fullText).."'"
		})
		for i = 1, #existingPhrases do
			existingPhrases[existingPhrases[i]] = true
		end
		
		cargo.store(PHRASE_THEMES_TABLE, {
			PhraseTable = table_name,
			Theme = theme,
			Language = language,
			Translation = theme_text
		})
		
		for i, phrase in ipairs(parent_frame.args) do
			if i ~= 1 and existingPhrases[phrase] == nil then
				cargo.store(table_name, {
					Language = language,
					Phrase = trim(phrase),
					Theme = theme
				})
			end
		end
	end
	
	local t = {}
	t[#t+1] = "<ul>"
	for i, phrase in ipairs(parent_frame.args) do
		if i ~= 1 then
			t[#t+1] = "<li>"
			t[#t+1] = frame:preprocess(phrase)
			t[#t+1] = "</li>"
		end
	end
	t[#t+1] = "</ul>"
	return table.concat(t)
end

return phrases