abreviaturas={
"af","sq","ar","be","bg","ca","zh-CN","hr","cs","da","nl","en","et","tl","fi","fr","gl","de","el",
"ht","iw","hi","hu","is","id","ga","it","ja","ko","lv","lt","mk","ms","mt","no","fa","pl","pt",
"ro","ru","sr","sl","es","sw","sv","tl","th","tr","uk","vi","cy","yi"}

completo={
"Afrikaans","Albanian","Arabic","Belarusian","Bulgarian","Catalan","Chinese","Croatian","Czech","Danish","Dutch",
"Inglés","Estonian","Filipino","Finnish","French","Galician","German","Greek","Haitian Creole","Hebrew","Hindi",
"Hungarian","Icelandic","Indonesian","Irish","Italian","Japanese","Korean","Latvian","Lithuanian","Macedonian","Malay",
"Maltese","Norwegian","Persian","Polish","Portugués","Romanian","Russian","Serbian","Slovenian",
"Español","Swahili","Swedish","Tagalog","Thai","Turkish","Ukrainian","Vietnamese","Welsh","Yiddish"}

function peticion()
tipo=false
http.get("http://translate.reference.com/translate?query="..conversor(palabra).."&src="..abreviaturas[origen].."&dst="..abreviaturas[destino],"a.txt")

a=io.open("a.txt","r+")
txt=a:read("*a")
a:close()


if string.find(txt,'<span class="BAB_CPPOSStyle">')!=nil then 
	a,b=string.find(txt,'<span class="BAB_CPPOSStyle">') txt=string.sub(txt,b+1,txt:len()) 
	if string.find(txt,'</span>')!=nil then a,b=string.find(txt,'</span>') txt=string.sub(txt,b+1,txt:len()) end
	if string.find(txt,'<br style')!=nil then a=string.find(txt,'<br style') txt=string.sub(txt,1,a-1) end
	
elseif string.find(txt,'<div class="translateTxt" >')!=nil then 
	a,b=string.find(txt,'<div class="translateTxt" >')
	txt=string.sub(txt,b+1,txt:len()) 
	if string.find(txt,'</div>')!=nil then a=string.find(txt,'</div>') txt=string.sub(txt,1,a-1) end
	
	if string.find(txt,'<div class="definition" dir="">')!=nil then 
		a,b=string.find(txt,'<div class="definition" dir="">')
		txt=string.sub(txt,b+1,txt:len()) 
		if string.find(txt,'<br style="clear : both; font-size : 1px;">')!=nil then a=string.find(txt,'<br style="clear : both; font-size : 1px;">') txt=string.sub(txt,1,a-1) end
	end


	txt=despaciador(txt)
	txt,lines=full_txt(txt,0.7,380)
	while lines>3 do
		txt=txt:sub(1,txt:len()-1)
		txt,lines=full_txt(txt,0.7,380)
	end
else
	os.message("No se ha podido encontrar traducción\nCompruebe el texto introducido e intentelo de nuevo.")
end

txt=nil
files.remove("a.txt")
end

function traductor()
liberar(10)
origen=12
destino=43
txt=""
palabra=""

while true do
controls.read()

stra[1].w=screen.textwidth(completo[origen],0.7)+10
stra[2].x=96+screen.textwidth("Traducir del:  "..completo[origen].."  al  ",0.7)
stra[2].w=screen.textwidth(completo[destino],0.7)+10
stra[5].x=stra[2].x+stra[2].w+10

if col(p,stra[3]) then 
	draw.gradrect(stra[3].x,stra[3].y,stra[3].w,stra[3].h,sky3,sky3,blanco,blanco)
	if controls.press("cross") then 
		if keyboard==1 then palabra=teclado(1,"Escribe tu palabra",30,"") else palabra=os.osk("Escribe tu palabra","",30,1,0) end
		palabra,lines=full_txt(palabra,0.7,380) 
	end
end
if col(p,stra[4]) then 
	draw.gradrect(stra[4].x,stra[4].y,stra[4].w,stra[4].h,sky3,sky3,blanco,blanco)
	if controls.press("cross") then peticion() end
end
if col(p,stra[5]) then 
	draw.gradrect(stra[5].x,stra[5].y,stra[5].w,stra[5].h,sky3,sky3,sky3,blanco)
	if controls.press("cross") then 
		a=origen b=destino destino=a origen=b
	end
end

screen.print(stra[3].x+10,stra[3].y+10,palabra,0.7,blanco,rojo)

if screen.textwidth(txt,0.7)<380 then
	screen.print(stra[4].x+10,stra[4].y+10,txt,0.7,blanco,rojo)
else
	screen.print(stra[4].x+10,stra[4].y+10,txt,0.7,blanco,rojo)
end

screen.print(stra[5].x+4,stra[5].y+4,"Invertir",0.7,blanco,azul,0)
draw.rect(stra[5].x,stra[5].y,stra[5].w,stra[5].h,azul)

for i=1,2 do
	if col(p,stra[i]) then 
	draw.gradrect(stra[i].x,stra[i].y,stra[i].w,stra[i].h,sky3,sky3,sky3,blanco)
	if not controls.triangle() then
		if i==1 and controls.press("down") then origen=origen+1 elseif i==1 and controls.press("up") then origen=origen-1 end
		if i==2 and controls.press("down") then destino=destino+1 elseif i==2 and controls.press("up") then destino=destino-1 end
	end
	end
	draw.rect(stra[i].x,stra[i].y,stra[i].w,stra[i].h,azul)
	draw.rect(stra[i+2].x,stra[i+2].y,stra[i+2].w,stra[i+2].h,azul)
end

if origen>#completo then origen=1 elseif origen<1 then origen=#completo end
if destino>#completo then destino=1 elseif destino<1 then destino=#completo end

screen.print(100,60,"Traducir del:  "..completo[origen].." al  "..completo[destino],0.7,blanco,azul,0)


if controls.start() then sistema() end
puntero()
screen.flip()
end
end
