Модул:Transliteration

С Википедије, слободне енциклопедије
Документација модула[прикажи] [уреди] [историја] [освежи]

Пресловљавање у ћирилицу или латиницу. Извози две функције:

  • cyrillize( text )
  • latinize( text )

Свака има по један аргумент, текст за пресловљавање.

За историју овог ко̑да, погледајте историју модула TitleReplace, одакле је овај код пренешен.

-- Module for different search and replace operations on strings.
local p = {}

-- Takes one string parameter, and returns the string with all characters with special meaning for Lua patterns escaped with a preceding `%`.
function p.escape_pattern(text)
    -- Replaces each occurrence of any of ().%+-*?[^$ with a `%` and then the character.
    local r = mw.ustring.gsub(text, "[%(%)%.%%%+%-%*%?%[%^%$]", "%%%1")
    return r
end

-- Returns the first parameter, with all occurrences of the second parameter replaced with the third parameter.
-- All special characters are ignored: {{#invoke:StringReplace|replace_all|test.a%1$foo|%1|bar}} results in `test.abarfoo`.
function p.replace_all(frame)
    local str = frame.args[1]
    local strToFind = frame.args[2]
    local strToreplaceWith = frame.args[3]
    local r = mw.ustring.gsub(str, p.escape_pattern(strToFind), p.escape_pattern(strToreplaceWith))
    return r
end

p.latinize = function(text)
	local x = mw.ustring.gsub( 
		text, 
		'[АаБбЦцЧчЋћДдЏџЂђЕеФфГгХхИиЈјКкЛлЉљМмНнЊњОоПпРрСсШшТтУуВвЗзЖж]', 
		{ 
			['А'] = 'A',
			['Б'] = 'B',
			['Ц'] = 'C',
			['Ч'] = 'Č',
			['Ћ'] = 'Ć',
			['Д'] = 'D',
			['Џ'] = 'Dž',
			['Ђ'] = 'Đ',
			['Е'] = 'E',
			['Ф'] = 'F',
			['Г'] = 'G',
			['Х'] = 'H',
			['И'] = 'I',
			['Ј'] = 'J',
			['К'] = 'K',
			['Л'] = 'L',
			['Љ'] = 'Lj',
			['М'] = 'M',
			['Н'] = 'N',
			['Њ'] = 'Nj',
			['О'] = 'O',
			['П'] = 'P',
			['Р'] = 'R',
			['С'] = 'S',
			['Ш'] = 'Š',
			['Т'] = 'T',
			['У'] = 'U',
			['В'] = 'V',
			['З'] = 'Z',
			['Ж'] = 'Ž',
			['а'] = 'a',
			['б'] = 'b',
			['ц'] = 'c',
			['ч'] = 'č',
			['ћ'] = 'ć',
			['д'] = 'd',
			['џ'] = 'dž',
			['ђ'] = 'đ',
			['е'] = 'e',
			['ф'] = 'f',
			['г'] = 'g',
			['х'] = 'h',
			['и'] = 'i',
			['ј'] = 'j',
			['к'] = 'k',
			['л'] = 'l',
			['љ'] = 'lj',
			['м'] = 'm',
			['н'] = 'n',
			['њ'] = 'nj',
			['о'] = 'o',
			['п'] = 'p',
			['р'] = 'r',
			['с'] = 's',
			['ш'] = 'š',
			['т'] = 't',
			['у'] = 'u',
			['в'] = 'v',
			['з'] = 'z',
			['ж'] = 'ž',
		} 
	)
	return mw.text.trim( x )
end

p.cyrillize = function(text)
	local y = ''
	y = mw.ustring.gsub( 
		text, 
		'Dž', 
		{ 
			['Dž'] = 'Џ',
		} 
	)
	y = mw.ustring.gsub(y, 
		'Lj', 
		{ 
			['Lj'] = 'Љ',
		} 
	)
	y = mw.ustring.gsub(y, 
		'Nj', 
		{ 
			['Nj'] = 'Њ',
		} 
	)
	y = mw.ustring.gsub(y, 
		'dž', 
		{ 
			['dž'] = 'џ',
		} 
	)
	y = mw.ustring.gsub(y, 
		'lj', 
		{ 
			['lj'] = 'љ',
		} 
	)
	y = mw.ustring.gsub(y, 
		'nj', 
		{ 
			['nj'] = 'њ',
		} 
	)
	y = mw.ustring.gsub(y, 
		'[AaBbCcČčĆćDdĐđEeFfGgHhIiJjKkLlMmNnOoPpRrSsŠšTtUuVvZzŽž]', 
		{ 
			['A'] = 'А',
			['B'] = 'Б',
			['C'] = 'Ц',
			['Č'] = 'Ч',
			['Ć'] = 'Ћ',
			['D'] = 'Д',
			['Đ'] = 'Ђ',
			['E'] = 'Е',
			['F'] = 'Ф',
			['G'] = 'Г',
			['H'] = 'Х',
			['I'] = 'И',
			['J'] = 'Ј',
			['K'] = 'К',
			['L'] = 'Л',
			['M'] = 'М',
			['N'] = 'Н',
			['O'] = 'О',
			['P'] = 'П',
			['R'] = 'Р',
			['S'] = 'С',
			['Š'] = 'Ш',
			['T'] = 'Т',
			['U'] = 'У',
			['V'] = 'В',
			['Z'] = 'З',
			['Ž'] = 'Ж',
			['a'] = 'а',
			['b'] = 'б',
			['c'] = 'ц',
			['č'] = 'ч',
			['ć'] = 'ћ',
			['d'] = 'д',
			['đ'] = 'ђ',
			['e'] = 'е',
			['f'] = 'ф',
			['g'] = 'г',
			['h'] = 'х',
			['i'] = 'и',
			['j'] = 'ј',
			['k'] = 'к',
			['l'] = 'л',
			['m'] = 'м',
			['n'] = 'н',
			['o'] = 'о',
			['p'] = 'п',
			['r'] = 'р',
			['s'] = 'с',
			['š'] = 'ш',
			['t'] = 'т',
			['u'] = 'у',
			['v'] = 'в',
			['z'] = 'з',
			['ž'] = 'ж',
		} 
	)
	return mw.text.trim( y )
end

return p