Module:I18n: Difference between revisions
From Bodhicitta
((by SublimeText.Mediawiker)) |
(VSCode edit) |
||
| Line 17: | Line 17: | ||
end | end | ||
-- Create the HTML structure with | -- Create the HTML structure with both language variants as child spans | ||
-- (storing wiki markup in data-attributes breaks parsing) | |||
local html = mw.html.create('span') | local html = mw.html.create('span') | ||
:addClass('i18n-text') | :addClass('i18n-text') | ||
:attr('data-default', default) | :attr('data-default', default) | ||
local enSpan = html:tag('span') | |||
if lang = | :addClass('i18n-lang-en') | ||
if lang ~= 'bo' then | |||
enSpan:css('display', 'inline') | |||
else | else | ||
enSpan:css('display', 'none') | |||
end | end | ||
enSpan:wikitext(en) | |||
if bo and bo ~= '' then | |||
local boSpan = html:tag('span') | |||
:addClass('i18n-lang-bo') | |||
if lang == 'bo' then | |||
boSpan:css('display', 'inline') | |||
else | |||
boSpan:css('display', 'none') | |||
end | |||
boSpan:wikitext(bo) | |||
end | |||
return tostring(html) | return tostring(html) | ||
end | end | ||
Revision as of 12:47, 4 March 2026
Documentation for this module may be created at Module:I18n/doc
-- Module:I18n (improved)
local p = {}
function p.text(frame)
local args = frame:getParent().args
local en = args.en or args[1] or ''
local bo = args.bo
local default = args.default or 'en'
-- For server-side rendering, always default to the specified default
-- JavaScript will handle the switching client-side
local lang = default
-- Only try to get user preference for logged-in users
if mw.user and not mw.user.isAnon() and mw.user.getOption then
lang = mw.user.getOption('bca-language') or default
end
-- Create the HTML structure with both language variants as child spans
-- (storing wiki markup in data-attributes breaks parsing)
local html = mw.html.create('span')
:addClass('i18n-text')
:attr('data-default', default)
local enSpan = html:tag('span')
:addClass('i18n-lang-en')
if lang ~= 'bo' then
enSpan:css('display', 'inline')
else
enSpan:css('display', 'none')
end
enSpan:wikitext(en)
if bo and bo ~= '' then
local boSpan = html:tag('span')
:addClass('i18n-lang-bo')
if lang == 'bo' then
boSpan:css('display', 'inline')
else
boSpan:css('display', 'none')
end
boSpan:wikitext(bo)
end
return tostring(html)
end
-- Simple version that can be called with explicit language parameter
function p.simple(frame)
local args = frame:getParent().args
local en = args.en or args[1] or ''
local bo = args.bo
local lang = args.lang or 'en'
if lang == 'bo' and bo ~= '' then
return bo
else
return en
end
end
-- Function to check if user is logged in and get their preference
function p.getUserLang(frame)
local default = frame.args.default or 'en'
if mw.user and not mw.user.isAnon() and mw.user.getOption then
return mw.user.getOption('bca-language') or default
end
return default
end
return p