2012-11-01 13 views
10

हमारे पास एक एंड्रॉइड ऐप है जो रिमोट पीसी प्रोग्राम के लिए क्लाइंट के रूप में काम करता है। हम एक फीचर जोड़ना चाहते हैं ताकि पीसी एंड्रॉइड ऐप को रनटाइम पर अपना लोकेल बदलने के लिए निर्देश दे सके, यानी ऐप शुरू करें; इसे पीसी के साथ संचार में डाल दें; कुछ समय बाद पीसी ऐप को स्पैनिश या चीनी कहने के लिए कहता है।रनटाइम पर लोकेल बदलें?

हमारे पास पहले से ही उनके संबंधित लोकेशंस के लिए सभी लेआउट और स्ट्रिंग संसाधन स्थापित हैं। हमारा ऐप एकमात्र ऐप है जिसे उपयोगकर्ता देखता है, इससे कोई फर्क नहीं पड़ता कि बाकी डिवाइस अंग्रेजी में रहता है या नहीं।

Change language programmatically in Android पर इस पर एक और धागा है लेकिन यह निष्कर्ष तक नहीं पहुंचता है।

मैं डाल सकता हूं। । ।

Locale locale = new Locale(sTheNewLocale); 
Locale.setDefault(locale); 
Configuration config = new Configuration(); 
config.locale = locale; 
getBaseContext().getResources().updateConfiguration(config, 
     getBaseContext().getResources().getDisplayMetrics()); 

। । । OnCreate() से पहले setContentView() लेकिन वह वास्तव में मदद नहीं करता है अगर मैं स्थान बदलना के बाद मेरे स्क्रीन ऊपर और चल रहा है चाहता हूँ में। गतिविधि पहले से चलने के बाद सामग्री दृश्य को फिर से लोड करने का कोई तरीका है? तो वहाँ स्थानों को बदलने के लिए किसी भी व्यावहारिक तरीका है पर मक्खी मज़बूती से या मैं अपने मालिक बताने के लिए यह एप्लिकेशन शुरू करने से पहले नए स्थान के लिए पूरे उपकरण स्थापित करने के लिए छोड़कर नहीं किया जा सकता है?

उत्तर

15

के बाद से एपीआई 11 आप recreate उपयोग कर सकते हैं तो अपनी गतिविधि में इस विधि बना सकते हैं:

private void restartInLocale(Locale locale) 
{ 
    Locale.setDefault(locale); 
    Configuration config = new Configuration(); 
    config.locale = locale; 
    Resources resources = getResources(); 
    resources.updateConfiguration(config, resources.getDisplayMetrics()); 
    recreate(); 
} 
+1

विधि 'अद्यतन कॉन्फ़िगरेशन() 'बहिष्कृत है। यहां एक गैर-बहिष्कृत समाधान है: http://stackoverflow.com/questions/40221711/android-context-getresources-updateconfiguration-deprecated – wilkas

+0

config.locale को –

+0

@ हसन अलीकाकाका भी कम किया गया है जो लिंक किए गए उत्तर में भी शामिल है। – weston

3

आप अपनी गतिविधि का एक नया उदाहरण शुरू करने और पुराने एक बाहर निकल सकते हैं। यहाँ एक पूर्ण (untested) उदाहरण है कि कैसे आप किसी भी वांछित भाषा की दुकान और अपने अनुप्रयोग को पुनः आरंभ कर सकते हैं। आपको अपनी पसंदीदा भाषा के साथ restartInLanguage पर कॉल करने की आवश्यकता है।

public class YourMainActivity extends Activity { 
    private static final String APP_SHARED_PREFS = "com.example.test"; 
    private SharedPreferences settings; 
    private Editor editor; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     settings=getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE); 
     editor=settings.edit(); 

     Locale locale = new Locale(settings.getString("lang", "default-lang")); 
     Locale.setDefault(locale); 
     Configuration config = new Configuration(); 
     config.locale = locale; 
     getResources().updateConfiguration(config, getResources().getDisplayMetrics()); 

     // your stuff... 
    } 

    public void restartInLanguage(String lang) { 
     editor.putString("lang", lang); 
     editor.commit(); 
     Intent intent = getIntent(); 
     finish(); 
     startActivity(intent); 
    } 

    // ... 
} 
+0

कहाँ मैं इस से करना होगा?कार्यक्रम मुख्य गतिविधि में शुरू होता है तो यह मुख्य स्क्रीन के लोकेल को कैसे बदल देगा? या क्या आप मुख्य गतिविधि से मुख्य गतिविधि का एक नया उदाहरण शुरू करना चाहते हैं और फिर पुरानी मुख्य गतिविधि खुद को मार डालें? (क्या यह भी किया जा सकता है?) – user316117

