DEV Community

Cover image for Localizing Gemini Prompts — Getting AI Responses in the User's Language
hiyoyo
hiyoyo

Posted on

Localizing Gemini Prompts — Getting AI Responses in the User's Language

If this is useful, a ❤️ helps others find it.

All tests run on an 8-year-old MacBook Air.

HiyokoLogcat supports Japanese and English. The AI diagnosis needed to respond in whichever language the user chose.

The simplest solution: write the system prompt in the target language. Gemini follows it reliably.


The naive approach (wrong)

// Don't do this
let prompt = format!("Analyze this log: {}\nRespond in Japanese.", context);
Enter fullscreen mode Exit fullscreen mode

Tacking "respond in Japanese" onto an English prompt produces inconsistent results. Sometimes Gemini complies, sometimes it doesn't.


The right approach: prompt in the target language

pub enum Lang {
    Japanese,
    English,
}

pub fn build_system_prompt(lang: &Lang) -> &'static str {
    match lang {
        Lang::Japanese =>
            "あなたはAndroid開発のスペシャリストです。\
             以下のlogcatからエラーの根本原因と解決策を、\
             日本語で簡潔に3〜5文で答えてください。\
             KEY ERROR LINEが対象のエラーです。",

        Lang::English =>
            "You are an Android development specialist. \
             Identify the root cause of the KEY ERROR LINE \
             and suggest a fix. Be concise — 3 to 5 sentences.",
    }
}

pub async fn diagnose(
    context: &str,
    api_key: &str,
    lang: &Lang,
) -> Result {
    let system = build_system_prompt(lang);
    call_gemini_with_system(system, context, api_key).await
}
Enter fullscreen mode Exit fullscreen mode

Japanese system prompt → Japanese response. Every time. No post-processing.


Reading the language from app state

#[tauri::command]
pub async fn run_diagnosis(
    context: String,
    api_key: String,
    language: String,  // "ja" or "en" from frontend i18n state
) -> Result {
    let lang = match language.as_str() {
        "ja" => Lang::Japanese,
        _ => Lang::English,
    };

    diagnose(&context, &api_key, &lang)
        .await
        .map_err(|e| format!("{:?}", e))
}
Enter fullscreen mode Exit fullscreen mode

The frontend passes its current locale string. Rust maps it to the enum. The prompt is built from there.


Adding a new language

pub enum Lang {
    Japanese,
    English,
    Korean,  // add new variant
}

pub fn build_system_prompt(lang: &Lang) -> &'static str {
    match lang {
        Lang::Korean =>
            "당신은 Android 개발 전문가입니다. \
             KEY ERROR LINE의 근본 원인과 해결책을 \
             한국어로 간결하게 3~5문장으로 답해주세요.",
        // ...
    }
}
Enter fullscreen mode Exit fullscreen mode

One new match arm. That's the whole change.


What about translation APIs?

Unnecessary complexity. Writing the prompt in the target language is simpler, cheaper (no extra API call), and more reliable than translating the response after the fact.


Hiyoko PDF Vault → https://hiyokoko.gumroad.com/l/HiyokoPDFVault
X → @hiyoyok

Top comments (0)