Module:Lang/sandbox
Appearance
This is the module sandbox page for Module:Lang (diff). |
This module depends on the following other modules: |
Logic for {{lang}} and {{lang block}}.
--[=[
Module description
]=]
local p = {} -- p stands for package
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
--[=[
Get the appropriate text direction for a language
]=]
function p._text_direction(args)
local rtl_langs = {
ae = true, -- Avestan
ar = true, -- Arabic
arc = true, -- Aramaic
dv = true, -- Dhivehi
fa = true, -- Persian
ha = true, -- Hausa
he = true, -- Hebrew
hbo = true, -- Hebrew
ira = true, -- Iranian
khw = true, -- Khowar
ks = true, -- Kashmiri
ku = true, -- Kurdish
['obm-hebr'] = true, -- Moabite
ps = true, -- Pashto
syc = true, -- Syriac
syr = true, -- Syriac
ur = true, -- Urdu
yi = true -- Yiddish
}
local lang = args.lang
if lang and rtl_langs[lang] then
return 'rtl'
elseif type(lang) == 'string' then
local stripped_lang = mw.text.split(lang, '-')[1]
if rtl_langs[stripped_lang] then
return 'rtl'
end
end
return 'ltr'
end
--[=[
Implements [[Template:Lang]] and [[Template:Lang block]]
]=]
function p._lang(args)
local lang = args.language or args.lang or args[1] or 'en'
local dir = args.direction or args.dir or p._text_direction({['lang'] = lang})
local text = args.text or args[2]
local inline = yesno(args.inline) ~= false -- default is true
local noclose = yesno(args.noclose) or false -- default is false
-- Span or div?
local tag = (inline and 'span') or 'div'
local content = mw.html.create(tag)
-- Set the attributes
content
:addClass(table.concat({'wst-lang', args.class}, ' '))
:attr('lang', lang)
:attr('xml:lang', lang)
:attr('dir', dir)
:css({['font-family'] = args.fonts or args.font, ['style'] = args.style})
:allDone()
if text and inline then
content:wikitext(text)
elseif text then
content:newline():wikitext(text)
end
if not inline and not noclose then
content:newline()
end
content = tostring(content)
if noclose then
content = string.gsub(content, '</' .. tag .. '>$', '')
end
return content
end
function p.lang(frame)
local args = getArgs(frame)
return p._lang(args)
end
return p