Localization: Change Languages in Our Android PDF Viewer

PSPDFKit for Android comes with many built-in languages:

  • Arabic (ar)

  • Chinese Simplified / Chinese Traditional (zh-Hans/zh-Hant)

  • Croatian (hr)

  • Czech (cs)

  • Danish (da)

  • Dutch (nl)

  • English (en)

  • English United Kingdom (en-GB)

  • Finnish (fi)

  • French (fr)

  • French Canada (fr-CA)

  • German (de)

  • Greek (el)

  • Hebrew (he)

  • Indonesian (id)

  • Italian (it)

  • Japanese (ja)

  • Korean (ko)

  • Malay (ms)

  • Norwegian Bokmål (nb-NO)

  • Polish (pl)

  • Portuguese Brazil / Portugal (pt-BR/pt-PT)

  • Russian (ru)

  • Slovak (sk)

  • Slovenian (sl)

  • Spanish (es)

  • Swedish (sv)

  • Thai (th)

  • Turkish (tr)

  • Ukrainian (uk)

  • Welsh (cy)

Adding Additional Localization to PSPDFKit

You can add additional translations by putting them into the res/strings-XX directory of your app. Android will automatically merge all string resources at build time. You can also override PSPDFKit strings of existing languages by putting them into the respective string folders.

💡 Tip: To see a list of all available PSPDFKit string resources, take a look at the extracted library AAR file in your project’s build folder at app/build/intermediates/incremental/mergeDebugResources/merged.dir/values.

Forcing a Specific Language

PSPDFKit uses localized string resources so that Android can automatically select the correct Locale based on the device’s system language and region. To force an app-wide locale different from the system’s locale, your application needs to modify Android’s resource Configuration using a custom Application class:

/**
 * Custom application using a fixed `{@link Locale}`.
 */
class MyApplication : Application() {

    /**
     * This example uses a hardcoded, fixed locale. Your app could also implement
     * a language switcher instead.
     */
    val USED_APP_LOCALE = Locale.GERMAN

    override fun onCreate() {
        super.onCreate()

        // Set the locale initially when the app is launched.
        setAppLocale(USED_APP_LOCALE)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)

        // Reapply the locale every time the app’s configuration changes
        // (e.g. due to the user changing the device’s locale).
        setAppLocale(USED_APP_LOCALE)
    }

    /**
     * Sets the app-wide `{@link Locale}`.
     */
    private fun setAppLocale(locale: Locale) {
        val configuration = Configuration()

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale)
        } else {
            configuration.locale = locale
        }

        // This will not replace the app configuration, but will merely
        // merge the chosen locale into the existing configuration.
        resources.updateConfiguration(configuration, null)
    }
}
/**
 * Custom application using a fixed `{@link Locale}`.
 */
public class MyApplication extends Application {

    /**
     * This example uses a hardcoded, fixed locale. Your app could also implement
     * a language switcher instead.
     */
    private static final Locale USED_APP_LOCALE = Locale.GERMAN;

    @Override public void onCreate() {
        super.onCreate();

        // Set the locale initially when the app is launched.
        setAppLocale(USED_APP_LOCALE);
    }

    @Override public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Reapply the locale every time the app’s configuration changes
        // (e.g. due to the user changing the device’s locale).
        setAppLocale(USED_APP_LOCALE);
    }

    /**
     * Sets the app-wide `{@link Locale}`.
     */
    private void setAppLocale(Locale locale) {
        final Configuration configuration = new Configuration();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale);
        } else {
            configuration.locale = locale;
        }

        // This will not replace the app configuration, but will merely
        // merge the chosen locale into the existing configuration.
        getResources().updateConfiguration(configuration, null);
    }
}

To tell Android it should use your custom MyApplication class, you need to define it within your app’s AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    package="com.pspdfkit.examples.locale"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <application android:name=".MyApplication">

        ...

    </application>

</manifest>