Sample code for 30+ languages & platforms
Zig

Google Translate Text

See more Google Translate Examples

Demonstrates how to use the Cloud Translation API to translate text from one spoken language to another. The example translates from English to Spanish.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // --------------------------------------------------------------------------------
    // IMPORTANT:
    // Don't forget you need to 1st enable the Cloud Translation API in your Google Developers Console at https://console.cloud.google.com
    // --------------------------------------------------------------------------------

    // It is assumed we previously obtained an OAuth2 access token.
    // This example loads the JSON access token file
    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/_googleTranslate.json") catch {
        std.debug.print("Failed to load _googleTranslate.json\n", .{});
        return;
    };

    const http = try chilkat.Http.init();
    defer http.deinit();

    http.setAuthToken(try json_token.stringOf(alloc, "access_token"));

    // The following JSON is sent in the request body.

    // {
    //     "q": "The quick brown fox jumped over the lazy dog.",
    //     "source": "en",
    //     "target": "es",
    //     "format": "text"
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    // The following code creates the JSON request body.
    json.updateString("q", "The quick brown fox jumped over the lazy dog.") catch {};
    json.updateString("source", "en") catch {};
    json.updateString("target", "es") catch {};
    json.updateString("format", "text") catch {};

    json.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json.emit(alloc)});

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://translation.googleapis.com/language/translate/v2", json, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    resp.getBodyJson(j_resp) catch {};
    j_resp.setEmitCompact(false);

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try j_resp.emit(alloc)});

    const resp_status_code = resp.getStatusCode();
    std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
    if (resp_status_code >= 400) {
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "data": {
    //     "translations": [
    //       {
    //         "translatedText": "El zorro r�pida salt� sobre el perro perezoso."
    //       }
    //     ]
    //   }
    // }

    var translated_text: [:0]const u8 = "";

    var i: i32 = 0;
    const n = j_resp.sizeOfArray("data.translations");
    while (i < n) {
        j_resp.setI(i);
        translated_text = try j_resp.stringOf(alloc, "data.translations[i].translatedText");
        std.debug.print("{s}\n", .{translated_text});
        i = i + 1;
    }
}