Module:I18n: Difference between revisions
From Bodhicitta
(VSCode edit) |
(VSCode edit) |
||
| Line 17: | Line 17: | ||
end | end | ||
-- Create the HTML structure with both language variants as | -- Use div for block content (multi-paragraph), span for inline content | ||
local hasBlock = en:find('\n') or (bo and bo:find('\n')) | |||
local outerTag = hasBlock and 'div' or 'span' | |||
local innerTag = hasBlock and 'div' or 'span' | |||
local showInline = hasBlock and 'block' or 'inline' | |||
-- Create the HTML structure with both language variants as children | |||
-- (storing wiki markup in data-attributes breaks parsing) | -- (storing wiki markup in data-attributes breaks parsing) | ||
local html = mw.html.create( | local html = mw.html.create(outerTag) | ||
:addClass('i18n-text') | :addClass('i18n-text') | ||
:attr('data-default', default) | :attr('data-default', default) | ||
-- :tag() makes the child the current node; :done() returns to parent | -- :tag() makes the child the current node; :done() returns to parent | ||
html:tag( | html:tag(innerTag) | ||
:addClass('i18n-lang-en') | :addClass('i18n-lang-en') | ||
:css('display', lang ~= 'bo' and | :css('display', lang ~= 'bo' and showInline or 'none') | ||
:wikitext(en) | :wikitext(en) | ||
:done() | :done() | ||
if bo and bo ~= '' then | if bo and bo ~= '' then | ||
html:tag( | html:tag(innerTag) | ||
:addClass('i18n-lang-bo') | :addClass('i18n-lang-bo') | ||
:css('display', lang == 'bo' and | :css('display', lang == 'bo' and showInline or 'none') | ||
:wikitext(bo) | :wikitext(bo) | ||
:done() | :done() | ||
Revision as of 14:08, 9 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
-- Use div for block content (multi-paragraph), span for inline content
local hasBlock = en:find('\n') or (bo and bo:find('\n'))
local outerTag = hasBlock and 'div' or 'span'
local innerTag = hasBlock and 'div' or 'span'
local showInline = hasBlock and 'block' or 'inline'
-- Create the HTML structure with both language variants as children
-- (storing wiki markup in data-attributes breaks parsing)
local html = mw.html.create(outerTag)
:addClass('i18n-text')
:attr('data-default', default)
-- :tag() makes the child the current node; :done() returns to parent
html:tag(innerTag)
:addClass('i18n-lang-en')
:css('display', lang ~= 'bo' and showInline or 'none')
:wikitext(en)
:done()
if bo and bo ~= '' then
html:tag(innerTag)
:addClass('i18n-lang-bo')
:css('display', lang == 'bo' and showInline 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