How to change language in android google maps v2 programmatically

最后都变了- 提交于 2019-12-01 13:08:42
public class MapActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        settings.setLocaleResources(getContext());
    }
}

public class Settings {
    final static String PREF_LOCALE = "locale";
    final Static String DEFAULT_LOCALE_STRING = "en_US";

    void setLocaleResources(final Context context) {
        String localeString = PreferenceManager
            .getDefaultSharedPreferences(context)
            .getString(PREF_LOCALE, DEFAULT_LOCALE_STRING);
        Resources res = context.getResources();
        // Change locale settings in the app.
        DisplayMetrics dm = res.getDisplayMetrics();
        Locale locale = getLocale(localeString);
        Locale.setDefault(locale); // this is needed to change even the map's language
        Configuration conf = res.getConfiguration();
        conf.locale = locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            conf.setLayoutDirection(conf.locale);
        }
        res.updateConfiguration(conf, dm);
        return res;
    }

    private static Locale getLocale(final String language2_COUNTRY2) {
        Locale locale;
        if (null == language2_COUNTRY2 || language2_COUNTRY2.isEmpty() || DEFAULT_LOCALE_STRING.equals(language2_COUNTRY2)) {
            locale = MyApp.getSystemLocale();
        } else {
            final String language = language2_COUNTRY2.substring(0, 2).toLowerCase(Locale.ENGLISH);
            final String country = language2_COUNTRY2.substring(3, 5).toUpperCase(Locale.ENGLISH);
            if (null == country || country.length() != 2) {
                locale = locales.get(language);
                if (null == locale) {
                    locale = new Locale(language);
                    locales.put(language, locale);
                }
            } else {
                locale = locales.get(language + '_' + country);
                if (null == locale) {
                    locale = new Locale(language, country);
                    locales.put(language + '_' + country, locale);
                }
            }
        }
        return locale;
    }
}

public class MyApp extends Application {
    @Override
    public void onCreate() {
        defaultSystemLocale = Locale.getDefault();
        Settings.setLocaleResources(getContext());
    }

    public static Locale getSystemLocale() {
        return defaultSystemLocale;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!