local charset = {
    ["А"]="A", ["Б"]="B", ["В"]="V", ["Г"]="G", ["Д"]="D", ["Е"]="E", ["Ё"]="Yo", ["Ж"]="Zh", ["З"]="Z", ["И"]="I", ["Й"]="J", ["К"]="K", ["Л"]="L", ["М"]="M", ["Н"]="N", ["О"]="O", ["П"]="P", ["Р"]="R", ["С"]="S", ["Т"]="T", ["У"]="U", ["Ф"]="F", ["Х"]="Kh", ["Ц"]="Ts", ["Ч"]="Ch", ["Ш"]="Sh", ["Щ"]="Sch", ["Ъ"]="", ["Ы"]="Y", ["Ь"]="", ["Э"]="E", ["Ю"]="Yu", ["Я"]="Ya",
    ["а"]="a", ["б"]="b", ["в"]="v", ["г"]="g", ["д"]="d", ["е"]="e", ["ё"]="yo", ["ж"]="zh", ["з"]="z", ["и"]="i", ["й"]="j", ["к"]="k", ["л"]="l", ["м"]="m", ["н"]="n", ["о"]="o", ["п"]="p", ["р"]="r", ["с"]="s", ["т"]="t", ["у"]="u", ["ф"]="f", ["х"]="kh", ["ц"]="ts", ["ч"]="ch", ["ш"]="sh", ["щ"]="sch", ["ъ"]="", ["ы"]="y", ["ь"]="", ["э"]="e", ["ю"]="yu", ["я"]="ya"
}

-- Function to convert UTF-8 string to Translit
local function Translit(text)
    if not text then return nil end
    -- Pattern to correctly handle multi-byte UTF-8 characters in 3.3.5
    local result = text:gsub("[%z\1-\127\194-\244][\128-\191]*", function(c)
        return charset[c] or c
    end)
    return result
end

local f = CreateFrame("Frame")
f:SetScript("OnUpdate", function(self, elapsed)
    self.timer = (self.timer or 0) + elapsed
    if self.timer < 0.1 then return end -- Update 10 times per second
    self.timer = 0

    local kids = {WorldFrame:GetChildren()}
    for _, frame in ipairs(kids) do
        -- 3.3.5 Nameplate detection
        if not frame:GetName() or frame:GetName():find("NamePlate") then
            local regions = {frame:GetRegions()}
            for _, region in ipairs(regions) do
                if region:IsObjectType("FontString") then
                    local text = region:GetText()
                    -- Only transliterate if string contains Cyrillic characters
                    if text and text:match("[\208-\209]") then
                        region:SetText(Translit(text))
                    end
                end
            end
        end
    end
end)

print("|cFF00FF00TranslitNames Loaded!|r Russian names will now be shown in Latin.")
