21

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);
bigstones
  • 15,087
  • 7
  • 65
  • 82
begiPass
  • 2,124
  • 6
  • 36
  • 57

6 Answers6

26

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.

lightbyte
  • 566
  • 4
  • 10
  • 4
    Hi @lightbyte , it change the top section display date but not the week day for the month name.Still they so as M, T ,W etc. – blackjack Mar 28 '19 at 08:46
13

You can just add this declaration before DatePicker:

Locale.setDefault(Locale.FRANCE);
Micer
  • 8,731
  • 3
  • 79
  • 73
pierre g
  • 131
  • 1
  • 2
  • 1
    Be careful if the app has other localized parts in your app, as this will change all of the app localization once the dialog is opened. – htafoya Aug 16 '19 at 17:52
  • 4
    This doesn't seem to translate ALL the date picker window, only the **Fri, Nov 21, 2014** header but not the month and day column names. – htafoya Aug 26 '19 at 19:51
6

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.

Igor K
  • 470
  • 6
  • 10
  • 2
    Hi guys, can you please tell me which min, target API is set in your app, because I get java.lang.NoSuchMethodException: updateSpinners [] in this line: final Method updateSpinner = mDatePicker.getClass().getDeclaredMethod("updateSpinners"); – DoubleK May 26 '15 at 08:26
  • In my application it was 16 and 21. As we can see _getDeclaredMethod()_ method was added in API level 9. It means that this solution won't work for devices below API level 9. If you are developing for devices below you need to use this solution only for devices from API level 9 and non-localized/alternative solution for other devices. Maybe you can obtain all methods with _getDeclaringMethods()_ method and then iterate through them. (Just a hint, I haven't tried it.) – Igor K May 27 '15 at 08:27
  • @IgorK How to use it for `TimepickerDialog` – Sagar Dec 20 '18 at 06:34
  • @SagarHudge I have no idea what do you mean by localizing TimePickerDialog. As I have seen there are 2 types of timepickers like here: https://stackoverflow.com/questions/18208082/creating-timepickerdialog-with-custom-style-in-android (12 pm/am & just 24 hours). What is your use case? I doubt you need am/pm type for all languages. – Igor K Dec 21 '18 at 22:26
  • this is a long process, android date picker directly pick the applied language directly from the context, try with this answer - https://stackoverflow.com/a/57889138/3448003 – Bajrang Hudda Sep 11 '19 at 12:32
  • what is short months? – Prajwal Waingankar Apr 06 '21 at 06:50
  • @PrajwalWaingankar there was a typo, corrected. – Igor K Apr 09 '21 at 20:40
2

Relevant classes:

http://developer.android.com/reference/android/app/DatePickerDialog.html http://developer.android.com/reference/android/widget/DatePicker.html http://developer.android.com/reference/android/widget/CalendarView.html

Currently I don't see any possibility to use the standard one with usage of localisation options or reuse the timepickerdialog and datepicker implementation to implement your own calenderview (you can get the calenderview but you can't set a calenderview). You can always implement your own custom dialog.

Also I found this opensource library on the web.

It indicates that it would handle local settings. Haven't tried it though.

Tobrun
  • 18,291
  • 10
  • 66
  • 81
1

Igor's answer helped me a lot. But I'd like to add that you might run into NoSuchMethodException as DoubleK pointed out, because in some TimePicker and DatePicker implementations there're separate classes like TimePickerSpinnerDelegate and DatePicker.DatePickerSpinnerDelegate which contain these variables and methods. Here's how I updated the pickers (for API 14+):

private void initPicker(Object object, String[] values) {
        try {
            Field[] fields = object.getClass().getDeclaredFields();

            for (Field field : fields) {
                // If there's a delegate, we use it instead.
                if (field.getName().equals("mDelegate")) {
                    field.setAccessible(true);
                    object = field.get(object);
                    fields = object.getClass().getDeclaredFields();
                    break;
                }
            }

            for (Field field : fields) {
                if (field.getName().equals("mAmPmStrings") ||
                        field.getName().equals("mShortMonths")) {
                    field.setAccessible(true);
                    field.set(object, values);
                } else if (field.getName().equals("mAmPmSpinner") ||
                        field.getName().equals("mMonthSpinner")) {
                    field.setAccessible(true);
                    Object innerObject = field.get(object);
                    Method method = innerObject.getClass().getDeclaredMethod(
                            "setDisplayedValues", String[].class);
                    method.setAccessible(true);
                    method.invoke(innerObject, (Object) values);
                }
            }

            Method[] methods = object.getClass().getDeclaredMethods();

            for (Method method : methods) {
                if (method.getName().equals("updateAmPmControl") ||
                        method.getName().equals("updateSpinners")) {
                    method.setAccessible(true);
                    method.invoke(object);
                    break;
                }
            }
        } catch (Exception e) {
            Log.e(APP_TAG, e.getMessage(), e);
        }
    }

So I just call

initPicker(timePicker, resources.getStringArray(R.array.am_pm));

and

initPicker(datePicker, resources.getStringArray(R.array.calendar_months));

after the views are created and everything works as expected.

Nikolai
  • 821
  • 3
  • 17
  • 22
0

You may try changing context of DateDialogPicker dialog. Whenever such problem occurs most of the time it's because of your current context.

I had same problem, sometimes I was able to change the languagle but some times not, so I tried changing context and now it's working fine. Before it was fragment context (requireContext()) as below -

datePickerDialog = DatePickerDialog(
            requireContext(), R.style.TimePickerTheme.....

but later I changed to below (activity context - requireActivity()) that is working fine-

datePickerDialog = DatePickerDialog(
            requireActivity(), R.style.TimePickerTheme.....
Bajrang Hudda
  • 3,028
  • 1
  • 36
  • 63