├── .gitignore ├── go.mod ├── go.sum ├── main_test.go ├── main.go └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # Ignore executable file 15 | google-translate 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/ismalzikri/google-translate 2 | 3 | go 1.20 4 | 5 | require ( 6 | github.com/bregydoc/gtranslate v0.0.0-20200913051839-1bd07f6c1fc5 // indirect 7 | github.com/robertkrimen/otto v0.2.1 // indirect 8 | github.com/rs/cors v1.9.0 // indirect 9 | golang.org/x/text v0.11.0 // indirect 10 | gopkg.in/sourcemap.v1 v1.0.5 // indirect 11 | ) 12 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/bregydoc/gtranslate v0.0.0-20200913051839-1bd07f6c1fc5 h1:fpVDaadW68V+6vqxJHU9jrW0/z1i2MQYJZyk9w2cjpw= 2 | github.com/bregydoc/gtranslate v0.0.0-20200913051839-1bd07f6c1fc5/go.mod h1:153ZQv0q0e2+tPGhDsQsYTRlVRTWIYMicEvriLNX2ZY= 3 | github.com/gilang-as/google-translate v0.1.1 h1:iI2kZg3y0/rxYH9O/7XInko/CB/4p0nalFrHkjpDM8Y= 4 | github.com/gilang-as/google-translate v0.1.1/go.mod h1:9orAAseM2cuv1ikQR20Jto90ryuxo+Tx6Vfh8PQ8lkY= 5 | github.com/robertkrimen/otto v0.2.1 h1:FVP0PJ0AHIjC+N4pKCG9yCDz6LHNPCwi/GKID5pGGF0= 6 | github.com/robertkrimen/otto v0.2.1/go.mod h1:UPwtJ1Xu7JrLcZjNWN8orJaM5n5YEtqL//farB5FlRY= 7 | github.com/rs/cors v1.9.0 h1:l9HGsTsHJcvW14Nk7J9KFz8bzeAWXn3CG6bgt7LsrAE= 8 | github.com/rs/cors v1.9.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= 9 | golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4= 10 | golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= 11 | gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI= 12 | gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78= 13 | -------------------------------------------------------------------------------- /main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "net/http" 7 | "net/http/httptest" 8 | "testing" 9 | ) 10 | 11 | func TestTranslateHandler(t *testing.T) { 12 | // Create a test request payload 13 | requestPayload := TranslateRequest{ 14 | Text: "Hello", 15 | To: "es", 16 | } 17 | payloadBytes, _ := json.Marshal(requestPayload) 18 | 19 | // Create a test request 20 | req, err := http.NewRequest("POST", "/translate", bytes.NewBuffer(payloadBytes)) 21 | if err != nil { 22 | t.Fatal(err) 23 | } 24 | 25 | // Create a response recorder to capture the response 26 | rr := httptest.NewRecorder() 27 | 28 | // Call the TranslateHandler function directly 29 | handler := http.HandlerFunc(TranslateHandler) 30 | handler.ServeHTTP(rr, req) 31 | 32 | // Check the response status code 33 | if rr.Code != http.StatusOK { 34 | t.Errorf("Expected status code %v, but got %v", http.StatusOK, rr.Code) 35 | } 36 | 37 | // Parse the response body 38 | var response TranslateResponse 39 | err = json.Unmarshal(rr.Body.Bytes(), &response) 40 | if err != nil { 41 | t.Errorf("Failed to unmarshal response body: %v", err) 42 | } 43 | 44 | // Check the response fields 45 | if response.TranslatedText == "" { 46 | t.Error("TranslatedText field is empty") 47 | } 48 | if response.Status != true { 49 | t.Error("Status field is not true") 50 | } 51 | if response.Message != "" { 52 | t.Error("Message field is not empty") 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "log" 7 | "net/http" 8 | 9 | "github.com/bregydoc/gtranslate" 10 | "github.com/rs/cors" 11 | ) 12 | 13 | type TranslateRequest struct { 14 | Text string `json:"text"` 15 | To string `json:"to"` 16 | } 17 | 18 | type TranslateResponse struct { 19 | TranslatedText string `json:"translatedText,omitempty"` 20 | Status bool `json:"status"` 21 | Message string `json:"message"` 22 | } 23 | 24 | func TranslateHandler(w http.ResponseWriter, r *http.Request) { 25 | if r.Method != http.MethodPost { 26 | http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) 27 | return 28 | } 29 | 30 | var request TranslateRequest 31 | err := json.NewDecoder(r.Body).Decode(&request) 32 | if err != nil { 33 | sendErrorResponse(w, "Invalid request payload", http.StatusBadRequest) 34 | return 35 | } 36 | 37 | translated, err := gtranslate.TranslateWithParams(request.Text, gtranslate.TranslationParams{ 38 | From: "auto", 39 | To: request.To, 40 | }) 41 | if err != nil { 42 | sendErrorResponse(w, "Translation failed", http.StatusInternalServerError) 43 | return 44 | } 45 | 46 | response := TranslateResponse{ 47 | TranslatedText: translated, 48 | Status: true, 49 | Message: "", 50 | } 51 | 52 | sendJSONResponse(w, response, http.StatusOK) 53 | } 54 | 55 | func sendErrorResponse(w http.ResponseWriter, message string, statusCode int) { 56 | response := TranslateResponse{ 57 | Status: false, 58 | Message: message, 59 | } 60 | sendJSONResponse(w, response, statusCode) 61 | } 62 | 63 | func sendJSONResponse(w http.ResponseWriter, data interface{}, statusCode int) { 64 | w.Header().Set("Content-Type", "application/json") 65 | w.WriteHeader(statusCode) 66 | err := json.NewEncoder(w).Encode(data) 67 | if err != nil { 68 | log.Printf("Failed to encode JSON response: %v", err) 69 | } 70 | } 71 | 72 | func main() { 73 | mux := http.NewServeMux() 74 | mux.HandleFunc("/translate", TranslateHandler) 75 | 76 | c := cors.Default().Handler(mux) 77 | 78 | fmt.Println("Starting server on http://localhost:8000") 79 | log.Fatal(http.ListenAndServe(":8000", c)) 80 | } 81 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Free Translate API 2 | 3 | Welcome to the Free Translate API! This API allows you to perform translations between different languages without any limits. It's a free and open-source project ported and baked by 4 | [Ismal Zikri](https://twitter.com/ZikriIsmal) . 5 | 6 | ## Table of Contents 7 | 8 | - [Getting Started](#getting-started) 9 | - [Installation](#installation) 10 | - [Usage](#usage) 11 | - [Supported Languages](#supported-languages) 12 | - [API Endpoints](#api-endpoints) 13 | - [Contributing](#contributing) 14 | - [License](#license) 15 | 16 | ## Getting Started 17 | 18 | ### Installation 19 | 20 | To use the Free Translate API, you need to have [Golang](https://golang.org/dl/) installed on your system. You can then follow these steps to get started: 21 | 22 | Clone the repository: 23 | 24 | ```bash 25 | git clone https://github.com/ismalzikri/free-translate-api.git 26 | 27 | cd free-translate-api 28 | 29 | ``` 30 | 31 | ## usage 32 | Start the API server: 33 | ```bash 34 | go run main.go 35 | ``` 36 | The API server will start running on http://localhost:8000. 37 | 38 | You can now send translation requests to the API using HTTP POST requests. The endpoint for translation is /translate. Here's an example using curl: 39 | ```bash 40 | curl -X POST -H "Content-Type: application/json" -d '{"text": "Hello", "to": "id"}' http://localhost:8000/translate 41 | ``` 42 | 43 | ## Supported Languages 44 | The Free Translate API supports translation between a wide range of languages. You can check this a list of all supported languages and their language codes. 45 | ``` 46 | AFAR = "aa" 47 | ABKHAZIAN = "ab" 48 | AFRIKAANS = "af" 49 | AKAN = "ak" 50 | AMHARIC = "am" 51 | ARAGONESE = "an" 52 | ARABIC = "ar" 53 | ASSAMESE = "as" 54 | AVAR = "av" 55 | AYMARA = "ay" 56 | AZERBAIJANI = "az" 57 | BASHKIR = "ba" 58 | BELARUSIAN = "be" 59 | BULGARIAN = "bg" 60 | BIHARI = "bh" 61 | BISLAMA = "bi" 62 | BAMBARA = "bm" 63 | BENGALI = "bn" 64 | TIBETAN = "bo" 65 | BRETON = "br" 66 | BOSNIAN = "bs" 67 | CATALAN = "ca" 68 | CHECHEN = "ce" 69 | CHAMORRO = "ch" 70 | CORSICAN = "co" 71 | CREE = "cr" 72 | CZECH = "cs" 73 | OLD_CHURCH_SLAVONIC = "cu" 74 | OLD_BULGARIAN = "cu" 75 | CHUVASH = "cv" 76 | WELSH = "cy" 77 | DANISH = "da" 78 | GERMAN = "de" 79 | DIVEHI = "dv" 80 | DZONGKHA = "dz" 81 | EWE = "ee" 82 | GREEK = "el" 83 | ENGLISH = "en" 84 | ESPERANTO = "eo" 85 | SPANISH = "es" 86 | ESTONIAN = "et" 87 | BASQUE = "eu" 88 | PERSIAN = "fa" 89 | PEUL = "ff" 90 | FINNISH = "fi" 91 | FIJIAN = "fj" 92 | FAROESE = "fo" 93 | FRENCH = "fr" 94 | WEST_FRISIAN = "fy" 95 | IRISH = "ga" 96 | SCOTTISH_GAELIC = "gd" 97 | GALICIAN = "gl" 98 | GUARANI = "gn" 99 | GUJARATI = "gu" 100 | MANX = "gv" 101 | HAUSA = "ha" 102 | HEBREW = "he" 103 | HINDI = "hi" 104 | HIRI_MOTU = "ho" 105 | CROATIAN = "hr" 106 | HAITIAN = "ht" 107 | HUNGARIAN = "hu" 108 | ARMENIAN = "hy" 109 | HERERO = "hz" 110 | INTERLINGUA = "ia" 111 | INDONESIAN = "id" 112 | INTERLINGUE = "ie" 113 | IGBO = "ig" 114 | SICHUAN_YI = "ii" 115 | INUPIAK = "ik" 116 | IDO = "io" 117 | ICELANDIC = "is" 118 | ITALIAN = "it" 119 | INUKTITUT = "iu" 120 | JAPANESE = "ja" 121 | JAVANESE = "jv" 122 | GEORGIAN = "ka" 123 | KONGO = "kg" 124 | KIKUYU = "ki" 125 | KUANYAMA = "kj" 126 | KAZAKH = "kk" 127 | GREENLANDIC = "kl" 128 | CAMBODIAN = "km" 129 | KANNADA = "kn" 130 | KOREAN = "ko" 131 | KANURI = "kr" 132 | KASHMIRI = "ks" 133 | KURDISH = "ku" 134 | KOMI = "kv" 135 | CORNISH = "kw" 136 | KIRGHIZ = "ky" 137 | LATIN = "la" 138 | LUXEMBOURGISH = "lb" 139 | GANDA = "lg" 140 | LIMBURGIAN = "li" 141 | LINGALA = "ln" 142 | LAOTIAN = "lo" 143 | LITHUANIAN = "lt" 144 | LATVIAN = "lv" 145 | MALAGASY = "mg" 146 | MARSHALLESE = "mh" 147 | MAORI = "mi" 148 | MACEDONIAN = "mk" 149 | MALAYALAM = "ml" 150 | MONGOLIAN = "mn" 151 | MOLDOVAN = "mo" 152 | MARATHI = "mr" 153 | MALAY = "ms" 154 | MALTESE = "mt" 155 | BURMESE = "my" 156 | NAURUAN = "na" 157 | NORTH_NDEBELE = "nd" 158 | NEPALI = "ne" 159 | NDONGA = "ng" 160 | DUTCH = "nl" 161 | NORWEGIAN_NYNORSK = "nn" 162 | NORWEGIAN = "no" 163 | SOUTH_NDEBELE = "nr" 164 | NAVAJO = "nv" 165 | CHICHEWA = "ny" 166 | OCCITAN = "oc" 167 | OJIBWA = "oj" 168 | OROMO = "om" 169 | ORIYA = "or" 170 | OSSETIAN = "os" 171 | PUNJABI = "pa" 172 | PALI = "pi" 173 | POLISH = "pl" 174 | PASHTO = "ps" 175 | PORTUGUESE = "pt" 176 | QUECHUA = "qu" 177 | RAETO_ROMANCE = "rm" 178 | KIRUNDI = "rn" 179 | ROMANIAN = "ro" 180 | RUSSIAN = "ru" 181 | RWANDI = "rw" 182 | SANSKRIT = "sa" 183 | SARDINIAN = "sc" 184 | SINDHI = "sd" 185 | SANGO = "sg" 186 | SERBO_CROATIAN = "sh" 187 | SINHALESE = "si" 188 | SLOVAK = "sk" 189 | SLOVENIAN = "sl" 190 | SAMOAN = "sm" 191 | SHONA = "sn" 192 | SOMALIA = "so" 193 | ALBANIAN = "sq" 194 | SERBIAN = "sr" 195 | SWATI = "ss" 196 | SOUTHERN_SOTHO = "st" 197 | SUNDANESE = "su" 198 | SWEDISH = "sv" 199 | SWAHILI = "sw" 200 | TAMIL = "ta" 201 | TELUGU = "te" 202 | TAJIK = "tg" 203 | THAI = "th" 204 | TIGRINYA = "ti" 205 | TURKMEN = "tk" 206 | TAGALOG = "tl" 207 | TSWANA = "tn" 208 | TONGA = "to" 209 | TURKISH = "tr" 210 | TSONGA = "ts" 211 | TATAR = "tt" 212 | TWI = "tw" 213 | TAHITIAN = "ty" 214 | UYGHUR = "ug" 215 | UKRAINIAN = "uk" 216 | URDU = "ur" 217 | VENDA = "ve" 218 | VIETNAMESE = "vi" 219 | VOLAPÜK = "vo" 220 | WALLOON = "wa" 221 | WOLOF = "wo" 222 | XHOSA = "xh" 223 | YIDDISH = "yi" 224 | YORUBA = "yo" 225 | ZHUANG = "za" 226 | CHINESE = "zh" 227 | ZULU = "zu" 228 | ``` 229 | 230 | ## API Endpoints 231 | `POST /translate`: 232 | 233 | Translates the provided text to the specified target language. The request body should be a JSON object with the following structure: 234 | ```json 235 | { 236 | "text": "Hello", 237 | "to": "id" 238 | } 239 | ``` 240 | The response will contain the translated text. 241 | 242 | ## Contributing 243 | Contributions to the Free Translate API are always welcome! If you find any issues or have suggestions for improvement, please feel free to open an issue or submit a pull request. 244 | 245 | ## License 246 | This project is licensed under the MIT License. 247 | --------------------------------------------------------------------------------