diff --git a/README.md b/README.md index 68cca9c2..1cc64f88 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,11 @@ See [howto](https://github.com/42wim/matterbridge/wiki/How-to-create-your-config ## Advanced configuration * [matterbridge.toml.sample](https://github.com/42wim/matterbridge/blob/master/matterbridge.toml.sample) for documentation and an example. +* If you'd like to allow channels to be Google Translated between + languages, then simply set an environment variable called +`GOOGLE_APPLICATION_CREDENTIALS_BASE64` to be the base64 encoded version +of the JSON credentials file. + ## Examples ### Bridge mattermost (off-topic) - irc (#testing) ``` diff --git a/gateway/gateway.go b/gateway/gateway.go index ab69f5cd..370659c5 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -2,6 +2,7 @@ package gateway import ( "bytes" + b64 "encoding/base64" "fmt" "io/ioutil" "net/http" @@ -27,6 +28,8 @@ import ( log "github.com/sirupsen/logrus" // "github.com/davecgh/go-spew/spew" "cloud.google.com/go/translate" + "google.golang.org/api/option" + "golang.org/x/oauth2/google" "golang.org/x/text/language" "crypto/sha1" "path/filepath" @@ -268,11 +271,14 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM msg.Avatar = gw.modifyAvatar(origmsg, dest) msg.Username = gw.modifyUsername(origmsg, dest) msg.ID = "" - if channel.Options.Locale != "" { + credsEnc, ok := os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS_BASE64") + if ok && channel.Options.Locale != "" { ctx := context.Background() lang, _ := language.Parse(channel.Options.Locale) - client, _ := translate.NewClient(ctx) + credsDec, _ := b64.StdEncoding.DecodeString(credsEnc) + creds, _ := google.CredentialsFromJSON(ctx, credsDec, translate.Scope) + client, _ := translate.NewClient(ctx, option.WithCredentials(creds)) defer client.Close() resp, _ := client.Translate(ctx, []string{msg.Text}, lang, nil)