0

I am learning Django's ways of doing internationalization and localization. To do so, I am following this tutorial:

http://www.marinamele.com/taskbuster-django-tutorial/internationalization-localization-languages-time-zones

The issue I have encountered is that the test the tutorial proposes is not working. Furthermore, I cannot see how it could ever have worked.

Here is the relevant template bit

  <div class="row">
    <div class="col-md-4">
      <h2 id="local-date">{{today}}</h2>
      <p>This is the time using your local information. </p>
      <p><a class="btn btn-default" href="#" role="button">View details &raquo;</a></p>
    </div>
    <div class="col-md-4">
      <h2 id="non-local-date">{{today|unlocalize}}</h2>
      <p>This is the default time format. </p>
      <p><a class="btn btn-default" href="#" role="button">View details &raquo;</a></p>
   </div> 

The view is as follows

from django.shortcuts import render
import datetime

def home(request):
    today = datetime.date.today()
    return render(request, "taskbuster/index.html", {'today': today})

The test is below

def test_localization(self):
    today = date.today()
    for lang in ['en', 'ca']:
        activate(lang)
        self.browser.get(self.get_full_url("home"))
        local_date = self.browser.find_element_by_id("local-date")
        non_local_date = self.browser.find_element_by_id("non-local-date")
        self.assertEqual(formats.date_format(today, use_l10n=True),
                              local_date.text)
        self.assertEqual(today.strftime('%Y-%m-%d'), non_local_date.text)

The failure I am seeing is

    self.assertEqual(today.strftime('%B %d, %Y'), non_local_date.text)
AssertionError: 'June 22, 2019' != 'jun. 22, 2019'
- June 22, 2019
? ^  ^
+ jun. 22, 2019

? ^ ^

As you can see, the Catalan version is not the same as the default non-localized version. Am I missing something obvious here? Has something changed with the way these bits work in Django that creates this failure? The author of the tutorial claims that this test should pass at this point in the tutorial.

P.S. If one adds other, more interesting languages in which there is a more logical day->month->year format for displaying dates, he can observe that failure as well. Consider the case of ru:

AssertionError: '22 June, 2019' != 'Июнь 22, 2019'
- 22 June, 2019
+ Июнь 22, 2019
MadPhysicist
  • 5,401
  • 11
  • 42
  • 107

1 Answers1

2

I too have been working thru the Marina Mele Django tutorial. I have found a great number of problems with it. For the section on date localization, I burned a couple of hours until I figured out that index.html in the tutorial needs to have both the following load statements to be able to handle both date localization and translation, as in:

{% extends "base.html" %}
{% block head_title %}TaskBuster Django Tutorial{% endblock %}
{% load i18n %}
{% load l10n %}

{% block content %}

Without this, you get an "invalid filter" error. I'm not sure if this will cure the problem you are having, but it is an example to illustrate that, although the tutorial is overall very good, problems you have trying to execute everything may be due to errors or omissions in the tutorial (in some cases could be because of incompatibilities from newer versions of packages that you have to install as part of the project).... at least, this provides useful (if frustrating) practice in debugging!

AB4EJ
  • 51
  • 7