Module:NormalizationLanguage: Difference between revisions

// via Wikitext Extension for VSCode
 
// via Wikitext Extension for VSCode
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:NormalizationLanguage
--[[
-- REVIEW: Name change
* Name: NormalizationLanguage
-- Maps language inputs (ISO codes, native names) to canonical English names.
* Author: Mark W. Datysgeld
--
* Description: Language input mapping from ISO codes and native names to canonical English names with extensive language support
-- Features:
* 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
--  * Maps ISO 639-1/2/3 codes to canonical names
]]
--  * 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 = {}
local CanonicalForms = require('Module:CanonicalForms')
local CanonicalForms = require('Module:CanonicalForms')
local DiacriticNormalization = require('Module:NormalizationDiacritic')
local DiacriticNormalization = require('Module:NormalizationDiacritic')
local ListGeneration = require('Module:ListGeneration')


-- Configuration
-- Configuration
Line 716: Line 705:
     isNativeFormCache[input] = nil
     isNativeFormCache[input] = nil
     return nil
     return nil
end
-- Format multiple languages with normalization
function p.formatLanguages(inputLanguages)
    if not inputLanguages or inputLanguages == "" then return "" end
   
    -- Split by semicolons
    local languages = {}
    for lang in inputLanguages:gmatch("[^;]+") do
        local trimmed = lang:match("^%s*(.-)%s*$")
        if trimmed and trimmed ~= "" then
            -- Check if native form
            local canonicalFromNative = p.isNativeForm(trimmed)
           
            -- If native form, use canonical + input as native
            local normalized, originalInput
            if canonicalFromNative then
                normalized = canonicalFromNative
                originalInput = trimmed
            else
                -- Otherwise normalize as usual
                normalized = p.normalize(trimmed)
                originalInput = nil
            end
           
            -- Get and format with native form if available
            local nativeForm = originalInput or p.getNativeForm(normalized)
            local formattedLang
           
            -- Exception: English never shows native form
            if nativeForm and config.showNativeForms and normalized ~= "English" then
                formattedLang = 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
                formattedLang = normalized
            end
           
            table.insert(languages, formattedLang)
        end
    end
   
    -- Format based on language count
    if #languages > 1 then
        local listItems = {}
        for _, lang in ipairs(languages) do
            table.insert(listItems, string.format("<li>%s</li>", lang))
        end
        return string.format("<ul class=\"template-list template-list-language\" style=\"margin:0; padding-left:1em;\">%s</ul>", table.concat(listItems, ""))
    elseif #languages == 1 then
        return languages[1]
    end
   
    return ""
end
end