summaryrefslogtreecommitdiff
path: root/main/src/main/java/de/blinkt/openvpn/core/LocaleHelper.java
blob: 2b0c1975723093d0c56c8e8bb316f88f55a7e81d (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Copyright (c) 2012-2021 Arne Schwabe
 * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
 */

package de.blinkt.openvpn.core;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;

import java.util.Locale;

public class LocaleHelper {
    static private Locale desiredLocale = null;

    public static void setDesiredLocale(Context c)
    {
        Locale current = Locale.getDefault();
        boolean defForce = true;
        if (current.getLanguage().equals(new Locale("de").getLanguage()))
            defForce = false;

        boolean allow_translation = Preferences.getDefaultSharedPreferences(c).getBoolean("allow_translation", defForce);

        if (!allow_translation)
            desiredLocale =  new Locale("en", current.getCountry());
    }

    public static Context updateResources(Context context) {
        if (desiredLocale == null)
            return context;

        Locale.setDefault(desiredLocale);

        Resources res = context.getResources();
        Configuration config = new Configuration(res.getConfiguration());
        if (Build.VERSION.SDK_INT >= 17) {
            config.setLocale(desiredLocale);
            context = context.createConfigurationContext(config);
        } else {
            config.locale = desiredLocale;
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
        return context;
    }

    public static void onConfigurationChange(Context context)
    {
        Resources res = context.getResources();

        Locale current;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
            current = res.getConfiguration().getLocales().get(0);
        else
            current = res.getConfiguration().locale;


        if (current == desiredLocale)
            return;

        Configuration config = new Configuration(res.getConfiguration());

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
            config.setLocale(desiredLocale);
        else
            config.locale = desiredLocale;

        res.updateConfiguration(config, res.getDisplayMetrics());
    }
}