Module:xdq-translit

From Linguifex
Jump to navigation Jump to search

This module will transliterate Kaitag language text. 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:xdq-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 the transliteration fails, returns nil.

local export = {}
local m_str_utils = require("Module:string utilities")
local gsub = m_str_utils.gsub
local sub = m_str_utils.sub
local upper = m_str_utils.upper
local lower = m_str_utils.lower
local len = m_str_utils.len

local letters = {
  ["м"] = "m", ["н"] = "n", ["б"] = "b", ["д"] = "d", ["г"] = "g",
  ["п"] = "p", ["т"] = "t", ["к"] = "k", ["ҡ"] = "q", ["ъ"] = "ʼ",
  ["ц"] = "c", ["ч"] = "č",
  ["в"] = "v", ["з"] = "z", ["ж"] = "ž", ["ғ"] = "ɣ",
  ["с"] = "s", ["ш"] = "š", ["ҳ"] = "x", ["х"] = "χ", ["ь"] = "h",
  ["р"] = "r", ["л"] = "l", ["й"] = "y",
  ["и"] = "i", ["у"] = "u", ["е"] = "e", ["а"] = "a", ["о"] = "o", ["я"] = "æ",

  -- Ejectives
  ["пь"] = "ṗ", ["ть"] = "ṭ", ["кь"] = "ḳ", ["ҡь"] = "q̇",
  ["ць"] = "c̣", ["чь"] = "č̣",
  
  -- Dialectal letters
  ["гӏ"] = "ʡ", ["хӏ"] = "ħ",
}

-- Generate upper-case and title-case variants
local mappings = {}
for k, v in pairs(letters) do
  mappings[k] = v
  mappings[upper(k)] = upper(v)
  if len(k) == 2 then
    mappings[gsub(k, "^.", upper)] = gsub(v, "^.", upper)
  end
end

function export.tr(text, lang, sc)
  local result = {}
  local i = 1

  while i <= len(text) do
    local one = sub(text, i, i)
    local two = one .. sub(text, i + 1, i + 1)
      
    local mapped = one
    if two ~= one and mappings[two] then
      mapped = mappings[two]
      i = i + 1 -- Skip next character if digraph matched
    elseif mappings[one] then
      mapped = mappings[one]
    end
    
    table.insert(result, mapped)
    i = i + 1
  end

  return table.concat(result)
end

return export