Reputation: 1005
We are building a product where we are planning to push static String from server for English and French users,for the purpose of performance. Both the Strings will be pushed at the same time.We are providing a switch on Navigation Drawer to switch between English and language. Since these strings will keep on changing over time period we can't keep them under values and values-fr folders.
Now there are couple of problems
When device locale changes we need to switch the language of application using these string at runtime.Is there any way to load string dynamically at run time into values and values-fr folder.
If above is not possible we are planning to perform checks on every View for each string,which will increase the complexity of code.
Is there any alternative or solution to above problem which we can implement.Any help is highly appreciated.
Upvotes: 2
Views: 772
Reputation: 91
use Application class
public class MyApplication extends Application {
private static MyApplication instance = null;
private String frenchValue;
private String englishValue;
private String env_lang_Value;
public String getFrenchValue() {
return frenchValue;
}
public void setFrenchValue(String frenchValue) {
UnravelApplication.frenchValue = frenchValue;
}
public String getEnglishValue() {
return englishValue;
}
public void setEnglishValue(String englishValue) {
UnravelApplication.englishValue = englishValue;
}
public String getEnv_lang_Value() {
return env_lang_Value;
}
public void setEnv_lang_Value(String env_lang_Value) {
UnravelApplication.env_lang_Value = env_lang_Value;
}
private MyApplication() {
// Exists only to defeat instantiation.
}
public static MyApplication getInstance() {
if(instance == null) {
instance = new MyApplication();
}
return instance;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.locale.getLanguage() == Configuration.locale.ENGLISH) {
env_lang_value=englishValue
}
if (newConfig.locale.getLanguage() == Configuration.locale.FRENCH) {
env_lang_value=frenchValue
}
}
}
from any activity while receiving push static String from server
public MyActivity extends Activity{
MyApplication myApplication = MyApplication.getInstance();
//to set while getting strings from server for both english and french
myApplication.setEnglishValue(englishValue);
myApplication.setFrenchValue(frenchValue);
if(Locale.getDefault().getDisplayLanguage().equalsIgnoreCase("English"))
myApplication.setEnv_lang_value=englishValue;
if(Locale.getDefault().getDisplayLanguage().equalsIgnoreCase("French"))
myApplication.setEnv_lang_value=frenchValue;
//to get value
String currentValue=myApplication.getEnv_lang_value;
}
Don't forget to specify Application class name in your AndroidManifest.xml's tag
<application android:icon="@drawable/icon" android:label="@string/app_name" android:name="MyApplication">
Upvotes: 0
Reputation: 157487
strings.xml
, with a set of empty items, only for the sake of the ids. You can use, of course, for this purpose, also ids.xml
. When the application started, I download the file which contained the pair key, value
, where the key
was the name
attribute I used in strings.xml
, and put the pair in a HashMap<String, String>
. In the end I had a small manager, which made the translation from id
to name
, retrieved
the string from the HashMap
and returned it to the client. As you can image it was/is a nightmare from the maintainability point of view. I would strongly discourage it, but I understand that sometimes it is just not possible
Upvotes: 2