This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
hilburn's profile picture

Terminal Glasses Translate (OpenPeripherals, MoarPeripherals)

Started by hilburn, 15 July 2014 - 11:55 AM
hilburn #1
Posted 15 July 2014 - 01:55 PM
A friend of mine (American) started playing on a server with a couple of his Canadian buddies, unfortunately the majority of the chat is in French and he feels a little left out at times

To remedy this I made him this code which uses OP and MP to automatically translate (using viluon's PHP script here) any French chat and display it on his terminal glasses. It also allows him to type in English and have the Chatbox say it in French.

The Code

Do NOT use in SMP for now - the translate script will get blocked by Google, will update when this is fixed.

Spoiler

--#Utilises viluon's Google Translate - http://www.computercraft.info/forums2/index.php?/topic/18568-google-translate-in-cc/

glass = peripheral.wrap("left") -- #Terminal Glasses (OpenPeripherals)
chat = peripheral.wrap("right") -- #Chatbox (MoarPeripherals)

glass.clear()

in_lang = "en"
out_lang = "fr"

me = "playername"

encode = textutils.urlEncode

translations = {}
chatTimers = {}

term.clear()
term.setCursorPos(1,1)
print("Terminal Glasses Translate By ValiarMarcus")
print("Powered by Google Translate c/o viluon")

function responseClean(rStr)
	rStr=string.sub(rStr,string.find(rStr,"<parse>(.-)</parse>")) --#cleans out crazy header
	rStr=string.gsub(rStr,"<(.-)>","") --#cleans out parses
	rStr=string.gsub(rStr,"\n","") --#cleans out random newline
	rStr=string.gsub(rStr,"%+"," ") --#re-inserts spaces
	return rStr
end

function outputFormat(oStr, lang)
	oStr="http://acos.bluefile.cz/translate.php?input="..encode(oStr).."&from=auto&to="..lang
	return oStr
end

function translate(str, lang)
	return responseClean(http.get(outputFormat(str,lang)).readAll())
end

function glassOutput(user, message)
	local output = string.format("<%s> %s", user, message)
	translations[#translations+1] = output
	chatTimers[os.startTimer(5)]=true
	glassPrint()
end

function glassPrint()
	glass.clear()
	glass.addBox(1,10,320,#translations*12,0x000000,0.4)
	for i,j in pairs(translations) do
		glass.addText(5,i*12,j,0xFFFFFF)
	end
end

function chatPrint(name, message)
	local output = string.format("<%s> %s", user, message)
	chat.say(output)
end

while true do
	local event, param1, param2, param3, param4 = os.pullEvent()
	if event == "chat_message" then
		local translation = translate(param3,in_lang)
		if translation ~= param3 then --#only print if it wasn't in English
			glassOutput(param2, translation)
		end
	elseif event == "chat_command" then
		chatPrint(me, translate(param1, out_lang))
	elseif event == "timer" then
		table.remove(translations,1)
		chatTimers[param1]=nil
		glassPrint()
	end
end

The Pastebin
http://pastebin.com/DdYiCudC

The P.S.
I know I could have used the Chatbox's tell() method which would have freed up a headslot and given my current failure to do text-wrapping would probably have looked better, but he wanted a HUD
Edited on 22 July 2014 - 09:21 PM
viluon #2
Posted 15 July 2014 - 01:57 PM
Proud to see somebody's using it :)/>

Edit: though you apparently forgot about

You can use this if:
You will PM me first and ask for detailed information
You will only use the script inside CC
You will only provide the product using my script for free
You will give me the credit for this script visible inside both the program (eg sth like about tab) and the source code

[Why should I PM you?]
As @ApeManZilla mentioned, the utility may stop working if Google realizes its a bot. If you plan to bombard the script with server chat, ask me first so I can get ready for it (you know, some Ajax request header magic and we're good to go).
Edited on 15 July 2014 - 12:13 PM
hilburn #3
Posted 15 July 2014 - 02:02 PM
Whoa, scary quick response, are you like Beetlejuice? Does mentioning your name summon you? :P/>

Seriously though, he thanks you muchly for that script and I thank you for enabling a fun little project
theoriginalbit #4
Posted 15 July 2014 - 02:43 PM
this is quite a novel idea! :)/> I quite like the use of my ChatBox ;)/> I'm glad to see people starting to use my mod :)/>

To extend on your P.S. using the tell would also mass spam his chat.
hilburn #5
Posted 15 July 2014 - 02:55 PM
The server I play on doesn't have MoarPeripherals, I installed it for single player to test this code for him while I was writing it. Now my server admin is getting annoyed at me constantly bugging him to add it in ;)/>

Proud to see somebody's using it :)/>

Edit: though you apparently forgot about

You can use this if:
You will PM me first and ask for detailed information
You will only use the script inside CC
You will only provide the product using my script for free
You will give me the credit for this script visible inside both the program (eg sth like about tab) and the source code

[Why should I PM you?]
As @ApeManZilla mentioned, the utility may stop working if Google realizes its a bot. If you plan to bombard the script with server chat, ask me first so I can get ready for it (you know, some Ajax request header magic and we're good to go).

Very sorry, I derped. He's agreed to stop using it until we get things sorted out. Changed the Pastebin and code in the spoiler to give you credit
Edited on 15 July 2014 - 02:11 PM
oeed #6
Posted 16 July 2014 - 12:24 AM
Wow.

This is genius! Well done to you.
hilburn #7
Posted 16 July 2014 - 05:59 AM
Coming from you that is high praise :)/>
Geforce Fan #8
Posted 20 July 2014 - 07:07 PM
Can it do any language? It sounds awesome!
You might want to simply have the chatbox say it to him, though.
I feel like joining a different-language server with these 2 peripherals just so I can try it out.
Edited on 20 July 2014 - 05:12 PM
hilburn #9
Posted 21 July 2014 - 12:12 AM
Anything that google translate can pick up as "XX" language can be translated into english, and you can translate into any langauge and text prefaced with ## (the chatbox command precursor) so long as you change the "fr" in the code to whatever other code is used by google for the language in question.

The reason I didn't get it to send it to the player's chat is that it would result in a LOT of text going through there and you could lose track of the conversation quite quickly, plus this way was more fun. However if you feel it would work better a different way then feel free to modify it however you like.

The only thing I would ask is that as pointed out above I actually screwed up posting it, viluon is currently working on a fix for his google translate mirror which will (hopefully) let it work when bombarded with server chat, until he gives the goahead please don't implement this in a SMP server as it could get it blocked by google
Cookiezi #10
Posted 21 July 2014 - 07:15 PM
This is a nice tool, I'm Japanese so my English isn't so good.
Could I use this except with Japanese and English?
rhyleymaster #11
Posted 22 July 2014 - 06:20 PM
It's a great program, just a little bug I found. If you continuously use it in SMP, you can get the server blocked by google.
secret6timb1 #12
Posted 30 July 2014 - 08:59 PM
Can someone help me? What is OP and MP? I googled it and nothing
LeonTheMisfit #13
Posted 30 July 2014 - 09:22 PM
Can someone help me? What is OP and MP? I googled it and nothing

OP = OpenPeripheral
MP = MoarPeripherals
secret6timb1 #14
Posted 30 July 2014 - 10:30 PM
(Had two windows open and accidentally posted to this forum again)
Edited on 30 July 2014 - 09:38 PM