+0

हां तो यह है ... आप उस कोड को * भाषा बदलकर हैंडलर * में डाल देते हैं जो आपकी मुख्य गतिविधि को पुनरारंभ करता है। – rekire

+0

मैं अभी भी आपके उत्तर का विश्लेषण नहीं कर सकता। क्या आप कह रहे हैं कि मैं अपनी मुख्य गतिविधि * से * मेरी मुख्य गतिविधि को पुनरारंभ कर सकता हूं? मैं उसको कैसे करू? – user316117

0

समाधान का उपयोग करने setContentView और controlLanguage (आप भी एक वैश्विक कक्षा से इस विधि कॉल कर सकते हैं) है अपने लोकेल को सेट करने के बाद रीज्यूम विधि में प्रत्येक गतिविधि में विधियां। उदाहरण:

@Override 
    public void onClick(View v) { 
     SharedPreferences.Editor editor; 
     editor = sharedPref.edit(); 
     Configuration config = new Configuration(getBaseContext().getResources().getConfiguration()); 
     String language = ""; 
     switch (v.getId()){ 
      case R.id.turkceButton: 
       editor.putString("language", "tr"); 
       language="tr"; 
       break; 
      case R.id.englishButton: 
       editor.putString("language", "en"); 
       language="en"; 
       break; 
     } 
     Locale locale = new Locale(language); 
     Locale.setDefault(locale); 
     config.locale = locale; 
     getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); 
     editor.commit(); 


@Override 
    protected void onResume() { 
     super.onResume(); 
     controlLanguage(getApplicationContext(), getResources()); 
     setContentView(R.layout.main); 
    } 

public void controlLanguage(Context context, Resources res){ 
     SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); 
     String language = sharedPref.getString("language","en"); 
     Locale locale = new Locale(language); 
     Locale.setDefault(locale); 
     res.getConfiguration().locale = locale; 
     res.updateConfiguration(res.getConfiguration(), res.getDisplayMetrics()); 
    } 
1

मैं @weston और @rekire संयुक्त है (दोनों +1) और इस यूज-केस को संभालने के लिए यह extendet:

  • ActivityA => ActivityB => SettingsActivity-changeLocale

SettingsActivity में ActivityA और ActivityB को नए लोकेल को प्रतिबिंबित करने के लिए भी पुनर्निर्मित किया जाना चाहिए।

मेरे समाधान: ActivityA और ActivityB LocalizedActivity जो onResume में जाँच करता है से विरासत अगर वातावरण बदल गया है और एक आवश्यक हो तो recreate()

तो हर गतिविधि कि LocalizedActivity से विरासत को गति प्रदान ऐप विशिष्ट स्थान परिवर्तन संभालती है।

/** 
* An activity that can change the locale (language) of its content. 
* 
* Inspired by http://stackoverflow.com/questions/13181847/change-the-locale-at-runtime 
* 
* Created by k3b on 07.01.2016. 
*/ 
public class LocalizedActivity extends Activity { 
    /** if myLocale != Locale.Default : activity must be recreated in on resume */ 
    private Locale myLocale = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     fixLocale(this); 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     // Locale has changed by other Activity ? 
     if ((myLocale != null) && (myLocale.getLanguage() != Locale.getDefault().getLanguage())) { 
      myLocale = null; 
      recreate(); 
     } 
    } 

    /** 
    * Set Activity-s locale to SharedPreferences-setting. 
    * Must be called before onCreate 
    * @param context 
    */ 
    public static void fixLocale(Context context) 
    { 
     final SharedPreferences prefs = PreferenceManager 
       .getDefaultSharedPreferences(context); 
     String language = prefs.getString(Global.PREF_KEY_USER_LOCALE, ""); 
     Locale locale = Global.systemLocale; // in case that setting=="use android-locale" 
     if ((language != null) && (!language.isEmpty())) { 
      locale = new Locale(language); // overwrite "use android-locale" 
     } 

     if (locale != null) { 
      Locale.setDefault(locale); 
      Configuration config = new Configuration(); 
      config.locale = locale; 
      Resources resources = context.getResources(); 
      resources.updateConfiguration(config, resources.getDisplayMetrics()); 
      // recreate(); 

      if (context instanceof LocalizedActivity) { 
       ((LocalizedActivity) context).myLocale = locale; 
      } 
     } 
    } 
} 

यहाँ LocalizedActivity.java और SettingsActivity.java के स्रोत में उपयोग किया जाता है कि है मेरी A Photo Manager project

संबंधित मुद्दे