Reputation: 129
I am using an app that have 2 languages, french and arabic and in order to do that i use this code:
Locale locale = new Locale("ar" or "fr");
Locale.setDefault(locale);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
But the problem is that it wants the numbers to be displayed in french, i found here that i need to use these commands:
NumberFormat nf=NumberFormat.getInstance(new Locale("fr","FR"));
nf.format(i);
But it works for a single string at a time and i need to find another command to use it in the all app, so i can set the numbers to be in french in one single step
Upvotes: 1
Views: 1138
Reputation: 5589
To format the numbers in a different locale you can define the NumberFormat ovject in the Application context, and use it from there:
If your app already has an Application object defined you have to add this code to that class, otherwise you create a new class that extends from Application. This is because there is only one instance of Application in your app.
Also keep in mind that the Application class has to be declared in the manifest.
public class MyApplication extends Application{
private NumberFormat nf = NumberFormat.getInstance(new Locale("fr","FR"));
public NumberFormat getNumberFormat(){
return nf;
}
public String getFormattedNmbr(double i){
return nf.format(i);
}
// add here getFormattedNmbr with different argument types
}
In the manifest:
<application
...
android:name="com.you.yourapp.MyApplication">
...
</Application>
In your activities:
getApplication().getNumberFormat().format(number);
//or
getApplication().getFormattedNbr(32.45);
Upvotes: 1