Localization

PSPDFKit comes with many languages already built-in:

  • Arabic (ar)
  • Chinese Simplified / Chinese Traditional (zh-Hans/zh-Hant)
  • Croatian (hr)
  • Czech (cs)
  • Danish (da)
  • Dutch (nl)
  • English (en)
  • Finnish (fi)
  • French (fr)
  • German (de)
  • Greek (el)
  • Indonesian (id)
  • Italian (it)
  • Japanese (ja)
  • Korean (ko)
  • Malay (ms)
  • Norwegian Bokmål (nb-NO)
  • Polish (pl)
  • Portuguese Brazil / Portugal (pt/pt-PT)
  • Russian (ru)
  • Serbian (sr)
  • Slovak (sk)
  • Slovenian (sl)
  • Spanish (es)
  • Swedish (sv)
  • Thai (th)
  • Turkish (tr)
  • Ukrainian (uk)

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 have a look at the extracted library aar file in your project's build folder, at build/intermediates/exploded-aar/pspdfkit/values/strings.xml

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.

Copy
MyApplication.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
 * Custom application using a fixed {@link Locale}.
 */
class MyApplication : Application() {

    /**
     * This example uses a hard-coded, 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 changed
        // (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)
    }
}
Copy
MyApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
 * Custom application using a fixed {@link Locale}.
 */
public class MyApplication extends Application {

    /**
     * This example uses a hard-coded, 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 changed
        // (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.

Copy
AndroidManifest.xml
1
2
3
4
5
6
7
8
9
10
11
12
<?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>