Gmod Translator

Here’s a simple translator I made while I was bored. I though I would post it if anyone wants it. Just throw it in a lua file in lua/autorun/client.
[lua]local english = “en”
local spanish = “es”
local german = “de”
local french = “fr”
local function ChatTranslate(ply, text, teamchat, isdead)
Translate(“fr”, “en”, text)
end
hook.Add(“OnPlayerChat”, “Translate”, ChatTranslate)

local function TranslateToChat(ply, cmd, args)
local text = string.Implode(" ", args)
TranslateTo(“en”, “fr”, text)
end
concommand.Add(“translate”, TranslateToChat)

function TranslateTo(from, to, text)
local text = text:gsub("%s", “%%20”)
http.Get(“http://translate.google.com/translate_a/t?client=t&text="..text.."&hl="..to.."&sl="..from.."&tl="..to.."&pc=0”, “”, TranslateToCallback)
end

function TranslateToCallback(contents, size)
local tab = string.Explode(",", contents)
local translated = string.Right(tab[1], string.len(tab[1])-4)
local translated = string.Left(translated, string.len(translated)-1)
RunConsoleCommand(“say”, translated)
end

function Translate(from, to, text)
local text = text:gsub("%s", “%%20”)
http.Get(“http://translate.google.com/translate_a/t?client=t&text="..text.."&hl="..to.."&sl="..from.."&tl="..to.."&pc=0”, “”, TranslateCallback)
end

function TranslateCallback(contents, size)
local tab = string.Explode(",", contents)
local translated = string.Right(tab[1], string.len(tab[1])-3)
chat.AddText(translated)
end[/lua]

If you want me to add more to it and make it more useable, please feel free to pester me into doing so.

Nice release! Good for servers who attract non-english speaking people, adding more languages could make it purfect!

For the love of god remove string.Replace and use gsub.

Could you give me an example of how to do that? When I tried it errored with invalid pattern or something like that.

EDIT: Fixed.

[editline]24th January 2011[/editline]

Here is a serverside implementation that will automatically translate anyone who’s language is set differently than yours.
[lua]AddCSLuaFile(“languages.lua”)

local languages = {}
if CLIENT then
local langs = {}
langs[“english”] = “en”
langs[“spanish”] = “es”
langs[“german”] = “de”
langs[“french”] = “fr”

local function ChatTranslate(ply, text, teamchat, isdead)
	if ply:Language() == LocalPlayer():Language() then return end
	Translate(ply:Language(), LocalPlayer():Language(), text)
end
hook.Add("OnPlayerChat", "Translate", ChatTranslate)
 
local function TranslateToChat(ply, cmd, args)
	local text = string.Implode(" ", args)
	TranslateTo("en", "fr", text)
end
concommand.Add("translate", TranslateToChat)
 
function TranslateTo(from, to, text)
	local text = text:gsub("%s", "%%20")
	http.Get("http://translate.google.com/translate_a/t?client=t&text="..text.."&hl="..to.."&sl="..from.."&tl="..to.."&pc=0", "", TranslateToCallback)
end
 
function TranslateToCallback(contents, size)
	local tab = string.Explode(",", contents)
	local translated = string.Right(tab[1], string.len(tab[1])-4)
	local translated = string.Left(translated, string.len(translated)-1)
	RunConsoleCommand("say", translated)
end
 
function Translate(from, to, text)
	local text = text:gsub("%s", "%%20")
	http.Get("http://translate.google.com/translate_a/t?client=t&text="..text.."&hl="..to.."&sl="..from.."&tl="..to.."&pc=0", "", TranslateCallback)
end
 
function TranslateCallback(contents, size)
	local tab = string.Explode(",", contents)
	local translated = string.Right(tab[1], string.len(tab[1])-3)
	chat.AddText(translated)
end

languages.table = {}

local meta = FindMetaTable("Player")

function meta:Language()
	local language = languages.table[self:UniqueID()]
	if not language then
		return "en"
	end
	return langs[language]
end

local function recieve(msg)
	languages.table[msg:ReadString()] = msg:ReadString()
end
usermessage.Hook("LanguageData", recieve)

local function prompt(msg)
	Derma_StringRequest("Languages", "What language do you speak?", "english", function(text) RunConsoleCommand("mylanguage", text) end)
end
usermessage.Hook("LanguagePrompt", prompt)

else

local function send(ply, lang)
	umsg.Start("LanguageData")
		umsg.String(ply:UniqueID())
		umsg.String(lang)
	umsg.End()
end

function languages.set(ply, cmd, args)
	send(ply, args[1])
end
concommand.Add("mylanguage", languages.set)

function languages.prompt(ply)
	umsg.Start("LanguagePrompt", ply)
	umsg.End()
end

local function spawnedply(ply)
	if not ply.lang then
		ply.lang = true
		languages.prompt(ply)
	end
end
hook.Add("PlayerSpawn", "LanguagesPrompt", spawnedply)

end[/lua]
Sorry about it looking like crap, but I’m pretty sure it works.

Aha nice!

Yeah this is actually cool, now niggers, beaners, and chinks can all come and play together!

[editline]23rd January 2011[/editline]

inb4ban4racist


(User was banned for this post ("Racism" - Swebonny))


(User was permabanned for this post ("Extended. Breaking rules on an alt also long ban history. Will not learn." - Swebonny))

Thank you for not including italian.
/sarcasm
:colbert:

Hey italian coders don’t deserve a traduction (considering they’re all skids)

If anyone wants me to add the code for other languages, just ask.

Why not expand on this and make a derma, with a combo box letting you choose from which language to which?

The serverside one has a Dialog that prompts what language you speak so the other users will have it automatically translated to their language if it is different. But I can do it to the clientside one if you wish.

Well it would be cool if there was like a combo box and a text box on the left side of the menu, in the combo box were all the languages you chose one wrote in the text box, then chose the language to translate to in another combo box on the right side then when you push GO or whatever it puts the translated text into the other textbox

Original idea to use googles translate function inside gmod.
I like it!

I call this… Smart idea.

Now make one that automatically responds with answers from cleverbot.

R.I.P. c-unit

R.I.P him. Well, you should make one on danish language too.

lol why would he do that? thats just encouraging danish kids to come on your server and rage

Well, the fact is that many people understand danish better than English, and as you see, danish is used by many people in Garry’s Mod.

English is used more…

I’m liking it, seems like it would be great to use to help aid those annoying people who talk French when you clearly state it’s an English server.