1

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

vincrichaud
  • 2,218
  • 17
  • 34
Kingofkech
  • 129
  • 9
  • You should not change the language like that. The device's language should be the primary language of your app. That's why you are having problems and you are trying to work the main issue around. – Alex Mar 07 '18 at 11:53
  • @Alex sometimes for consistency in date formats, or number formats you have a need to use a different locale for specific parts of the application – Juan Mar 07 '18 at 11:59
  • @Juan totally agree, I misunderstood what he was asking. My mistake. So for this case he/she should implement a helper method which would format the values without code duplication – Alex Mar 07 '18 at 12:03

1 Answers1

1

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);
Juan
  • 5,525
  • 2
  • 15
  • 26
  • it will change all the strings not only the numbers ? i wil not be able to display my arabic layouts – Kingofkech Mar 07 '18 at 12:09
  • This only creates a Locale object that you can reference from any part of your app and use it in the NumberFormat (for example) instruction instead of new Locale(). It doesn't change anything by itself. It can also be used in String.format(locale, pattern, args). In any formatting function that takes a locale parameter. – Juan Mar 07 '18 at 12:20
  • my goal is to format all the string to arabic except numbers :/ – Kingofkech Mar 07 '18 at 12:24
  • You can follow a similar approach directly with the NumberFormat instance. I'll assume that the default locale is arabic and I'll update the the answer. – Juan Mar 07 '18 at 12:27
  • I have updated the answer for having the NumberFormat object available in the Application Context, where you could also write the methods to directly get the formatted numbers. Check the number format javadoc to see wich methods to wrap up, I just put one example of how you can do it. – Juan Mar 07 '18 at 12:39