Topic: Datapicker - choose language dynamically

diakeo free asked 3 years ago


Hello, I develop a website with django. It is translated in English and French. It is possible to switch language any time. I use the MDB Datepicker. I understand how I can invoke it in a given language chosen in the code. I know almost nothing to Javascript and jQuery. I would like to know how I can choose the datapicker language dynamically at the time it is raised: have it in English if the form with the date field is currently in English and in French if it is currently in French. Maybe passing a parameter to the calling function (what parameter and what function ?), maybe reading the LANGUAGE_CODE setting from the Javascript or jQuery code ? Thanks a lot


Bartłomiej Malanowski staff commented 3 years ago

What have you tried so far?


diakeo free commented 3 years ago

Hi Bartlomiej, Thanks for your answer. Not much, I've done what is in the documentation: add translation parameters in the Javascript snippet placed in myapp.js $('.datepicker').pickadate({ monthsFull: ['Janvier',.. weekdaysShort: ['Dim',.. ... }); }); This way, I have the datepicker in French. I works fine but is set statically in the code. I'd like to have these parameters only when the user has chosen to have the form in French. Best, Thomas


Bartłomiej Malanowski staff commented 3 years ago

Personally, I'd initialize the date picker basing on Django's language settings. I don't know what's your solution for internationalization of your site so I can't create you a demo but if you tell me how did you "translate" your site or share your code with me, I'll see what can we do with this. If you'd like to share your code with me, here's my email: b.malanowski@mdbootstrap.com


diakeo free commented 3 years ago

Hi Bartlomiej, Thanks for your answers.. and your questions, quite logical. The currently used language at any given time is in a global variable called "LANGUAGE_CODE". It is set by the menu where the user chooses the language (and at init) and then it is one of the mechanisms used in standard by Django to translate everything as needed and there is hardly anything else to do (apart from providing the translated strings). I also use a cookie, the use of which was made necessary by subdomains, the global variable scope being limited to a session and the sessions to a single subdomain. So, either I can set a language value parameter in the call to use the datepicker (and I know how to get the value) or the datepicker can read the environment variable or the cookie. I hope it's clearer. I don't know what piece of code I could send you. My question is more on possibilities provided by the datepicker code. Thanks for your assistance. Thomas



I'm not sure if it's a solution that you're looking for but to my template, I've added the following code and it seems to work:

  <script>
    {% load i18n %}
    {% get_current_language as LANGUAGE_CODE %}

    {% if LANGUAGE_CODE == 'fr-fr' %}
      $.extend($.fn.pickadate.defaults, {
        monthsFull: ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre',
        'Novembre', 'Décembre'],
        weekdaysShort: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
        today: 'aujourd\'hui',
        clear: 'effacer',
        formatSubmit: 'yyyy/mm/dd'
      })
    {% endif %}

    $('#date').pickadate();
  </script>

diakeo free commented 3 years ago

Hi Bartlomiej, Thanks a lot. It looks exactly like what I was looking for. In the meantime, I managed to find a solution with the javascript code reading the cookie. But it's very inelegant. If I manage to adapt your snippet in my code (which I think should be the case), I'll use it. Otherwise, I'll keep my inelegant way (until I learn correctly Javascript and jQuery some day). You can consider you're done with this ticket. I'll let you know whether I could use your code. And, once more, Thanks a lot, that's what I was looking for. Best Thomas



Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • User: Free
  • Premium support: No
  • Technology: MDB jQuery
  • MDB Version: 4.7.3
  • Device: Laptop - VM
  • Browser: Chrome / Firefox / Opera
  • OS: Linux / Windows
  • Provided sample code: No
  • Provided link: No