Siirry sisältöön

Moduli:Languages

On otettu Мегавики-späi

-- Модуль для работы с языками ISO 639

-- загрузка модуля данных с таблицей языков local languages = mw.loadData('Module:Languages/data') local p = {}

-- Проверяет, пустой ли дан параметр local function isEmpty(s) return s == nil or s == end

-- вспомогательная функция, удаляет пробелы local function trimstr(s)

 return (s:gsub("^%s*(.-)%s*$", "%1"))

end

-- получает код языка, возвращает ссылку и нормализованный код языка (или пустые строки) local function get_lang_data(code)

   local l = languages[code];
   if l ~= nil then
       return "[[" .. l[2] .. "|" .. l[1] .. "]]", code
   elseif code ~= "" then
       return code, ""
   else
       return "", ""
   end

end

function p.getRefHtmlFrame( frame ) return p.getRefHtml( trimstr( frame.args[1] ) ) end

function p.getRefHtml( code ) local l = languages[code]; if l == nil then mw.log( 'Language description for code ' .. code .. ' not found' )

       return ""
   else
       return '(' .. l[ 1 ] .. ')'
   end

end

function p.getWikidataRefHtmlFrame( frame ) return p.getWikidataRefHtml( trimstr( frame.args[1] ) ) end

function p.getWikidataRefHtml( wikidataItemId ) local codeByItemId = mw.loadData( "Module:Wikidata/Language-codes" ) local code = codeByItemId[ wikidataItemId ]; if code == nil then mw.log( 'Language code not found for ' .. wikidataItemId ) return "" end return p.getRefHtml( code ) end

-- принимает zh, возвращает аббревиатуру function p.abbr(frame) local code = trimstr( frame.args[1] ) if not isEmpty(code) then return (languages[code] and languages[code][1] or ) end end

-- принимает zh|我|tt|мин, возвращает название статьи из Module:Languages/data function p.name(frame) local code = trimstr( frame.args[1] ) if code ~= nil and code ~= then return (languages[code] and languages[code][2] or ) end end

-- принимает zh|我|tt|мин, возвращает список через запятую function p.list(frame)

   local curr_lang = nil
   local result = nil
   for n, v in frame:argumentPairs() do

local trimmed = trimstr(v) if curr_lang == nil then if trimmed ~= then -- если язык пропущен, оставим прошлый curr_lang = trimstr(v) end else if trimmed ~= then local link, lang_code = get_lang_data(curr_lang) local list_item if lang_code ~= then list_item = link .. " " .. trimmed .. "" else list_item = link .. " " .. trimmed .. "".."" end

if result == nil then result = list_item else result = result .. ", " .. list_item end else -- Пустой текст — значит, текущий язык совпадает с следующим local link, lang_code = get_lang_data(curr_lang) if result == nil then result = link else result = result .. ", " .. link end end

curr_lang = nil

     end
   end
   
   -- просто #invoke:Languages|list|yue должно возвращать всё же ссылку без текста
   if curr_lang ~= nil then
       local link, lang_code = get_lang_data(curr_lang)
       if result ~= nil then
           result = result .. ", " .. link
       else
           result = link
       end
   end
   return result

end

-- принимает zh|tt, возвращает ref-zh, ref-tt function p.list_ref(frame) local result = local v = frame.args['в'] or nil local ref = nil

if v then ref = ' [%1%]' else ref = ' (%1%)' end

for x, lg in pairs( frame.args ) do local code = mw.ustring.lower(trimstr(lg)) if code ~= and code ~= 'в' then local l = languages[code] if l and l ~= nil then result = result .. mw.ustring.gsub(mw.ustring.gsub(ref, '%%2%%', p._transform_lang(code)), '%%1%%', l[1]) else if mw.title.new('ref-' .. code, 10).exists == false then code = 'und' end result = result .. frame:expandTemplate{ title = 'ref-' .. code } .. end end end

return result end

-- Выводит название языка в предложном падеже function p._transform_lang(code) if isEmpty(languages[code]) then return '<неизвестный код ' .. code .. '>' end

   if languages[code][3] then return languages[code][3] end
   local ln = mw.ustring.lower(
       languages[code] and languages[code][2]
       or mw.language.fetchLanguageName(code,'ru')
    ):gsub('%s+язык%s+',  ' '):gsub('%s*%(?язык%)?%s*',  )
   if not ln then return 'языке с ISO-кодом '..code..' (?)' end
   if ln:match('.*лингва$') then return ln:gsub('а$','е') end
   if mw.ustring.match(ln,'[сц]кий$') or ln:match('ный$') or mw.ustring.match(ln,'[сц]кий%s%b()$') or ln:match('ный%s%b()$')
   then
       ln = mw.ustring.gsub(
           mw.ustring.gsub(ln, 'н([ыи])й(%A)', function(y,s) return (y=='ы' and 'ном' or 'нем')..s end)
               :gsub('ный$', 'ном'),
           '([сц]к)ий(%A)', '%1ом%2'
       ):gsub('ский$', 'ском'):gsub('цкий$', 'цком');
       if ln:match('%)$') and not (ln:match('ом%)$') or ln:match('нем%)$')) -- «языке» перед уточнением, если оно не склоняется
       then 
           local r,s=ln:gsub('(%s)(%b())$','%1языке%1%2');
           if s==1 then return r end
       end
       return ln..' языке'
   else
       return 'языке '..ln
   end

end

function p.transform_lang(frame) return p._transform_lang(trimstr(frame.args[1])) end

return p