Use localizations and locale codes in Android SDK

To show each user a flow in a language they read, pass their languages to Adapty Android SDK 4.5 or later. Users see a flow in their most preferred language that the flow has. A flow with none of these languages appears in its default locale.

On SDK 4.0–4.4, your app passes one locale code to getFlowConfiguration, and the SDK matches it exactly. To update your code, read Migrate Adapty Android SDK to v4.5.

On SDK 4.5 or later, three cases follow other rules:

Pass the user’s languages to the SDK

AdaptyUI.getFlowConfiguration requires the preferredLocales parameter. Pass the user’s languages in it, most preferred first. Your app decides which languages to pass, such as the device languages or the language from a picker in your app.

val preferredLocales = LocaleListCompat.getAdjustedDefault().toLanguageTags().split(",")

AdaptyUI.getFlowConfiguration(flow, preferredLocales) { result ->
    // handle the result
}

Three rules decide which localization users see:

  • The SDK tries the user’s languages in order and matches the first one that the flow has. This is Android resource matching: the rules Android uses to select an app’s translations. The SDK matches close variants of a language: de-AT matches de, and zh-TW matches zh-hant. On Android 6 and earlier, the SDK matches the first localization of the flow in the user’s language and script, whatever its region. Paywalls from the old Paywall Builder only use the user’s language with the highest priority.
  • Case and separators don’t matter: pt_BR, pt-BR, and pt-br match the same locale. Pass the values as Android returns them.
  • With an empty list, or a list that matches none of the flow’s locales, the SDK uses the default locale.

Pass one of three lists. The list sets the order in which the SDK tries languages, but it can’t add a language that the flow lacks.

ValueThe flow appears inTrade-off
LocaleListCompat.getAdjustedDefault()The language that the user selected for your app, in the Android settings or in your app. Without one, the first device language that the flow hasBelow Android 13, a language that your app sets with AppCompatDelegate.setApplicationLocales only applies after the first AppCompatActivity is created. Read the list after that, not in Application.onCreate.
LocaleManagerCompat.getSystemLocales(context)The first device language that the flow has, even when the user selected another language for your appThe flow can appear in another language than your app’s UI
selectedLanguage, then LocaleListCompat.getAdjustedDefault()The language from the language picker in your app, then the device languages—

To pass a list, convert it to locale codes:

// The language selected for your app, then the device languages
val appLocales = LocaleListCompat.getAdjustedDefault().toLanguageTags().split(",")

// The device languages
val deviceLocales = LocaleManagerCompat.getSystemLocales(context).toLanguageTags().split(",")

// The language from the language picker in your app, then the other languages
val pickerLocales = listOf(selectedLanguage) + appLocales

The first two lists give different results when the user selects a language for your app. For example, on a device set to English, a user selects Spanish for your app. This user sees Spanish with getAdjustedDefault(), and English with getSystemLocales().

A language picker that saves the language with AppCompatDelegate.setApplicationLocales doesn’t need the third list. With such a picker, the first list starts with the selected language.

Tip

Don’t pass Locale.getDefault().toLanguageTag() on its own. It contains one language, so the SDK has no other language to try.

Most apps don’t pass localizationStrategy. Without it, the SDK uses AdaptyUiLocalizationStrategy.Auto, which fits flows from the Flow & Paywall Builder and paywalls from the old Paywall Builder.

Test which localization a user sees

When the flow has none of the user’s languages, the SDK uses the default locale without raising an error. To find out which localization a user sees, test the flow on a device:

  1. Set the log level to AdaptyLogLevel.VERBOSE.
  2. In the device settings, move the test language to the top of the language list.
  3. Open the flow in your app.
  4. In Logcat, find the line with getFlowConfiguration: preferredLocales. It ends with the code of the displayed localization, such as strategy = Auto -> pt-BR. The dashboard shows this code in lowercase: pt-br for pt-BR.

To test a language without a change to the device settings, pass a fixed preferredLocales list, such as listOf("pt-PT"), in a debug build.

When part of the flow still appears in the default locale, the displayed localization has no translation for that part. To fill the gaps, translate the missing rows in the Flow & Paywall Builder.

Select which locales to add to a flow

To cover the majority of users with the fewest locales, follow three rules when you add locales in the Select languages field:

  • Add a language without a region. For example, de covers German speakers in Germany, Austria, and Switzerland.
  • Only add a regional locale for a second translation of a language, such as fr-ca in addition to fr.
  • Add every language that your users read. The SDK doesn’t match one language to another, such as Catalan to Spanish. Catalan speakers see their next preferred language that the flow has, or the default locale.
Important

These rules need Android or iOS SDK 4.5 or later. Apps on any Adapty SDK from 4.0 to 4.4 pass one locale code. Their users can see a different localization of the same flow than users on SDK 4.5. Keep every code that those apps pass, such as an existing zh or pt-br.

Select codes for Chinese, Spanish, and Portuguese

