如何以编程方式更改Android Google Maps V2中的语言 [英] How to change language in android google maps v2 programmatically

查看:102
本文介绍了如何以编程方式更改Android Google Maps V2中的语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,您可以在其中更改设置的语言环境(目的是使应用程序中的语言环境与系统语言环境不同),并且我希望能够设置地图的语言也一样我只能找到诸如设置手机的系统语言"之类的答案,这不是我想要的.有没有办法通过编程方式或从xml设置地图的语言?

I have an app where you can change the locale from the settings (the purpose is to be able to have a locale inside the app that is different from the system locale) and I would like to be able to set the map's language as well. I could only find answers like "set the system language of your phone", which is not what I am looking for. Is there a way to set the map's language programmatically or from xml?

public class LocalizedActivity extends FragmentActivity {
    private GoogleMap map;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // you need to call this ASAP:
        setLocaleResources("iw");
        // after this most parts of the UI are localized, but not the map

        setContentView(R.layout.main);

        android.support.v4.app.FragmentManager sfm = getSupportFragmentManager();
        final Fragment f = sfm.findFragmentById(R.id.map);
        final SupportMapFragment mf = (SupportMapFragment) f;
        map = mf.getMap();
    }

    static void setLocaleResources(final String languageCode) {
        final Context context = RedAlert.getContext();
        Resources res = context.getResources();
        // Change locale settings in the app.
        DisplayMetrics dm = res.getDisplayMetrics();
        android.content.res.Configuration conf = res.getConfiguration();
        conf.locale = new Locale(languageCode);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            conf.setLayoutDirection(conf.locale);
        }
        res.updateConfiguration(conf, dm);
    }

main.xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.SupportMapFragment"/>

推荐答案

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;
    }
}

这篇关于如何以编程方式更改Android Google Maps V2中的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