Module:I18n: Difference between revisions
From Bodhicitta
((by SublimeText.Mediawiker)) |
(VSCode edit) |
||
| (7 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
-- Module:I18n | -- Module:I18n (improved) | ||
local p = {} | local p = {} | ||
| Line 5: | Line 5: | ||
local args = frame:getParent().args | local args = frame:getParent().args | ||
local en = args.en or args[1] or '' | local en = args.en or args[1] or '' | ||
local bo = args.bo | |||
local bo = args.bo | |||
local default = args.default or 'en' | 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 | local lang = default | ||
if mw.user and mw.user.getOption then | |||
-- 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 | lang = mw.user.getOption('bca-language') or default | ||
end | end | ||
-- | -- Use div for block content (multi-paragraph), span for inline content | ||
local | 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' | |||
-- | -- Pre-expand wikitext into HTML BEFORE building the wrapper tags. | ||
-- If we use :wikitext() inside mw.html tags, the parser expands the | |||
-- wikitext *after* serialisation and multi-paragraph content produces | |||
-- <p> tags that break out of their parent <span>/<div>, causing the | |||
-- two language siblings to interleave in the DOM. | |||
local enHtml = frame:preprocess(en) | |||
local boHtml = bo and bo ~= '' and frame:preprocess(bo) or nil | |||
-- Build the display style attributes manually and emit raw HTML. | |||
-- We cannot use mw.html here because :node() only accepts mw.html | |||
-- objects, not raw strings. String concatenation is the safe path. | |||
local enDisplay = lang ~= 'bo' and showInline or 'none' | |||
local boDisplay = lang == 'bo' and showInline or 'none' | |||
local out = '<' .. outerTag .. ' class="i18n-text" data-default="' .. default .. '">' | |||
out = out .. '<' .. innerTag .. ' class="i18n-lang-en" style="display:' .. enDisplay .. '">' | |||
out = out .. enHtml | |||
out = out .. '</' .. innerTag .. '>' | |||
if boHtml then | |||
out = out .. '<' .. innerTag .. ' class="i18n-lang-bo" style="display:' .. boDisplay .. '">' | |||
out = out .. boHtml | |||
out = out .. '</' .. innerTag .. '>' | |||
end | end | ||
return | out = out .. '</' .. outerTag .. '>' | ||
return out | |||
end | end | ||
-- | -- Simple version that can be called with explicit language parameter | ||
function p.simple(frame) | function p.simple(frame) | ||
local args = frame:getParent().args | local args = frame:getParent().args | ||
local en = args.en or args[1] or '' | local en = args.en or args[1] or '' | ||
local bo = args.bo | |||
local bo = args.bo | |||
local lang = args.lang or 'en' | local lang = args.lang or 'en' | ||
| Line 46: | Line 66: | ||
end | end | ||
-- Function to get | -- Function to check if user is logged in and get their preference | ||
function p.getUserLang(frame) | function p.getUserLang(frame) | ||
local default = frame.args.default or 'en' | local default = frame.args.default or 'en' | ||
if mw.user and mw.user.getOption then | if mw.user and not mw.user.isAnon() and mw.user.getOption then | ||
return mw.user.getOption('bca-language') or default | return mw.user.getOption('bca-language') or default | ||
end | end | ||
Latest revision as of 14:09, 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'
-- Pre-expand wikitext into HTML BEFORE building the wrapper tags.
-- If we use :wikitext() inside mw.html tags, the parser expands the
-- wikitext *after* serialisation and multi-paragraph content produces
-- <p> tags that break out of their parent <span>/<div>, causing the
-- two language siblings to interleave in the DOM.
local enHtml = frame:preprocess(en)
local boHtml = bo and bo ~= '' and frame:preprocess(bo) or nil
-- Build the display style attributes manually and emit raw HTML.
-- We cannot use mw.html here because :node() only accepts mw.html
-- objects, not raw strings. String concatenation is the safe path.
local enDisplay = lang ~= 'bo' and showInline or 'none'
local boDisplay = lang == 'bo' and showInline or 'none'
local out = '<' .. outerTag .. ' class="i18n-text" data-default="' .. default .. '">'
out = out .. '<' .. innerTag .. ' class="i18n-lang-en" style="display:' .. enDisplay .. '">'
out = out .. enHtml
out = out .. '</' .. innerTag .. '>'
if boHtml then
out = out .. '<' .. innerTag .. ' class="i18n-lang-bo" style="display:' .. boDisplay .. '">'
out = out .. boHtml
out = out .. '</' .. innerTag .. '>'
end
out = out .. '</' .. outerTag .. '>'
return out
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