Module:I18n: Difference between revisions

From Bodhicitta
((by SublimeText.Mediawiker))
 
(VSCode edit)
 
(9 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 tib = args.tib or args[2] or ''
     local bo = args.bo
     local bo = args.bo or tib  -- Tibetan in Tibetan script
     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
    -- 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'
      
      
    local lang = mw.getCurrentFrame():getTitle():inNamespace(0) and
        mw.user.getOption('language') or default
   
    -- Create the HTML structure
    local html = mw.html.create('span')
        :addClass('i18n-text')
        :attr('data-lang-en', en)
        :attr('data-lang-tib', tib)
        :attr('data-lang-bo', bo)
   
    -- Set initial display based on user preference or default
     if lang == 'bo' and bo ~= '' then
     if lang == 'bo' and bo ~= '' then
         html:wikitext(bo)
         return bo
    elseif lang == 'tib' and tib ~= '' then
        html:wikitext(tib)
     else
     else
         html:wikitext(en)
         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
     end
      
      
     return tostring(html)
     return default
end
end


return p
return p

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