Jump to content

Module:kdr-translit

From Wiktionary, the free dictionary

Delete A user has added this module to requests for deletion(+).
Please see that page for discussion and justifications. You may continue to edit this module while the discussion proceeds, but please mention significant edits at the RFD discussion and ensure that the intention of votes already cast is not left unclear. Do not remove the {{rfd}} until the debate has finished.

This module will transliterate Karaim text per WT:KDR TR. The module should preferably not be called directly from templates or other modules. To use it from a template, use {{xlit}}. Within a module, use Module:languages#Language:transliterate.

For testcases, see Module:kdr-translit/testcases.

Functions

tr(text, lang, sc)
Transliterates a given piece of text written in the script specified by the code sc, and language specified by the code lang.
When transliteration fails, returns nil.

local export = {}

-- Digraphs (processed first)
local digraphs = {
    ["Дж"] = "C",  ["дж"] = "c",
    ["Къ"] = "Q",  ["къ"] = "q",
    ["Нъ"] = "Ñ",  ["нъ"] = "ñ",
    ["Хъ"] = "X",  ["хъ"] = "x",
    ["Гъ"] = "Ğ",  ["гъ"] = "ğ"
}

-- Single characters
local single_chars = {
    ["А"] = "A",   ["а"] = "a",
    ["Б"] = "B",   ["б"] = "b",
    ["В"] = "V",   ["в"] = "v",
    ["Г"] = "G",   ["г"] = "g",
    ["Д"] = "D",   ["д"] = "d",
    ["Е"] = "Ye",  ["е"] = "ye",
    ["Ж"] = "J",   ["ж"] = "j",
    ["З"] = "Z",   ["з"] = "z",
    ["И"] = "I",   ["и"] = "i",
    ["Й"] = "Y",   ["й"] = "y",
    ["К"] = "K",   ["к"] = "k",
    ["Л"] = "L",   ["л"] = "l",
    ["М"] = "M",   ["м"] = "m",
    ["Н"] = "N",   ["н"] = "n",
    ["О"] = "O",   ["о"] = "o",
    ["Ӧ"] = "Ö",   ["ӧ"] = "ö",
    ["П"] = "P",   ["п"] = "p",
    ["Р"] = "R",   ["р"] = "r",
    ["С"] = "S",   ["с"] = "s",
    ["Т"] = "T",   ["т"] = "t",
    ["У"] = "U",   ["у"] = "u",
    ["Ӱ"] = "Ü",   ["ӱ"] = "ü",
    ["Ф"] = "F",   ["ф"] = "f",
    ["Х"] = "H",   ["х"] = "h",
    ["Ц"] = "Ts",  ["ц"] = "ts",
    ["Ч"] = "Ç",   ["ч"] = "ç",
    ["Ш"] = "Ş",   ["ш"] = "ş",
    ["Щ"] = "Şç",  ["щ"] = "şç",
    ["Ъ"] = "ʺ",   ["ъ"] = "ʺ",
    ["Ы"] = "I",   ["ы"] = "ı",
    ["Ь"] = "ʹ",   ["ь"] = "ʹ",
    ["Э"] = "E",   ["э"] = "e",
    ["Ю"] = "Yu",  ["ю"] = "yu",
    ["Я"] = "Ya",  ["я"] = "ya"
}

function export.tr(text, lang, sc)
    -- Handle template invocations vs direct module calls
    if type(text) == "table" then
        text = text.args[1]
    end

    if not text or text == "" then
        return ""
    end

    -- If script is provided as a string code or object, validate if necessary
    -- (In Wiktionary, if sc is passed and it's not 'Cyrl', we usually don't transliterate)
    if sc and sc ~= "Cyrl" and type(sc) == "string" then
        return nil
    end

    -- Normalize Unicode to NFC form
    text = mw.ustring.toNFC(text)

    -- Step 1: Replace digraphs (cyr = Cyrillic key, lat = Latin value)
    for cyr, lat in pairs(digraphs) do
        text = mw.ustring.gsub(text, cyr, lat)
    end

    -- Step 2: Replace individual characters
    text = mw.ustring.gsub(text, ".", single_chars)

    return text
end

return export