The Select languages field has more than one code for Chinese, Spanish, and Portuguese. Add the codes from the table:

LanguageAddUsers see
Chinesezh-hans and zh-hant, not a new zh. Only add zh-hk for a separate Hong Kong translation.zh-hans in China and Singapore, and zh-hant in Taiwan, Hong Kong, and Macau.
Cantoneseyue, always with zh-hantyue on devices set to Cantonese, and zh-hant on devices set to Traditional Chinese. The SDK doesn’t show zh-hant to users on devices set to Cantonese, or yue to users on devices set to Traditional Chinese.
Spanishes and es-419, not a new es-mxes in Spain, and es-419 in Latin America and the United States.
Portuguesept-br for Brazilian Portuguese, and pt for European Portuguesept-br in Brazil, and pt in Portugal.

If your flow has several regional variants of one language, such as es-ar and es-co, add es-419 as well. Users in other Latin American countries, such as Mexico, then see es-419.

To plan translations in stages, start from one of three sets. Each set contains the previous one.

SetLocalesTotal
Minimalen, es, pt-br, fr, de, it, ru, ja, ko, zh-hans, zh-hant11
ExtendedThe minimal set and tr, pl, nl, sv, da, no, fi, uk, ar, he, hi, id, th, vi, es-419, pt27
BroadThe extended set and cs, sk, hu, ro, el, bg, hr, sl, lt, lv, et, ca, kk, ms, fil, fa, ur, bn, ta, te, mr, fr-ca49

In these sets, users see the following locales:

  • In the minimal set, es covers Spanish speakers in every country, and pt-br covers Portuguese speakers in Portugal too.
  • In the extended set, es-419 and pt are second translations. With them, es serves Spain, and pt serves Portugal. Translate pt into European Portuguese.
  • Catalan and Malay speakers only see their language in its own locale: ca or ms. The SDK doesn’t show them Spanish or Indonesian instead.

Select the default locale

Users see the default locale when the flow has none of their languages. A new flow starts with en as its only locale and its default. To change it, set another locale as default.

Follow three rules for the default locale:

  • For users without a matching locale, select the language that most of them understand. Usually, that’s English, not the language of your main market.
  • Translate the default locale fully. Users without a matching locale see the whole flow in the default locale. Users with a matching locale see the flow in their language. Any untranslated part appears in the default locale.
  • Don’t select a language with two scripts, such as Chinese. Users of the other script may not read the default locale.

Select a localization strategy

Most apps don’t pass localizationStrategy, so the SDK uses AdaptyUiLocalizationStrategy.Auto. This strategy applies Android resource matching to flows from the Flow & Paywall Builder, and the single-language rule to paywalls from the old Paywall Builder.

Under the single-language rule, the SDK only reads the language with the highest priority in preferredLocales. It tries the full code, then shorter codes (pt-br, then pt), and otherwise shows the default locale. A paywall from the old Paywall Builder keeps working under this rule, even after you republish it. To make the SDK try every language in preferredLocales, move the paywall to the Flow & Paywall Builder. Then serve the new flow from a flow placement.

To change how the SDK matches the user’s languages with the locales of the flow, pass one of the following values in the localizationStrategy parameter:

  • AdaptyUiLocalizationStrategy.System: every flow uses Android resource matching, paywalls from the old Paywall Builder included.
  • AdaptyUiLocalizationStrategy.Legacy: every flow uses the single-language rule, not the exact matching of SDK 4.4. Don’t pass Legacy for flows from the Flow & Paywall Builder: it misses their locales with a region or a script, such as pt-BR.
  • AdaptyUiLocalizationStrategy.Custom(resolver, fallbackResolver): your code picks the localization.

Pick a flow’s localization in your own code

Android resource matching doesn’t cover every script. For example, when a device lists Azerbaijani in Cyrillic script (az-Cyrl) but not in Latin, a flow with az appears in its default locale. To show az instead, or to pick the localization by other rules of your own, implement AdaptyUiCustomLocalizationResolver. Then pass it to AdaptyUiLocalizationStrategy.Custom in the localizationStrategy parameter.

Other Adapty SDKs match languages by other rules, so the same user can see different localizations of a flow on Android and iOS. To show the same localization on every platform, write a resolver that returns the same locale code as your app on the other platforms.

The following rules apply to the resolver:

  • The SDK doesn’t call the resolver for a flow with one localization. The flow appears in that localization.
  • The SDK calls resolve with the list that your app passed, an AdaptyUiLocalizationSource with the flow’s locales and defaultLocale, and a callback. The locales come in the order of the flow’s localizations.
  • To show a localization, pass a code from locales or defaultLocale to callback, spelled exactly the same, case included: ar, not AR.
  • If callback receives any other code or null, the SDK uses the resolver in fallbackResolver. Without fallbackResolver, the SDK uses AdaptyUiLocalizationResolver.Auto.
  • The SDK calls resolve on the main thread. Call callback once, from any thread.
  • Call callback right away, without network requests or other slow work. The SDK waits for callback without a time limit, and the flow only appears after your code calls callback.

