आप नीचे दिए गए कोड का उपयोग कर सकते हैं। उदाहरण के लिए, नीचे प्रस्तुत किए गए कार्यों को कक्षा के अंदर रखा जा सकता है, जो कि कक्षा वर्ग को बढ़ाया गया है।
public class MyApplication extends Application {
...
public static String APP_LANG;
private Context ctx = getBaseContext();
private Resources res = ctx.getResources();
public SharedPreferences settingPrefs;
...
public void restoreAppLanguage(){
/**
*Use this method to store the app language with other preferences.
*This makes it possible to use the language set before, at any time, whenever
*the app will started.
*/
settingPrefs = getSharedPreferences("ConfigData", MODE_PRIVATE);
String lang = settingPrefs.getString("AppLanguage", "");
if(!settingPrefs.getAll().isEmpty() && lang.length() == 2){
Locale myLocale;
myLocale = new Locale(lang);
Locale.setDefault(myLocale);
Configuration config = new Configuration();
config.locale = myLocale;
res.updateConfiguration(config, res.getDisplayMetrics());
}
}
public void storeAppLanguage(int lang) {
/**
*Store app language on demand
*/
settingPrefs = getSharedPreferences("ConfigData", MODE_PRIVATE);
Editor ed = settingPrefs.edit();
Locale myLocale;
myLocale = new Locale(lang);
Locale.setDefault(myLocale);
Configuration config = new Configuration();
config.locale = myLocale;
res.updateConfiguration(config, res.getDisplayMetrics());
ed.putString("AppLanguage", lang);
ed.commit();
}
public void setAppLanguage(String lang){
/**
*Use this method together with getAppLanguage() to set and then restore
*language, whereever you need, for example, the specifically localized
*resources.
*/
Locale myLocale;
myLocale = new Locale(lang);
Locale.setDefault(myLocale);
Configuration config = new Configuration();
config.locale = myLocale;
res.updateConfiguration(config, res.getDisplayMetrics());
}
public void getAppLanguage(){
/**
*Use this method to obtain the current app language name
*/
settingPrefs = getSharedPreferences("ConfigData",MODE_PRIVATE);
String lang = settingPrefs.getString("AppLanguage", "");
if(!settingPrefs.getAll().isEmpty() && lang.length() == 2){
APP_LANG = lang;
}
else APP_LANG = Locale.getDefault().getLanguage();
}
}
और फिर जहाँ भी मुख्य कोड में हम लिख सकते हैं:
public class MainActivity extends ActionBarActivity {
...
MyApplication app;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
app = (MyApplication)getApplicationContext();
...
if(settingPrefs.getAll().isEmpty()){
//Create language preference if it is not
app.storeAppLanguage(Locale.getDefault().getLanguage());
}
//Restore previously used language
app.restoreAppLanguage();
...
}
...
void SomethingToDo(){
String lang = "en"; //"fr", "de", "es", "it", "ru" etc.
...
app.getAppLanguage();
app.setAppLanguage(lang);
...
//do anything
...
app.setAppLanguage(app.APP_LANG);
}
...
}
आपके मामले में, आप कुछ ही समय, getAppLanguage()
उपयोग कर सकते हैं और उसके बाद प्राप्त करने के लिए क्या भाषा वर्तमान में प्रयोग किया जाता है सार्वजनिक चर APP_LANG
जाँच ।
स्रोत
2015-04-24 11:55:47
मुझे लगता है कि यह समाधान आपके प्रश्न का हिस्सा माना जाना चाहिए था। –
@Yaqub अहमद मैं बाद में इस समाधान में आया था। लेकिन मैं अभी भी संभव बेहतर समाधान की प्रतीक्षा कर रहा हूं। –
मुझे यह समाधान पसंद है क्योंकि तब मुझे केवल स्थानीय कोड प्राप्त करने की गारंटी है यदि मेरे अनुवाद संसाधनों का उपयोग किया जा रहा है। और यही वह समय है जिसे मैं चाहता हूं। अन्यथा, अगर मुझे लोकेल प्राप्त करने में कुछ गलती हुई तो मुझे दूसरी भाषा में एक भाषा और अन्य स्ट्रिंग संसाधनों में तिथियां मिल सकती हैं। – Suragch