2014-10-07 81 views
1

我有一個應用程序,您可以從設置中更改區域設置(目的是能夠在應用程序內部具有與系統區域設置不同的區域設置),而我希望能夠設置地圖的語言。我只能找到像「設置您的手機的系統語言」,這不是我正在尋找的答案。有沒有一種方法來設置地圖的語言編程或從XML?如何以編程方式在android google maps v2中更改語言

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"/> 
+0

一個非常糟糕的主意。 Google建議您不要更改語言,而是使用用戶選擇的語言。 – Martin 2014-10-07 22:04:58

+1

@Martin,其實這是一個非常好的主意。 「您可以從設置中更改區域設置」,但不必更改默認(系統)語言。爲什麼會這樣做有幾個原因。例如,我的手機的默認語言是en_US,但是這個應用程序在希伯來語中是「更好」的,因爲顯示的一些實時數據沒有被翻譯成英語,並且由於ltr和rtl文本方向如果應用程序是英文的,那麼在句子中混雜起來很難閱讀,但是我不想強迫希伯來語對任何人。 – Gavriel 2014-10-07 22:28:50

+0

由於地圖由另一個進程渲染,如果您有控制其語言的手段,我會感到很驚訝。 – CommonsWare 2014-10-07 23:52:09

回答

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

它確實會更改應用程序語言,但不會更改地圖 – 2018-02-12 07:19:00

相關問題