Simple translation frontend, forked from
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.1 KiB

// gtranslate - Better front-end for Google Translate
// Copyright (C) 2021-present Alexey Yerin
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <>.
package main
import (
type TranslateSettings struct {
proxy *url.URL
userAgent string
func main() {
bind := pflag.StringP("bind", "b", ":5000", "Address to bind the server to, [addr]:port")
templatesDir := pflag.String("templates-dir", "./templates", "Templates directory")
staticDir := pflag.String("static-dir", "./static", "Static files directory")
proxy := pflag.String("proxy", "", "Proxy URL, with no scheme http is assumed")
userAgent := pflag.String("user-agent", "", "User-Agent header to use")
var proxyUrl *url.URL
if *proxy != "" {
u, err := url.Parse(*proxy)
if err != nil {
log.Fatalln("Invalid proxy URL:", err)
proxyUrl = u
settings := TranslateSettings{proxyUrl, *userAgent}
funcMap := template.FuncMap{
"title": strings.Title,
tmpl := template.Must(template.New("index").Funcs(funcMap).ParseGlob(path.Join(*templatesDir, "*.html")))
r := mux.NewRouter()
r.HandleFunc("/", CreateWebHandler(tmpl, &settings)).Methods("GET", "POST")
r.HandleFunc("/api", CreateApiHandler(&settings)).Methods("POST")
// Static files
fs := http.FileServer(http.Dir(*staticDir))
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
log.Println("Starting on", *bind)
log.Fatalln(http.ListenAndServe(*bind, r))