Module:NormalizationLanguage: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:NormalizationLanguage
--[[
-- Maps language inputs (ISO codes, native names) to canonical English names.
* Name: NormalizationLanguage
--
* Author: Mark W. Datysgeld
-- Features:
* Description: Language input mapping from ISO codes and native names to canonical English names with extensive language support
--  * Maps ISO 639-1/2/3 codes to canonical names
* Notes: Maps ISO 639-1/2/3 codes to canonical names; recognizes native names (e.g., "Español" → "Spanish"); toggleable native forms display; strips diacritics for flexible matching; supports Indo-European, Sino-Tibetan, Austronesian, Afro-Asiatic, and other language families; includes caching for performance
--  * Recognizes native names (e.g., "Español" → "Spanish")
]]
--  * Displays native forms with canonical names (toggleable)
--  * Strips diacritics for flexible matching
--   * Formats multiple languages for templates
--
-- Configuration:
--  * setShowNativeForms(true/false) - Toggle native forms display
--  * getShowNativeForms() - Check current setting
--
-- Dependencies:
--  * Module:CanonicalForms - Normalization pattern
--  * Module:NormalizationDiacritic - Diacritic removal


local p = {}
local p = {}
Line 716: Line 705:
     isNativeFormCache[input] = nil
     isNativeFormCache[input] = nil
     return nil
     return nil
end
-- Format multiple languages with normalization using the centralized ListGeneration module
function p.formatLanguages(inputLanguages)
    if not inputLanguages or inputLanguages == "" then return "" end
    -- Define the item hook for language-specific formatting
    local function languageItemHook(lang)
        local trimmed = lang:match("^%s*(.-)%s*$")
        if trimmed == "" then return nil end
        -- Check if the input is a native form to preserve it
        local canonicalFromNative = p.isNativeForm(trimmed)
        local normalized, originalInput
        if canonicalFromNative then
            normalized = canonicalFromNative
            originalInput = trimmed
        else
            normalized = p.normalize(trimmed)
            originalInput = nil
        end
        -- Get the native form for the normalized language
        local nativeForm = originalInput or p.getNativeForm(normalized)
       
        -- Format with native name, except for English
        if nativeForm and config.showNativeForms and normalized ~= "English" then
            return string.format("%s<br/><span style=\"display:inline-block; width:0.1em; visibility:hidden;\">*</span><span style=\"font-size:75%%;\">%s</span>", normalized, nativeForm)
        else
            return normalized
        end
    end
    -- Set the options for the list generation
    local options = {
        mode = 'bullet',
        listClass = 'template-list-language',
        itemHook = languageItemHook
    }
    -- Use the centralized list generation function
    -- This will handle single vs. multiple items consistently.
    -- For a single item, it will correctly return a one-item list.
    return ListGeneration.createList(inputLanguages, options)
end
end