Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Cyrillic to Latin (and vice-versa) transliterator


  • Please log in to reply
No replies to this topic
Talisman
  • Moderators
  • 51 posts
  • Last active: Feb 26 2014 02:22 PM
  • Joined: 24 Feb 2010
A bit of work on the transliteration rules could be applied still, but this works pretty well, provided you know how to "speak" the cyrillic alphabet. For "special" characters, you will need to use ` (backtick) to apply the special rule... I used the ahk escape character for a reason, just don't ask me coz I can't remember :D now.

Note: This script is based on someone else's script, but for the life of me I can't seem to find it, but anyway, thanks for the script.

; #NoTrayIcon
#SingleInstance, force

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

TLFlag := False
Return

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

	StringSplit tokenRU, strTokenRU, `,
	StringSplit tokenEN, strTokenEN, `,
	counter := 0
	specialCounter := 0
	Loop Parse, str
	{
		isReplaced := false
		counter++

		If (SpecialCounter > 0) { ; We have added (1 or 2) characters to the token, so we need to
			SpecialCounter--        ; decrement the counter and
			Continue                ; skip them in the main loop.
		}
		token := A_LoopField
		; Special cases (multi-char)
		if ( (token == "y") || (token == "z") || (token == "t") || (token == "c") || (token == "s") || (token == "'")
		  || (token == "Y") || (token == "Z") || (token == "T") || (token == "C") || (token == "S") ) {
			token .= SubStr(str, (counter + 1), 1)
			If ( (token != "ya") && (token != "yo") && (token != "yu") && (token != "zh") && (token != "tz") && (token != "ch") && (token != "sc") && (token != "sh") && (token != "'i") && (token != "'e")
				&& (token != "Ya") && (token != "Yo") && (token != "Yu") && (token != "Zh") && (token != "Tz") && (token != "Ch") && (token != "Sc") && (token != "Sh") && (token != "'I") && (token != "'E") )
				token := substr(token, 1, 1)
			else
				SpecialCounter++
		}
		If ( (token == "sc")
			|| (token == "Sc") ) {
			token .= SubStr(str, (counter + 2), 1)
			SpecialCounter++
		}

		Loop, %tokenEN0%
		{
			; Normal 1 char case
			if (token == tokenEN%A_Index%) {
				isReplaced := true
				tokenReplace := tokenRU%A_Index%
				break
			}
		}
		if (isReplaced)
			strRes := strRes . tokenReplace
		else
			strRes := strRes . token
	}
;~ 	return PutUni(strRes)
	return, strRes
}

; я вас слушаю
; доброе утро
TransRU(str) {
  strTokenRU = а,б,в,г,д,е,ё,ж,з,и,й,к,л,м,н,о,п,р,с,т,у,ф,х,ц,ч,ш,щ,ъ,ы,ь,э,ю,я,А,Б,В,Г,Д,Е,Ё,Ж,З,И,Й,К,Л,М,Н,О,П,Р,С,Т,У,Ф,Х,Ц,Ч,Ш,Щ,Ъ,Ы,Ь,Э,Ю,Я
  strTokenEN = a,b,v,g,d,e,yo,zh,z,i,y,k,l,m,n,o,p,r,s,t,u,f,h,ts,ch,sh,sch,',i,,e,yu,ya,A,B,V,G,D,E,Yo,Zh,Z,I,Y,K,L,M,N,O,P,R,S,T,U,F,H,Ts,Ch,Sh,Sch,',I,,E,Yu,Ya
	strRes := ""
	StringSplit tokenRU, strTokenRU, `,
	StringSplit tokenEN, strTokenEN, `,
	Loop Parse, str
	{
		token := A_LoopField
		isReplaced := false
		Loop %tokenRU0%
		{
			if (token == tokenRU%A_Index%) {
				isReplaced := true
				tokenReplace := tokenEN%A_Index%
				break
			}
		}
		if (isReplaced)
			strRes := strRes . tokenReplace
		else
			strRes := strRes . token
	}
	return strRes
}

GuiClose:
{
	ExitApp
	Return
}

#t:: Suspend, Toggle
#!,::
{
	Clipboard := ""
	SendInput, {CtrlDown}{Left}{ShiftDown}{Right}{ShiftUp}c{CtrlUp}
	TLFlag := True
}
#,::
{
	If (!TLFlag) {
		Clipboard := ""
		SendInput, ^c
	}
	ClipWait
	TLFlag := False
	SendInput, % TransEN(Clipboard)
	Return
}
#!.::
{
	Clipboard := ""
	SendInput, {CtrlDown}{Left}{ShiftDown}{Right}{ShiftUp}c{CtrlUp}
	TLFlag := True
}
#.::
{
	If (!TLFlag) {
		Clipboard := ""
		SendInput, ^c
	}
	ClipWait
	TLFlag := False
	SendInput, % TransRU(Clipboard)
	Return
}

Return

Tal.