Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set language to French in android DatePickerDialog

Is there any way to have date displayed in DatePickerDialog in french

I have searched about this but found no results

Here is my code:

 Calendar c = Calendar.getInstance();

 picker = new DatePickerDialog(PaymentView.this, 
                               PaymentView.this,
                   c.get(Calendar.YEAR), 
                               c.get(Calendar.MONTH),
                   c.get(Calendar.DAY_OF_MONTH));

 picker.setIcon(R.drawable.ic_launcher);
 picker.setTitle("Choisir la date");
 picker.getDatePicker().setMinDate(System.currentTimeMillis() - 2000);

Instead of Fri, Nov 21, 2014 I want to have french abbreviation I have also added that before instantiate it :

 Locale locale = new Locale("FR");
 Locale.setDefault(locale);
 Configuration config = new Configuration();
 config.locale = locale;
 getApplicationContext().getResources().updateConfiguration(config, null);
like image 949
begiPass Avatar asked Jan 21 '14 11:01

begiPass


3 Answers

I made it!

I have added these 2 lines before all the DatePickerDialog stuff:

Locale locale = getResources().getConfiguration().locale;
Locale.setDefault(locale);

and after that:

Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(MainActivity.this,
            this, mYear, mMonth, mDay);
dialog.getDatePicker().setMaxDate(c.getTimeInMillis());
dialog.setTitle(R.string.main_first_day_of_your_last_period);
dialog.show();

I hope it helps someone.

like image 62
lightbyte Avatar answered Sep 28 '22 00:09

lightbyte


You can just add this declaration before DatePicker:

Locale.setDefault(Locale.FRANCE);
like image 38
pierre g Avatar answered Sep 28 '22 01:09

pierre g


I had almost similar problem and I found solution for this answer here

In my case I needed:

  1. have initPicker(context) method from the link

     /**
      * Use reflection to use the Locale of the application for the month spinner.
      * 
      * PS: DAMN DATEPICKER DOESN'T HONOR Locale.getDefault()
      * <a href="http://code.google.com/p/android/issues/detail?id=25107">Bug Report</a>
      * @param context
      */
     public void initPicker(Context context) {
         String monthPickerVarName;
         String months[] =  context.getResources().getStringArray(R.array.short_months);
    
         if (Build.VERSION.SDK_INT >= 14) {
             monthPickerVarName = "mMonthSpinner";
         } else {
             monthPickerVarName = "mMonthPicker";
         }
    
         try {
             Field f[] = mDatePicker.getClass().getDeclaredFields();
    
             for (Field field : f) {
                 if (field.getName().equals("mShortMonths")) {
                     field.setAccessible(true);
                     field.set(mDatePicker, months); 
                 } else if (field.getName().equals(monthPickerVarName)) {
                     field.setAccessible(true);
                     Object o = field.get(mDatePicker);
                     if (Build.VERSION.SDK_INT >= 14) {
                         Method m = o.getClass().getDeclaredMethod("setDisplayedValues", String[].class);
                         m.setAccessible(true);
                         m.invoke(o, (Object)months);
                     } else {
                         Method m = o.getClass().getDeclaredMethod("setRange", int.class, int.class, String[].class);
                         m.setAccessible(true);
                         m.invoke(o, 1, 12, (Object)months);
                     }
                 }
             }
    
         } catch (Exception e) {
             Log.e(TAG, e.getMessage(), e);
         }
    
         try {
             final Method updateSpinner = mDatePicker.getClass().getDeclaredMethod("updateSpinners");
             updateSpinner.setAccessible(true);
             updateSpinner.invoke(mDatePicker);
             updateSpinner.setAccessible(false);
    
    
         } catch (Exception e) {
             Log.e(TAG, e.getMessage(), e);
         }
    
     }
    
  2. to call initPicker(context) before call to picker.init(...) in onViewCreated method of my custom widget

  3. Define short_months in res/values/arrays.xml

     <string-array name="short_months">
         <item>Jan</item>
         <item>Feb</item>
         <item>Mar</item>
         <item>Apr</item>
         <item>May</item>
         <item>Jun</item>
         <item>Jul</item>
         <item>Aug</item>
         <item>Sep</item>
         <item>Oct</item>
         <item>Nov</item>
         <item>Dec</item>
     </string-array>
    
  4. Define short_months in res/values-ru/arrays.xml (in this question must be analogue res/values-fr/arrays.xml)

     <string-array name="short_months">
         <item>Янв</item>
         <item>Фев</item>
         <item>Мар</item>
         <item>Апр</item>
         <item>Май</item>
         <item>Июн</item>
         <item>Июл</item>
         <item>Авг</item>
         <item>Сеп</item>
         <item>Окт</item>
         <item>Ноя</item>
         <item>Дек</item>
     </string-array>
    

EDIT: Typo month_values were replaced with short_months in arrays.

like image 32
Igor K Avatar answered Sep 27 '22 23:09

Igor K