← Back to Home

How to Translate Text with Gemini AI

Updated January 14, 2026
geminitranslationtext generationbatch

Translating Text with Gemini AI

Gemini AI provides translation capabilities using the text generation models.

Model Selection

Choose between cost and quality:

Single Translation

import { GoogleGenAI } from '@google/genai';

const genAI = new GoogleGenAI(process.env.GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: 'gemini-3-flash-preview' });

const prompt = `Translate the following Japanese text to English:\n\n${text}`;

const response = await model.generateContent(prompt);
const translatedText = response.response.text();

Batch Translation

More efficient for multiple items:

const prompt = `Translate the following Japanese titles to English.
Return JSON format: [{"original": "...", "translated": "..."}]

Titles:
${titles.map((t, i) => `${i + 1}. ${t}`).join('\n')}
`;

const response = await model.generateContent(prompt);
const result = JSON.parse(response.response.text());

Response Parsing

Gemini wraps JSON in markdown code blocks:

function parseGeminiJSON(text: string): any {
  // Remove markdown code blocks
  const cleaned = text.replace(/```(?:json)?\s*([\s\S]*?)\s*```/, '$1');
  return JSON.parse(cleaned);
}

Configuration

const response = await genAI.models.generateContent({
  model: 'gemini-3-flash-preview',
  contents: prompt,
  config: {
    generationConfig: {
      temperature: 0.7,
      maxOutputTokens: 1000,
    },
  },
});