@molecule/api-ai-translation-google

Provider bond · ai-translation · API (Node) · v1.0.1 · Apache-2.0

Google Cloud Translation provider for molecule.dev — text translation via the Google Translate API

npm install @molecule/api-ai-translation-google

npm · Source on GitHub · Implements @molecule/api-ai-translation

How it works

@molecule/api-ai-translation-google is a provider bond on the API (Node) side: it implements the ai-translation core interface (@molecule/api-ai-translation) with a concrete vendor or library behind it.

Your code calls the core; you wire this provider once at startup. Swapping vendors later is one line in that wiring, not a rewrite.

import { setProvider, requireProvider } from '@molecule/api-ai-translation'
import { provider } from '@molecule/api-ai-translation-google'

setProvider(provider) // at startup — lazy; reads GOOGLE_TRANSLATE_API_KEY on first use

const { translations } = await requireProvider().translate({
  text: ['{{count}} items in your cart'],
  targetLang: 'de',
  protect: ['{{count}}'],
})
// translations[0].text → '{{count}} Artikel in Ihrem Warenkorb'

Works with: @molecule/api-ai-translation, @molecule/api-secrets

Secrets: GOOGLE_TRANSLATE_API_KEY

Reference

Google Cloud Translation provider for molecule.dev.

Implements the @molecule/api-ai-translation contract (translate, getSupportedLanguages, getUsage) over the Cloud Translation API (Basic, v2), batching large inputs (128 texts or ~25k characters per request).

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

import { setProvider, requireProvider } from '@molecule/api-ai-translation'
import { provider } from '@molecule/api-ai-translation-google'

setProvider(provider) // at startup — lazy; reads GOOGLE_TRANSLATE_API_KEY on first use

const { translations } = await requireProvider().translate({
  text: ['{{count}} items in your cart'],
  targetLang: 'de',
  protect: ['{{count}}'],
})
// translations[0].text → '{{count}} Artikel in Ihrem Warenkorb'

Type

provider

Installation

npm install @molecule/api-ai-translation-google @molecule/api-ai-translation @molecule/api-secrets

API

Interfaces

GoogleTranslateConfig

Configuration for the Google Cloud Translation provider.

interface GoogleTranslateConfig {
  /** Google Cloud API key with the Cloud Translation API enabled. Defaults to GOOGLE_TRANSLATE_API_KEY. */
  apiKey?: string
  /** Base URL of the Translation API. Defaults to GOOGLE_TRANSLATE_BASE_URL, then 'https://translation.googleapis.com'. */
  baseUrl?: string
}

Functions

createProvider(config)

Creates a Google Cloud Translation provider instance.

function createProvider(config?: GoogleTranslateConfig): AITranslationProvider
  • config — Google-specific configuration (API key, base URL).

Returns: An AITranslationProvider backed by the Cloud Translation API.

Constants

provider

The provider implementation.

const provider: AITranslationProvider

aiTranslationGoogleSecretDefinitions

Secret definitions required by the Google Cloud Translation bond.

const aiTranslationGoogleSecretDefinitions: SecretDefinition[]

Core Interface

Implements @molecule/api-ai-translation interface.

Bond Wiring

Setup function to register this provider with the core interface:

import { setProvider } from '@molecule/api-ai-translation'
import { provider } from '@molecule/api-ai-translation-google'

export function setupAiTranslationGoogle(): void {
  setProvider(provider)
}

Injection Notes

Requirements

Peer dependencies:

  • @molecule/api-ai-translation >=1.0.1
  • @molecule/api-secrets ^1.0.1

Environment Variables

  • GOOGLE_TRANSLATE_API_KEY (required) — Google Cloud Translation API key

Runtime Dependencies

  • @molecule/api-ai-translation

  • @molecule/api-secrets

  • Wiring: bond the lazy provider export once — setProvider(provider) — or setProvider(createProvider(config?)) to pass explicit config.

  • Config: GOOGLE_TRANSLATE_API_KEY (required — a Google Cloud API key with the Cloud Translation API enabled); GOOGLE_TRANSLATE_BASE_URL (optional endpoint override).

  • Language codes are Google's, lowercase BCP-47 style (de, pt, zh-TW) — not DeepL's (DE, PT-BR, ZH-HANT). Take them from getSupportedLanguages().

  • protect is the only safe way to keep placeholders. Google ignores custom tags and splits braces even inside translate="no", so this bond sends each protected substring as an index and restores the original afterwards.

  • formality, glossaryId, modelType, context and preserveFormatting have no v2 equivalent and are ignored.

  • getUsage() counts this process only (characterLimit is Infinity): Google has no usage endpoint — spend lives in the Cloud console, billed per character.

  • Rate limits (429, and 403 "User Rate Limit Exceeded") and 5xx are retried with backoff; other failures throw an Error whose status is the HTTP status.

E2E Tests

Integration checklist — drive the real UI (live preview, no mocks), adapt each item to this app's actual screens/flows, and check every box off one by one. A box you can't check is an integration bug to fix — not a skip. The sandbox has a live AI provider, so translations run for real; output is non-deterministic, so assert on the resulting LANGUAGE/meaning, never an exact string:

  • Translating real text to a target language through the UI returns text ACTUALLY in that language — English→Spanish produces recognizably Spanish, not the original echoed back or left in English.
  • Switching the target language (from the picker populated by getSupportedLanguages('target')) changes the output language for the same input — the same source re-translates into the newly chosen language.
  • With sourceLang omitted the provider auto-detects: a known-language input comes back with the correct detectedSourceLang, and if the UI shows a detected-language label it names the right one.
  • Text already in the target language is left sensible — unchanged or a valid paraphrase, never mangled, doubled, or emptied.
  • Empty or untranslatable input (whitespace, emoji, a bare code snippet) is handled gracefully — a clear UI state, nothing crashes.
  • A provider failure (bad key, quota exhausted, network drop) surfaces a visible error in the UI, not an unhandled 500 or a silently blank result.
  • The translate call runs server-side only — the provider key never reaches the browser (check the network panel: no key in any request).