DocsIntegrations (experimental)

Google Drive (experimental)

@generic-translation/google-drive - translate Google Docs, Sheets, Slides and text files in Drive into copies. Experimental and unverified against any real Google service.

On this page

Experimental. Unverified against any real Google service. There was no Google Cloud project, OAuth client or service-account key available when @generic-translation/google-drive was written. Every Google call was built from the public REST references (Drive v3, Docs v1, Sheets v4, Slides v1, the service-account OAuth flow) and tested only against an in-memory fake that encodes our reading of them. Only the Generic Translation half was exercised live. Details are in Status.

Source: generic-translation-integrations/google-drive. Not on npm yet; link it from a checkout. It uses fetch and WebCrypto only (no googleapis), so it runs on Node 20+ and edge runtimes.

Quickstart

  1. In a Google Cloud project, enable the Drive, Docs, Sheets and Slides APIs.
  2. Get credentials, either an OAuth access token for a user (scope https://www.googleapis.com/auth/drive; the narrower drive.file scope cannot reach arbitrary existing files), or a service account key. A service account is its own identity: share the source file or folder with its client_email (Viewer reads, Editor writes copies beside the source) and pass a targetFolderId it can write to, or the copies land in its own invisible Drive. With Workspace domain-wide delegation you can pass subject to act as a user.
  3. A Generic Translation project and API key with translations:generate, files:write and files:read.
export GENERIC_TRANSLATION_API_KEY=gt-api-...
export GOOGLE_ACCESS_TOKEN=ya29...        # or --service-account key.json
npx generic-translation-drive translate <fileOrFolderId> --locales es,fr --dry-run
npx generic-translation-drive translate <folderId> --locales de --include "*.md" --target-folder <id>

Run against a copy of a folder first and check the output. The exit code is 1 if any file errored.

From code:

import { GenericTranslation } from "@generic-translation/core";
import { translateDriveFile, translateDriveFolder } from "@generic-translation/google-drive";

const genericTranslation = new GenericTranslation({ apiKey: process.env.GENERIC_TRANSLATION_API_KEY });
const auth = { accessToken: process.env.GOOGLE_ACCESS_TOKEN! };
// or: { serviceAccount: JSON.parse(keyJson), subject: "optional-user@example.com" }

const results = await translateDriveFile({ auth, genericTranslation, fileId: "1AbC...", locales: ["es", "ja"], targetFolderId: "1XyZ..." });
await translateDriveFolder({ auth, genericTranslation, folderId: "1Fold...", locales: ["fr"], include: ["*.md"], dryRun: true });

auth.accessToken may be a function called before each request, for your own refresh logic. Per-file errors do not throw; each result has action: "error" and an error string.

Reference

Per file type

Type How
Google Doc Reads the structural JSON (all tabs, body, headers, footers, footnotes, table cells, list items). Each paragraph becomes one message per stretch of text runs; runs styled differently from the paragraph become numbered tags (Click <0>here</0> and <1>bold</1>), so bold, italic, links and colors survive and can be reordered. The doc is copied, then documents.batchUpdate rewrites the copy back-to-front using UTF-16 indices. Inline elements (footnote references, images, people chips) stay in place and split a paragraph into separate messages.
Google Sheet Reads cells as formulas; translates string cells only (formulas, numbers, booleans, dates, bare URLs and strings without letters are skipped); copies the spreadsheet and writes cells as RAW. Tab names and charts are not translated.
Google Slides Text in shapes, table cells and grouped shapes, on a copy. Auto text (slide numbers, dates) stays. Not translated: speaker notes, layouts and masters, alt text, text inside images.
Plain files (txt, md, mdx, html, json, yaml, srt, po, strings, resx, xliff, csv) Downloaded, sent to Generic Translation’s files route, and written to <name> (es).<ext> next to the source; an existing translation is updated in place. .xml is ambiguous, so pass fileFormat: "androidxml". Files over 5 MB or containing NUL bytes are refused.

Forms, drawings, shortcuts, PDFs, images and other binaries are reported as unsupported (or skipped, for shortcuts).

Naming, idempotency and re-runs

Copies are named <name> (<locale>) (nameTemplate overrides). Each carries genericTranslationSourceId, genericTranslationLocale and genericTranslationSourceRev in Drive appProperties (or properties with propertyStore: "properties"). A re-run finds the copy by source id and locale:

  • source unchanged: skipped;
  • source changed (or force: true): plain files are updated in place; for Docs, Sheets and Slides a new copy is made and the stale one is moved to the bin (staleCopies: "keep" to keep it), so the file id and sharing of a native copy change on update;
  • translateDriveFolder ignores files carrying those markers, so outputs are never translated again.

appProperties are private to the OAuth client or service account that wrote them, so another client will not see them and will make a second copy. Use propertyStore: "properties" if several clients share a folder.

Caveats

  • Documents with unresolved suggested edits: indices follow documents.get’s default view, so accept or reject suggestions first.
  • A translation that loses or invents formatting tags is rejected for that paragraph (original kept, warning returned). Newlines in a translation become soft line breaks.
  • Translations are cached per run, so identical strings are sent once.
  • Generic Translation usage counts as usual; use dryRun to count units first.

Status

Exercised for real The Generic Translation side. An opt-in live test signs up a throwaway @example.com user, creates a project and key, sends text extracted from fixture Doc, Sheet, Slides and Markdown data through the real translate and files routes, checks that bold and link runs came back intact, and deletes the project. It passed.
Tested against a fake only All Google calls. The fake enforces rules we know of (UTF-16 indices, no split surrogate pairs, the final newline cannot be deleted, valid field masks), but it is our reading of the docs.
Never confirmed against real Google The exact JSON from documents.get, presentations.get, values:batchGet and files.list; batchUpdate index behavior in real documents (lists, tables, footnotes, headers, named ranges, suggestions, tabs) and how inserted text inherits style; files.copy preserving structure; appProperties queries, revision semantics, shared drives and quotas; the service-account JWT exchange and domain-wide delegation (the JWT is checked with WebCrypto in tests but no token has been minted by Google); the CLI with real credentials.

Something wrong or unclear on this page? Open an issue.

Type to search the documentation.