Module:Lang: Difference between revisions
Jump to navigation
Jump to search
m (1 revision imported) |
enws>CalendulaAsteraceae (think I've fixed all the errors now) |
||
| Line 1: | Line 1: | ||
--[=[ | --[=[ | ||
Module description | |||
]=] | ]=] | ||
local p = {} -- p stands for package | |||
local | |||
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' | |||
if | |||
if | |||
return | |||
end | end | ||
end | end | ||
return 'ltr' | |||
return | |||
end | 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 | 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) | |||
local | |||
local | |||
-- 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 | if text and inline then | ||
content:wikitext(text) | |||
elseif text then | |||
content:newline():wikitext(text) | |||
end | end | ||
if not inline and not noclose then | |||
content:newline() | |||
end | end | ||
content = tostring(content) | |||
if | if noclose then | ||
content = string.gsub(content, '</' .. tag .. '>$', '') | |||
end | end | ||
return | return content | ||
end | end | ||
function p.lang(frame) | |||
local args = getArgs(frame) | |||
return p._lang(args) | |||
end | end | ||
return p | |||
Revision as of 09:08, 9 January 2025
Documentation for this module may be created at Module:Lang/doc
--[=[
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