Module:ItemData/nav: Difference between revisions

fixed /en subpage being added to links
added support for language subpages with more that 2 letters
Line 6: Line 6:


local function get_language_from_subpage()
local function get_language_from_subpage()
     -- Get the current page title
     -- Get the full page path (including subpages)
     local page_title = mw.title.getCurrentTitle().text
     local full_page_name = mw.title.getCurrentTitle().fullText
      
      
     -- Check if the page is a subpage (e.g., "Items/ru")
     -- Split path by slashes
     local subpage_lang = page_title:match("/(%a%a)$") -- Match two-letter language code at the end of the subpage
     local parts = {}
    for part in full_page_name:gmatch("[^/]+") do
        table.insert(parts, part)
    end
      
      
     -- If a language code is found, return it; otherwise, default to English ("en")
     -- Last part - potential language code
     return subpage_lang or "en"
    local last_part = parts[#parts] or ""
   
    -- Check for language codes matching:
    -- 1. Exactly 2 letters (like 'ru'), OR
    -- 2. 2 letters + hyphen + any characters (like 'zh-hans')
    if last_part:match("^[a-z][a-z]$") or last_part:match("^[a-z][a-z]%-[%w]+$") then
        -- Convert to lowercase and replace _ with -
        return last_part:lower():gsub("_", "-")
    end
   
     return "en" -- Default to English
end
end