Module:I18n: Difference between revisions
From Bodhicitta
(VSCode edit) |
(VSCode edit) |
||
| Line 23: | Line 23: | ||
:attr('data-default', default) | :attr('data-default', default) | ||
-- :tag() makes the child the current node; :done() returns to parent | |||
html:tag('span') | |||
:addClass('i18n-lang-en') | :addClass('i18n-lang-en') | ||
:css('display', lang ~= 'bo' and 'inline' or 'none') | |||
:wikitext(en) | |||
:done() | |||
if bo and bo ~= '' then | if bo and bo ~= '' then | ||
html:tag('span') | |||
:addClass('i18n-lang-bo') | :addClass('i18n-lang-bo') | ||
:css('display', lang == 'bo' and 'inline' or 'none') | |||
:wikitext(bo) | |||
:done() | |||
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)
-- :tag() makes the child the current node; :done() returns to parent
html:tag('span')
:addClass('i18n-lang-en')
:css('display', lang ~= 'bo' and 'inline' or 'none')
:wikitext(en)
:done()
if bo and bo ~= '' then
html:tag('span')
:addClass('i18n-lang-bo')
:css('display', lang == 'bo' and 'inline' or 'none')
:wikitext(bo)
:done()
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