Locale code standard at Adapty

Adapty locale codes follow the BCP 47 standard: a language subtag, then an optional script or region subtag, separated by hyphens. The Select languages field shows the codes in lowercase: en (English), pt-br (Portuguese, Brazil), zh-hans (Simplified Chinese), and zh-hant (Traditional Chinese).

Localize custom paywalls and onboardings

SDK 4.5 only changes the localization rules for flows. Custom paywalls and onboardings work as in SDK 4.4.

Pick a custom paywall’s remote config by language

getFlow has no locale parameter. For a custom paywall, it returns every configured localization in flow.remoteConfigs. Each entry has a locale code and the config content (jsonString, or the parsed dataMap). Pick the entry that matches the user, with your own fallback:

Adapty.getFlow("YOUR_PLACEMENT_ID") { result ->
    when (result) {
        is AdaptyResult.Success -> {
            val flow = result.value
            val config = flow.remoteConfigs.firstOrNull { it.locale == "en" }
                ?: flow.remoteConfigs.firstOrNull()
            // read your values from config?.dataMap
        }
        is AdaptyResult.Error -> {
            // handle the error
        }
    }
}

Adapty stores those locale codes in the format described in Locale code standard at Adapty. The SDK doesn’t match remote configs against a locale, so which entry to apply is up to your app.

Pass a locale to an onboarding

Warning

Onboardings are deprecated starting with Adapty SDK v4. Build flows instead: unlike onboardings, which run inside a WebView, flows render natively on the device — smoother animations, a consistent native look and feel, faster load times, and no WebView runtime dependency.

getOnboarding keeps its locale parameter in SDK 4.5. When you pass a locale to getOnboarding, the server picks the localization:

  1. The locale string is converted to lowercase and all the underscores (_) are replaced with hyphens (-)
  2. Adapty looks for the localization with the fully matching locale code
  3. If no match is found, Adapty takes the substring before the first hyphen (pt for pt-br) and looks for the matching localization
  4. If no match is found again, Adapty returns the content for the onboarding’s default locale

This way pt_BR, pt-BR, and pt-br all resolve to the same onboarding localization.

Why this is important

There are a few scenarios when locale codes come into play — for example, when you’re trying to fetch the correct paywall for the current localization of your app.

As locale codes are complicated and can vary from platform to platform, we rely on an internal standard for all the platforms we support. However, because these codes are complicated, it is really important for you to understand what exactly are you sending to our server to get the correct localization, and what happens next — so you will always receive what you expect.

Locale code standard at Adapty

For locale codes, Adapty uses a slightly modified BCP 47 standard: every code consists of lowercase subtags, separated by hyphens. Some examples: en (English), pt-br (Portuguese (Brazil)), zh (Simplified Chinese), zh-hant (Traditional Chinese).

Locale code matching

When Adapty receives a call from the client-side SDK with the locale code and starts looking for a corresponding localization of a paywall, the following happens:

  1. The incoming locale string is converted to lowercase and all the underscores (_) are replaced with hyphens (-)
  2. We then look for the localization with the fully matching locale code
  3. If no match was found, we take the substring before the first hyphen (pt for pt-br) and look for the matching localization
  4. If no match was found again, we return the content for the paywall’s default locale

This way an iOS device that sent 'pt_BR', an Android device that sent pt-BR, and another device that sent pt-br will get the same result.

If you’re wondering about localizations, chances are you’re already dealing with the localized string files in your project. If that’s the case, we recommend placing some key-value with the intended Adapty locale code in each of your files for the corresponding localizations. And then extract the value for this key when calling our SDK, like so:

// 1. Modify your strings.xml files

/*
strings.xml - Spanish
*/
<string name="adapty_paywalls_locale">es</string>

/*
strings.xml - Portuguese (Brazil)
*/
<string name="adapty_paywalls_locale">pt-br</string>

// 2. Extract and use the locale code

val localeCode = context.getString(R.string.adapty_paywalls_locale)
// pass locale code to AdaptyUI.getViewConfiguration or Adapty.getPaywall method

That way you can ensure you’re in full control of what localization will be retrieved for every user of your app.

Implementing localizations: the other way

You can get similar (but not identical) results without explicitly defining locale codes for every localization. That would mean extracting a locale code from some other objects that your platform provides, like this:

val locale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
    context.resources.configuration.locales[0]
else
    context.resources.configuration.locale

val localeCode = locale.toLanguageTag()
// pass locale code to AdaptyUI.getViewConfiguration or Adapty.getPaywall method

Note that we don’t recommend this approach because it’s hard to predict what exactly will Adapty’s server get.

Should you decide to use this approach anyway — make sure you’ve covered all the relevant use cases.