Skip to content

Localization on Android

Customizing Localization

If any language is required by your application but is not supported by Document Reader SDK, or you want to change our localized strings, you can add localized strings to strings.xml file and allow the Document Reader SDK to be presented to the users in their desired system locale. To do it, you need to know the identifiers of the strings that are available in the values.xml file provided with the latest release version. You can redefine every string in Document Reader SDK with the appropriate translated values. This way, when the user has their device's System Language set to a language supported by your application, the SDK will utilize the appropriate string resources.

The values.xml file with all available strings can be found in the project: External Libraries -> Gradle: com.regula.documentreader:api:X.X.X@aar -> values -> values.xml

Changing the app's language programmatically

If you wish to allow users to change the app's language right in the app, you can apply the following code snippet to your code (changing to Spanish is demonstrated there):

class ContextUtils(base: Context?) : ContextWrapper(base) {
    companion object {
        fun updateLocale(context: Context, localeToSwitchTo: Locale?): ContextWrapper {
            var context: Context = context
            val resources: Resources = context.getResources()
            val configuration: Configuration = resources.getConfiguration()
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                val localeList = LocaleList(localeToSwitchTo)
                LocaleList.setDefault(localeList)
                configuration.setLocales(localeList)
            } else {
                configuration.locale = localeToSwitchTo
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
                context = context.createConfigurationContext(configuration)
            } else {
                resources.updateConfiguration(configuration, resources.getDisplayMetrics())
            }
            return ContextUtils(context)
        }
    }
}
public class ContextUtils extends ContextWrapper {
    public ContextUtils(Context base) {
        super(base);
    }

    public static ContextWrapper updateLocale(Context context, Locale localeToSwitchTo) {
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            LocaleList localeList = new LocaleList(localeToSwitchTo);
            LocaleList.setDefault(localeList);
            configuration.setLocales(localeList);
        } else {
            configuration.locale = localeToSwitchTo;
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            context = context.createConfigurationContext(configuration);
        } else {
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        }

        return new ContextUtils(context);
    }
}

In your application class, you should modify the attachBaseContext method:

class MainApplication : Application() {
    override fun attachBaseContext(newBase: Context?) {
        val localeToSwitchTo = Locale("es")
        val localeUpdatedContext = ContextUtils.updateLocale(newBase, localeToSwitchTo)
        super.attachBaseContext(localeUpdatedContext)
    }
}
public class MainApplication extends Application {        
    @Override
    protected void attachBaseContext(Context newBase) {
        Locale localeToSwitchTo = new Locale("es");
        ContextWrapper localeUpdatedContext = ContextUtils.updateLocale(newBase, localeToSwitchTo);

        super.attachBaseContext(localeUpdatedContext);
    }
}

In your activity class, you should also modify the attachBaseContext method:

class MainActivity : AppCompatActivity() {
    override fun attachBaseContext(newBase: Context?) {
        val localeToSwitchTo = Locale("es")
        val localeUpdatedContext = ContextUtils.updateLocale(newBase, localeToSwitchTo)
        super.attachBaseContext(localeUpdatedContext)
    }
}
public class MainActivity extends AppCompatActivity {        
    @Override
    protected void attachBaseContext(Context newBase) {
        Locale localeToSwitchTo = new Locale("es");
        ContextWrapper localeUpdatedContext = ContextUtils.updateLocale(newBase, localeToSwitchTo);

        super.attachBaseContext(localeUpdatedContext);
    }
}

Here is the alternative solution with deprecated API:

val configuration: Configuration = resources.configuration
configuration.setLocale(Locale("es"))
resources.updateConfiguration(configuration, resources.displayMetrics)
applicationContext.resources.updateConfiguration(configuration, resources.displayMetrics)
Configuration configuration = getResources().getConfiguration();
configuration.setLocale(new Locale("es"));
getResources().updateConfiguration(configuration,  getResources().getDisplayMetrics());
getApplicationContext().getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());

Warning

Make sure that you have updated configuration for resources also for allicationContext.

Additional information

For more information on localizing your application and best practices, please see the official Android documentation.

Next Steps