Google AI

AI translation with Google Gemini and Lingo.dev Compiler

What is Google AI?

Google AI Studio is a web-based development environment for building, testing, and deploying applications powered by Google’s generative AI models, such as Gemini. It allows developers to experiment with prompts, fine-tune responses, and integrate models into apps via APIs, all within an easy-to-use interface connected to Google Cloud.

Setting the API Key

Google AI requires an API key to authenticate requests from Lingo.dev Compiler. Choose the setup method that best fits your workflow:

Option 1: Environment Variable (Terminal)

Best for quick experiments and testing without modifying any files.

Set the key directly in your terminal session:

export GOOGLE_API_KEY="your-api-key-here"

This key will only be available until you close your terminal window.

Option 2: Project Configuration (.env)

Best for project-specific configuration and team environments where each project may use different credentials.

Create a .env file in your project root:

touch .env

Add the following line to the file:

GOOGLE_API_KEY="your-api-key-here"

Lingo.dev Compiler checks for environment files in this priority order:

  1. .env.development (highest priority)
  2. .env.local
  3. .env (lowest priority)

Values in higher-priority files override those in lower-priority files.

Option 3: Global Configuration (User Settings)

Best for individual developers who want to use the same API key across all their projects.

Create a configuration file in your home directory:

touch ~/.lingodotdevrc

Add the following content to the file:

[llm]
googleApiKey="your-api-key-here"

This configuration persists across all terminal sessions and projects on your machine.

Configuration Priority

When multiple configuration methods are used, Lingo.dev Compiler checks for API keys in this order:

  1. Environment variables (highest priority)
  2. Project .env files (in their own priority order)
  3. User configuration file ~/.lingodotdevrc (lowest priority)

The first valid API key that's found is used.

Using Google AI

To enable Google AI, set the models property in the compiler options:

import react from "@vitejs/plugin-react";
import lingoCompiler from "lingo.dev/compiler";
import { type UserConfig } from "vite";

// https://vite.dev/config/
const viteConfig: UserConfig = {
  plugins: [react()],
};

const withLingo = lingoCompiler.vite({
  sourceRoot: "src",
  lingoDir: "lingo",
  sourceLocale: "en",
  targetLocales: ["es", "fr", "de", "ja"],
  rsc: false,
  useDirective: false,
  debug: false,
  models: {
    "*:*": "google:gemini-1.5-flash",
  },
});

export default withLingo(viteConfig);

The property accepts an object where:

  • the keys are pairs of source and target locales, with * representing any locale
  • the values are model identifiers (e.g., google:gemini-1.5-flash)

You can use Google AI to translate:

  • between all locales
  • from a specific source locale
  • to a specific target locale
  • between a specific source and target locale

Translating all locales

Use the wildcard pattern *:* to apply the same Google model for all translation pairs:

const withLingo = lingoCompiler.vite({
  sourceRoot: "src",
  lingoDir: "lingo",
  sourceLocale: "en",
  targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
  models: {
    // Use Gemini 1.5 Flash for all translation pairs
    "*:*": "google:gemini-1.5-flash",
  },
});

Translating from a specific source locale

Use a specific source locale with wildcard target to apply a model for all translations from that source:

const withLingo = lingoCompiler.vite({
  sourceRoot: "src",
  lingoDir: "lingo",
  sourceLocale: "en",
  targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
  models: {
    // Use Gemini 1.5 Pro for all translations from English
    "en:*": "google:gemini-1.5-pro",
    // Use Gemini 1.5 Flash for translations from Spanish to any language
    "es:*": "google:gemini-1.5-flash",
    // Fallback for other source languages
    "*:*": "google:gemini-1.5-flash-latest",
  },
});

Translating to a specific target locale

Use a wildcard source with specific target locale to apply a model for all translations to that target:

const withLingo = lingoCompiler.vite({
  sourceRoot: "src",
  lingoDir: "lingo",
  sourceLocale: "en",
  targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
  models: {
    // Use specialized model for translations to Japanese
    "*:ja": "google:gemini-1.5-pro",
    // Use Gemini 1.5 Flash for translations to Chinese
    "*:zh": "google:gemini-1.5-flash",
    // Use Gemini 1.5 Pro for translations to German
    "*:de": "google:gemini-1.5-pro-latest",
    // Default for other target languages
    "*:*": "google:gemini-1.5-flash",
  },
});

Translating between specific locales

Define exact source-target pairs for fine-grained control over which model handles specific language combinations:

const withLingo = lingoCompiler.vite({
  sourceRoot: "src",
  lingoDir: "lingo",
  sourceLocale: "en",
  targetLocales: ["es", "fr", "de", "ja", "pt", "zh"],
  models: {
    // Specific pairs with optimal models
    "en:es": "google:gemini-1.5-flash", // English to Spanish
    "en:ja": "google:gemini-1.5-pro", // English to Japanese
    "en:zh": "google:gemini-1.5-pro", // English to Chinese
    "es:en": "google:gemini-1.5-flash", // Spanish to English
    "fr:en": "google:gemini-1.5-flash-latest", // French to English
    "de:en": "google:gemini-1.0-pro", // German to English

    // Fallback for unspecified pairs
    "*:*": "google:gemini-1.5-flash",
  },
});