Nov 14, 9:43pm

It depends on whether the translation already exists in the app's resources or not.

1. The translation already exists in the app (e.g. values-fr/strings.xml)

Wrap the context in a configuration context with the target locale, then resolve the string:

kotlin
fun getStringForLocale(context: Context, locale: Locale, @StringRes resId: Int): String {
    val config = Configuration(context.resources.configuration)
    config.setLocale(locale)
    return context.createConfigurationContext(config).getString(resId)
}

// Example: get the French version of a string
val french = getStringForLocale(context, Locale.FRENCH, R.string.hello)

This does not change the app's or device's locale; it only resolves the resource for that locale. Notes:

  • The string must actually be present in values-<lang>/strings.xml, otherwise Android falls back to the default values/strings.xml.
  • For another app's resources, use packageManager.getResourcesForApplication(packageName) combined with the same configuration trick.
  • To change the whole app's language (rather than fetch one string), use AppCompatDelegate.setApplicationLocales() (AndroidX AppCompat) or the per-app language preferences on Android 13+.

2. No translation exists yet, and you want to generate it

You need a translation service, since Android itself doesn't translate anything:

  • On-device, offline: Google's ML Kit on-device translation, which supports ~59 languages and works offline after downloading the model.
  • Cloud: Google Cloud Translation API, DeepL API, Azure Translator, etc., called from your backend or app.
  • At build time: tools or services that translate your strings.xml files (e.g. Lokalise, Phrase, Crowdin) so translators or MT produce the values-<lang> files before release.

So in short: existing localized resource → createConfigurationContext + getString; missing translation → a translation API/SDK.

Nov 14, 9:43pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

Vibe can make mistakes. Check answers. Learn more