Moduł:Lang

Z VatoWiki
Wersja z dnia 08:37, 21 cze 2016 autorstwa Admin (dyskusja | edycje) (1 wersja)
(różn.) ← poprzednia wersja | przejdź do aktualnej wersji (różn.) | następna wersja → (różn.)
Przejdź do nawigacji Przejdź do wyszukiwania

local lang = {}

local function langData(code) if not code or (#code == 0) then -- brak kodu języka mw.log("Pusty kod języka: "..code); return end

local data = mw.loadData( 'Module:lang/data' )

-- bezbłędny kod local lang = data[code] if lang then return lang, code end

-- może kod jest podany dużymi literami lub ma spacje przed i po local lcode = mw.text.trim(string.lower(code)) -- dobre kody języka są tylko w ASCII lang = data[lcode] if lang then mw.log('kod "'..code..'" został zamieniony na "'..lcode..'"') return lang, lcode end

-- może kod jest podany z variantem local scode = string.match(lcode, "^([a-z]+)[_-]") lang = data[scode] if scode then mw.log('kod "'..code..'" został zinterpretowany jako "'..scode..'"') return lang, scode end

-- poddaje się mw.log('Nieznany kod języka: "'..code..'"'); return false, code end

function lang.istnieje(frame)

   local languageData, code = langData(frame.args[1])
   return languageData and code or nil

end

function lang.kursywa(frame)

   local languageData, code = langData(frame.args[1])
   return (languageData and (languageData.kursywa ~= false)) and "" or ""

end

lang["hasło"] = function(frame)

   local languageData, _ = langData(frame.args[1])
   return languageData['hasło']

end

lang["skrót"] = function(frame)

   local languageData, _ = langData(frame.args[1])
   return languageData['skrót'] or languageData['mianownik']

end

function lang.mianownik(frame)

   local languageData, _ = langData(frame.args[1])
   return languageData['mianownik']

end

lang["dopełniacz"] = function(frame)

   local languageData, _ = langData(frame.args[1])
   return languageData['dopełniacz']

end

function lang.miejscownik(frame)

   local languageData, _ = langData(frame.args[1])
   return languageData['miejscownik']

end

function list(frame, multi, showPL)

   -- konwersja kodów języków na kod wiki text opisujący kody
   local items = {}       -- tabela na skonwertowane kody
   
   -- flagi informujące, że na liście znajdują się podejrzane kody
   local invalid = false  -- nieznany kod języka
   local warning = false  -- język z listy częstych błędów
   local hiddenpl = false -- ukryty język polski
   local modified = false -- kod języka z automatyczną korektą
   local duplicated = false -- wielokrotnie podany (prawie) taki sam kod języka
   local args = frame.args;
   if not args[1] then
       -- brak argumentów sugeruje wywołanie z szablonu
       local parent = frame:getParent()
       if parent then
           args = parent.args
       end
   end
   if not args[1] then
       return "Błąd! Brak kodu języka."
   end
   
   -- języki wpadające do
   local warnings = {
   	ee = true,
   	am = true,
   	an = true,
   	ang = true,
   	ng = true,
   	kr = true,
   	se = true,
   	si = true
   }
   
   local formatLanguageItem = function(langCode, languageData)
       local skrot = languageData['skrót'] or languageData['mianownik']
       local nativeName = mw.language.fetchLanguageName(langCode)
       if nativeName and #nativeName > 0 and langCode ~= "pl" then
           return string.format("%s", languageData['hasło'], languageData['miejscownik'], nativeName, skrot)
       else
           return string.format("%s", languageData['hasło'],  languageData['miejscownik'], skrot)
       end
   end

local parsedLanguages = {}

   local countGood = 0
   
   local formatItem = function(languageCode)

local languageData, langCode = langData(languageCode) if langCode and (languageCode ~= langCode) then modified = true end

   	if languageData then
   		if warnings[langCode] then
               warning = true
           end

			local item = formatLanguageItem(langCode, languageData)
			if parsedLanguages[langCode] then
				duplicated = true
				mw.log("Powtórne przywołanie języka ("..languageCode..")")
			else
				parsedLanguages[langCode] = item;

if (langCode == "pl") and not showPL then -- pomiń język polski hiddenpl = true mw.log("Ukrywam język polski") else table.insert(items, item) countGood = countGood + 1 end

			end
       elseif not langCode or #langCode == 0 then
           if #items == 0 then
               -- pierwszy kod nie może być pusty, reszta może, bo jest przekazywana jako pusta w szablonach cytowania
               invalid = true
               table.insert(items, "Błąd! Brak kodu języka.")
           end
       else
           invalid = true
           local item = string.format("Błąd! Nieznany kod języka: %s. Sprawdź listę kodów.", languageCode)
			if parsedLanguages[languageCode] then
				duplicated = true
				mw.log("Powtórne przywołanie języka ("..languageCode..")")
			else
				parsedLanguages[languageCode] = item;
				table.insert(items, item)
			end
       end
   end
   
   if args[2] or multi then
       -- wiele argumentów sugeruje przekazywnie każdego kodu języka w oddzielnych argumentach
       for i, v in ipairs(args) do
           if #v > 0 then
               formatItem(v)
           end
       end
   else
       -- jeden argument pozwala także wysłać wszystkie kody oddzielone spacją
       for languageCode in string.gmatch(args[1], "%S+") do
           formatItem(languageCode)
       end
   end
   
   if #items == 0 then
   	if hiddenpl then
   		-- podano tylko język polski, który jest ukryty
   	else

-- pusta lista kodów invalid = true table.insert(items, "Błąd! Brak kodu języka.")

       end
   end
   -- ostateczne formatowanie wyniku
   local result = {}
   
   if #items > 0 then
   	if (countGood > 0) and hiddenpl then
   		-- przywracam język polski jeśli lista zawiera więcej niż jeden język 
   		table.insert(items, 1, parsedLanguages.pl)
   		hiddenpl = false
   	end
   	

table.insert(result, "(") table.insert(result, table.concat(items, " • ")) table.insert(result, ")") end

   if mw.title.getCurrentTitle().namespace == 0 then
       if warning then
           table.insert(result, "")
       end
       if invalid then
           table.insert(result, "")
       end
       if hiddenpl then
           table.insert(result, "")
       end
       if modified or duplicated then
           table.insert(result, "")
       end
   end
   return table.concat(result, "")

end

function lang.lang(frame) return list(frame, true, true) end

lang["język"] = function(frame) return list(frame, false, true) end

return lang