Ejemplo n.º 1
0
    def test_l10n_disabled(self):
        """
        Catalan locale with format i18n disabled translations will be used,
        but not formats
        """
        settings.USE_L10N = False
        activate('ca')
        try:
            self.assertEqual('N j, Y', get_format('DATE_FORMAT'))
            self.assertEqual(0, get_format('FIRST_DAY_OF_WEEK'))
            self.assertEqual('.', get_format('DECIMAL_SEPARATOR'))
            self.assertEqual(u'10:15 a.m.', time_format(self.t))
            self.assertEqual(u'des. 31, 2009', date_format(self.d))
            self.assertEqual(u'desembre 2009', date_format(self.d, 'YEAR_MONTH_FORMAT'))
            self.assertEqual(u'12/31/2009 8:50 p.m.', date_format(self.dt, 'SHORT_DATETIME_FORMAT'))
            self.assertEqual('No localizable', localize('No localizable'))
            self.assertEqual(decimal.Decimal('66666.666'), localize(self.n))
            self.assertEqual(99999.999, localize(self.f))
            self.assertEqual(datetime.date(2009, 12, 31), localize(self.d))
            self.assertEqual(datetime.datetime(2009, 12, 31, 20, 50), localize(self.dt))
            self.assertEqual(u'66666.666', Template('{{ n }}').render(self.ctxt))
            self.assertEqual(u'99999.999', Template('{{ f }}').render(self.ctxt))
            self.assertEqual(u'2009-12-31', Template('{{ d }}').render(self.ctxt))
            self.assertEqual(u'2009-12-31 20:50:00', Template('{{ dt }}').render(self.ctxt))
            self.assertEqual(u'66666.67', Template('{{ n|floatformat:2 }}').render(self.ctxt))
            self.assertEqual(u'100000.0', Template('{{ f|floatformat }}').render(self.ctxt))
            self.assertEqual(u'10:15 a.m.', Template('{{ t|time:"TIME_FORMAT" }}').render(self.ctxt))
            self.assertEqual(u'12/31/2009', Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
            self.assertEqual(u'12/31/2009 8:50 p.m.', Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt))

            form = I18nForm({
                'decimal_field': u'66666,666',
                'float_field': u'99999,999',
                'date_field': u'31/12/2009',
                'datetime_field': u'31/12/2009 20:50',
                'time_field': u'20:50',
                'integer_field': u'1.234',
            })
            self.assertEqual(False, form.is_valid())
            self.assertEqual([u'Introdu\xefu un n\xfamero.'], form.errors['float_field'])
            self.assertEqual([u'Introdu\xefu un n\xfamero.'], form.errors['decimal_field'])
            self.assertEqual([u'Introdu\xefu una data v\xe0lida.'], form.errors['date_field'])
            self.assertEqual([u'Introdu\xefu una data/hora v\xe0lides.'], form.errors['datetime_field'])
            self.assertEqual([u'Introdu\xefu un n\xfamero sencer.'], form.errors['integer_field'])

            form2 = SelectDateForm({
                'date_field_month': u'12',
                'date_field_day': u'31',
                'date_field_year': u'2009'
            })
            self.assertEqual(True, form2.is_valid())
            self.assertEqual(datetime.date(2009, 12, 31), form2.cleaned_data['date_field'])
            self.assertEqual(
                u'<select name="mydate_month" id="id_mydate_month">\n<option value="1">gener</option>\n<option value="2">febrer</option>\n<option value="3">mar\xe7</option>\n<option value="4">abril</option>\n<option value="5">maig</option>\n<option value="6">juny</option>\n<option value="7">juliol</option>\n<option value="8">agost</option>\n<option value="9">setembre</option>\n<option value="10">octubre</option>\n<option value="11">novembre</option>\n<option value="12" selected="selected">desembre</option>\n</select>\n<select name="mydate_day" id="id_mydate_day">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31" selected="selected">31</option>\n</select>\n<select name="mydate_year" id="id_mydate_year">\n<option value="2009" selected="selected">2009</option>\n<option value="2010">2010</option>\n<option value="2011">2011</option>\n<option value="2012">2012</option>\n<option value="2013">2013</option>\n<option value="2014">2014</option>\n<option value="2015">2015</option>\n<option value="2016">2016</option>\n<option value="2017">2017</option>\n<option value="2018">2018</option>\n</select>',
                SelectDateWidget(years=range(2009, 2019)).render('mydate', datetime.date(2009, 12, 31))
            )
        finally:
            deactivate()
Ejemplo n.º 2
0
    def test_l10n_disabled(self):
        """
        Catalan locale with format i18n disabled translations will be used,
        but not formats
        """
        settings.USE_L10N = False
        activate("ca")
        try:
            self.assertEqual("N j, Y", get_format("DATE_FORMAT"))
            self.assertEqual(0, get_format("FIRST_DAY_OF_WEEK"))
            self.assertEqual(".", get_format("DECIMAL_SEPARATOR"))
            self.assertEqual(u"10:15 a.m.", time_format(self.t))
            self.assertEqual(u"des. 31, 2009", date_format(self.d))
            self.assertEqual(u"desembre 2009", date_format(self.d, "YEAR_MONTH_FORMAT"))
            self.assertEqual(u"12/31/2009 8:50 p.m.", date_format(self.dt, "SHORT_DATETIME_FORMAT"))
            self.assertEqual("No localizable", localize("No localizable"))
            self.assertEqual(decimal.Decimal("66666.666"), localize(self.n))
            self.assertEqual(99999.999, localize(self.f))
            self.assertEqual(datetime.date(2009, 12, 31), localize(self.d))
            self.assertEqual(datetime.datetime(2009, 12, 31, 20, 50), localize(self.dt))
            self.assertEqual(u"66666.666", Template("{{ n }}").render(self.ctxt))
            self.assertEqual(u"99999.999", Template("{{ f }}").render(self.ctxt))
            self.assertEqual(u"2009-12-31", Template("{{ d }}").render(self.ctxt))
            self.assertEqual(u"2009-12-31 20:50:00", Template("{{ dt }}").render(self.ctxt))
            self.assertEqual(u"66666.67", Template("{{ n|floatformat:2 }}").render(self.ctxt))
            self.assertEqual(u"100000.0", Template("{{ f|floatformat }}").render(self.ctxt))
            self.assertEqual(u"10:15 a.m.", Template('{{ t|time:"TIME_FORMAT" }}').render(self.ctxt))
            self.assertEqual(u"12/31/2009", Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
            self.assertEqual(
                u"12/31/2009 8:50 p.m.", Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt)
            )

            form = I18nForm(
                {
                    "decimal_field": u"66666,666",
                    "float_field": u"99999,999",
                    "date_field": u"31/12/2009",
                    "datetime_field": u"31/12/2009 20:50",
                    "time_field": u"20:50",
                }
            )
            self.assertEqual(False, form.is_valid())
            self.assertEqual([u"Introdu\xefu un n\xfamero."], form.errors["float_field"])
            self.assertEqual([u"Introdu\xefu un n\xfamero."], form.errors["decimal_field"])
            self.assertEqual([u"Introdu\xefu una data v\xe0lida."], form.errors["date_field"])
            self.assertEqual([u"Introdu\xefu una data/hora v\xe0lides."], form.errors["datetime_field"])

            form2 = SelectDateForm({"date_field_month": u"12", "date_field_day": u"31", "date_field_year": u"2009"})
            self.assertEqual(True, form2.is_valid())
            self.assertEqual(datetime.date(2009, 12, 31), form2.cleaned_data["date_field"])
            self.assertEqual(
                u'<select name="mydate_month" id="id_mydate_month">\n<option value="1">gener</option>\n<option value="2">febrer</option>\n<option value="3">mar\xe7</option>\n<option value="4">abril</option>\n<option value="5">maig</option>\n<option value="6">juny</option>\n<option value="7">juliol</option>\n<option value="8">agost</option>\n<option value="9">setembre</option>\n<option value="10">octubre</option>\n<option value="11">novembre</option>\n<option value="12" selected="selected">desembre</option>\n</select>\n<select name="mydate_day" id="id_mydate_day">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31" selected="selected">31</option>\n</select>\n<select name="mydate_year" id="id_mydate_year">\n<option value="2009" selected="selected">2009</option>\n<option value="2010">2010</option>\n<option value="2011">2011</option>\n<option value="2012">2012</option>\n<option value="2013">2013</option>\n<option value="2014">2014</option>\n<option value="2015">2015</option>\n<option value="2016">2016</option>\n<option value="2017">2017</option>\n<option value="2018">2018</option>\n</select>',
                SelectDateWidget(years=range(2009, 2019)).render("mydate", datetime.date(2009, 12, 31)),
            )
        finally:
            deactivate()
Ejemplo n.º 3
0
    def test_l10n_enabled(self):
        """
        Catalan locale
        """
        settings.USE_L10N = True
        activate('ca')
        try:
            self.assertEqual('j \de F \de Y', get_format('DATE_FORMAT'))
            self.assertEqual(1, get_format('FIRST_DAY_OF_WEEK'))
            self.assertEqual(',', get_format('DECIMAL_SEPARATOR'))
            self.assertEqual(u'10:15:48', time_format(self.t))
            self.assertEqual(u'31 de desembre de 2009', date_format(self.d))
            self.assertEqual(u'desembre del 2009', date_format(self.d, 'YEAR_MONTH_FORMAT'))
            self.assertEqual(u'31/12/2009 20:50', date_format(self.dt, 'SHORT_DATETIME_FORMAT'))
            self.assertEqual('No localizable', localize('No localizable'))

            settings.USE_THOUSAND_SEPARATOR = True
            self.assertEqual(u'66.666,666', localize(self.n))
            self.assertEqual(u'99.999,999', localize(self.f))

            settings.USE_THOUSAND_SEPARATOR = False
            self.assertEqual(u'66666,666', localize(self.n))
            self.assertEqual(u'99999,999', localize(self.f))
            self.assertEqual(u'31 de desembre de 2009', localize(self.d))
            self.assertEqual(u'31 de desembre de 2009 a les 20:50', localize(self.dt))

            settings.USE_THOUSAND_SEPARATOR = True
            self.assertEqual(u'66.666,666', Template('{{ n }}').render(self.ctxt))
            self.assertEqual(u'99.999,999', Template('{{ f }}').render(self.ctxt))

            settings.USE_THOUSAND_SEPARATOR = False
            self.assertEqual(u'66666,666', Template('{{ n }}').render(self.ctxt))
            self.assertEqual(u'99999,999', Template('{{ f }}').render(self.ctxt))
            self.assertEqual(u'31 de desembre de 2009', Template('{{ d }}').render(self.ctxt))
            self.assertEqual(u'31 de desembre de 2009 a les 20:50', Template('{{ dt }}').render(self.ctxt))
            self.assertEqual(u'66666,67', Template('{{ n|floatformat:2 }}').render(self.ctxt))
            self.assertEqual(u'100000,0', Template('{{ f|floatformat }}').render(self.ctxt))
            self.assertEqual(u'10:15:48', Template('{{ t|time:"TIME_FORMAT" }}').render(self.ctxt))
            self.assertEqual(u'31/12/2009', Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
            self.assertEqual(u'31/12/2009 20:50', Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt))

            form3 = I18nForm({
                'decimal_field': u'66666,666',
                'float_field': u'99999,999',
                'date_field': u'31/12/2009',
                'datetime_field': u'31/12/2009 20:50',
                'time_field': u'20:50'
            })
            self.assertEqual(True, form3.is_valid())
            self.assertEqual(decimal.Decimal('66666.666'), form3.cleaned_data['decimal_field'])
            self.assertEqual(99999.999, form3.cleaned_data['float_field'])
            self.assertEqual(datetime.date(2009, 12, 31), form3.cleaned_data['date_field'])
            self.assertEqual(datetime.datetime(2009, 12, 31, 20, 50), form3.cleaned_data['datetime_field'])
            self.assertEqual(datetime.time(20, 50), form3.cleaned_data['time_field'])

            form4 = SelectDateForm({
                'date_field_month': u'12',
                'date_field_day': u'31',
                'date_field_year': u'2009'
            })
            self.assertEqual(True, form4.is_valid())
            self.assertEqual(datetime.date(2009, 12, 31), form4.cleaned_data['date_field'])
            self.assertEqual(
                u'<select name="mydate_day" id="id_mydate_day">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31" selected="selected">31</option>\n</select>\n<select name="mydate_month" id="id_mydate_month">\n<option value="1">gener</option>\n<option value="2">febrer</option>\n<option value="3">mar\xe7</option>\n<option value="4">abril</option>\n<option value="5">maig</option>\n<option value="6">juny</option>\n<option value="7">juliol</option>\n<option value="8">agost</option>\n<option value="9">setembre</option>\n<option value="10">octubre</option>\n<option value="11">novembre</option>\n<option value="12" selected="selected">desembre</option>\n</select>\n<select name="mydate_year" id="id_mydate_year">\n<option value="2009" selected="selected">2009</option>\n<option value="2010">2010</option>\n<option value="2011">2011</option>\n<option value="2012">2012</option>\n<option value="2013">2013</option>\n<option value="2014">2014</option>\n<option value="2015">2015</option>\n<option value="2016">2016</option>\n<option value="2017">2017</option>\n<option value="2018">2018</option>\n</select>',
                SelectDateWidget(years=range(2009, 2019)).render('mydate', datetime.date(2009, 12, 31))
            )
        finally:
            deactivate()

        # English locale

        settings.USE_L10N = True
        activate('en')
        try:
            self.assertEqual('N j, Y', get_format('DATE_FORMAT'))
            self.assertEqual(0, get_format('FIRST_DAY_OF_WEEK'))
            self.assertEqual('.', get_format('DECIMAL_SEPARATOR'))
            self.assertEqual(u'Dec. 31, 2009', date_format(self.d))
            self.assertEqual(u'December 2009', date_format(self.d, 'YEAR_MONTH_FORMAT'))
            self.assertEqual(u'12/31/2009 8:50 p.m.', date_format(self.dt, 'SHORT_DATETIME_FORMAT'))
            self.assertEqual('No localizable', localize('No localizable'))

            settings.USE_THOUSAND_SEPARATOR = True
            self.assertEqual(u'66,666.666', localize(self.n))
            self.assertEqual(u'99,999.999', localize(self.f))

            settings.USE_THOUSAND_SEPARATOR = False
            self.assertEqual(u'66666.666', localize(self.n))
            self.assertEqual(u'99999.999', localize(self.f))
            self.assertEqual(u'Dec. 31, 2009', localize(self.d))
            self.assertEqual(u'Dec. 31, 2009, 8:50 p.m.', localize(self.dt))

            settings.USE_THOUSAND_SEPARATOR = True
            self.assertEqual(u'66,666.666', Template('{{ n }}').render(self.ctxt))
            self.assertEqual(u'99,999.999', Template('{{ f }}').render(self.ctxt))

            settings.USE_THOUSAND_SEPARATOR = False
            self.assertEqual(u'66666.666', Template('{{ n }}').render(self.ctxt))
            self.assertEqual(u'99999.999', Template('{{ f }}').render(self.ctxt))
            self.assertEqual(u'Dec. 31, 2009', Template('{{ d }}').render(self.ctxt))
            self.assertEqual(u'Dec. 31, 2009, 8:50 p.m.', Template('{{ dt }}').render(self.ctxt))
            self.assertEqual(u'66666.67', Template('{{ n|floatformat:2 }}').render(self.ctxt))
            self.assertEqual(u'100000.0', Template('{{ f|floatformat }}').render(self.ctxt))
            self.assertEqual(u'12/31/2009', Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
            self.assertEqual(u'12/31/2009 8:50 p.m.', Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt))

            form5 = I18nForm({
                'decimal_field': u'66666.666',
                'float_field': u'99999.999',
                'date_field': u'12/31/2009',
                'datetime_field': u'12/31/2009 20:50',
                'time_field': u'20:50'
            })
            self.assertEqual(True, form5.is_valid())
            self.assertEqual(decimal.Decimal('66666.666'), form5.cleaned_data['decimal_field'])
            self.assertEqual(99999.999, form5.cleaned_data['float_field'])
            self.assertEqual(datetime.date(2009, 12, 31), form5.cleaned_data['date_field'])
            self.assertEqual(datetime.datetime(2009, 12, 31, 20, 50), form5.cleaned_data['datetime_field'])
            self.assertEqual(datetime.time(20, 50), form5.cleaned_data['time_field'])

            form6 = SelectDateForm({
                'date_field_month': u'12',
                'date_field_day': u'31',
                'date_field_year': u'2009'
            })
            self.assertEqual(True, form6.is_valid())
            self.assertEqual(datetime.date(2009, 12, 31), form6.cleaned_data['date_field'])
            self.assertEqual(
                u'<select name="mydate_month" id="id_mydate_month">\n<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12" selected="selected">December</option>\n</select>\n<select name="mydate_day" id="id_mydate_day">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31" selected="selected">31</option>\n</select>\n<select name="mydate_year" id="id_mydate_year">\n<option value="2009" selected="selected">2009</option>\n<option value="2010">2010</option>\n<option value="2011">2011</option>\n<option value="2012">2012</option>\n<option value="2013">2013</option>\n<option value="2014">2014</option>\n<option value="2015">2015</option>\n<option value="2016">2016</option>\n<option value="2017">2017</option>\n<option value="2018">2018</option>\n</select>',
                SelectDateWidget(years=range(2009, 2019)).render('mydate', datetime.date(2009, 12, 31))
            )
        finally:
            deactivate()
Ejemplo n.º 4
0
    def test_l10n_enabled(self):
        """
        Catalan locale
        """
        settings.USE_L10N = True
        activate("ca")
        try:
            self.assertEqual("j \de F \de Y", get_format("DATE_FORMAT"))
            self.assertEqual(1, get_format("FIRST_DAY_OF_WEEK"))
            self.assertEqual(",", get_format("DECIMAL_SEPARATOR"))
            self.assertEqual(u"10:15:48", time_format(self.t))
            self.assertEqual(u"31 de desembre de 2009", date_format(self.d))
            self.assertEqual(u"desembre del 2009", date_format(self.d, "YEAR_MONTH_FORMAT"))
            self.assertEqual(u"31/12/2009 20:50", date_format(self.dt, "SHORT_DATETIME_FORMAT"))
            self.assertEqual("No localizable", localize("No localizable"))

            settings.USE_THOUSAND_SEPARATOR = True
            self.assertEqual(u"66.666,666", localize(self.n))
            self.assertEqual(u"99.999,999", localize(self.f))

            settings.USE_THOUSAND_SEPARATOR = False
            self.assertEqual(u"66666,666", localize(self.n))
            self.assertEqual(u"99999,999", localize(self.f))
            self.assertEqual(u"31 de desembre de 2009", localize(self.d))
            self.assertEqual(u"31 de desembre de 2009 a les 20:50", localize(self.dt))

            settings.USE_THOUSAND_SEPARATOR = True
            self.assertEqual(u"66.666,666", Template("{{ n }}").render(self.ctxt))
            self.assertEqual(u"99.999,999", Template("{{ f }}").render(self.ctxt))

            form3 = I18nForm(
                {
                    "decimal_field": u"66.666,666",
                    "float_field": u"99.999,999",
                    "date_field": u"31/12/2009",
                    "datetime_field": u"31/12/2009 20:50",
                    "time_field": u"20:50",
                    "integer_field": u"1.234",
                }
            )
            self.assertEqual(True, form3.is_valid())
            self.assertEqual(decimal.Decimal("66666.666"), form3.cleaned_data["decimal_field"])
            self.assertEqual(99999.999, form3.cleaned_data["float_field"])
            self.assertEqual(datetime.date(2009, 12, 31), form3.cleaned_data["date_field"])
            self.assertEqual(datetime.datetime(2009, 12, 31, 20, 50), form3.cleaned_data["datetime_field"])
            self.assertEqual(datetime.time(20, 50), form3.cleaned_data["time_field"])
            self.assertEqual(1234, form3.cleaned_data["integer_field"])

            settings.USE_THOUSAND_SEPARATOR = False
            self.assertEqual(u"66666,666", Template("{{ n }}").render(self.ctxt))
            self.assertEqual(u"99999,999", Template("{{ f }}").render(self.ctxt))
            self.assertEqual(u"31 de desembre de 2009", Template("{{ d }}").render(self.ctxt))
            self.assertEqual(u"31 de desembre de 2009 a les 20:50", Template("{{ dt }}").render(self.ctxt))
            self.assertEqual(u"66666,67", Template("{{ n|floatformat:2 }}").render(self.ctxt))
            self.assertEqual(u"100000,0", Template("{{ f|floatformat }}").render(self.ctxt))
            self.assertEqual(u"10:15:48", Template('{{ t|time:"TIME_FORMAT" }}').render(self.ctxt))
            self.assertEqual(u"31/12/2009", Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
            self.assertEqual(u"31/12/2009 20:50", Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt))

            form4 = I18nForm(
                {
                    "decimal_field": u"66666,666",
                    "float_field": u"99999,999",
                    "date_field": u"31/12/2009",
                    "datetime_field": u"31/12/2009 20:50",
                    "time_field": u"20:50",
                    "integer_field": u"1234",
                }
            )
            self.assertEqual(True, form4.is_valid())
            self.assertEqual(decimal.Decimal("66666.666"), form4.cleaned_data["decimal_field"])
            self.assertEqual(99999.999, form4.cleaned_data["float_field"])
            self.assertEqual(datetime.date(2009, 12, 31), form4.cleaned_data["date_field"])
            self.assertEqual(datetime.datetime(2009, 12, 31, 20, 50), form4.cleaned_data["datetime_field"])
            self.assertEqual(datetime.time(20, 50), form4.cleaned_data["time_field"])
            self.assertEqual(1234, form4.cleaned_data["integer_field"])

            form5 = SelectDateForm({"date_field_month": u"12", "date_field_day": u"31", "date_field_year": u"2009"})
            self.assertEqual(True, form5.is_valid())
            self.assertEqual(datetime.date(2009, 12, 31), form5.cleaned_data["date_field"])
            self.assertEqual(
                u'<select name="mydate_day" id="id_mydate_day">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31" selected="selected">31</option>\n</select>\n<select name="mydate_month" id="id_mydate_month">\n<option value="1">gener</option>\n<option value="2">febrer</option>\n<option value="3">mar\xe7</option>\n<option value="4">abril</option>\n<option value="5">maig</option>\n<option value="6">juny</option>\n<option value="7">juliol</option>\n<option value="8">agost</option>\n<option value="9">setembre</option>\n<option value="10">octubre</option>\n<option value="11">novembre</option>\n<option value="12" selected="selected">desembre</option>\n</select>\n<select name="mydate_year" id="id_mydate_year">\n<option value="2009" selected="selected">2009</option>\n<option value="2010">2010</option>\n<option value="2011">2011</option>\n<option value="2012">2012</option>\n<option value="2013">2013</option>\n<option value="2014">2014</option>\n<option value="2015">2015</option>\n<option value="2016">2016</option>\n<option value="2017">2017</option>\n<option value="2018">2018</option>\n</select>',
                SelectDateWidget(years=range(2009, 2019)).render("mydate", datetime.date(2009, 12, 31)),
            )
        finally:
            deactivate()

        # English locale

        settings.USE_L10N = True
        activate("en")
        try:
            self.assertEqual("N j, Y", get_format("DATE_FORMAT"))
            self.assertEqual(0, get_format("FIRST_DAY_OF_WEEK"))
            self.assertEqual(".", get_format("DECIMAL_SEPARATOR"))
            self.assertEqual(u"Dec. 31, 2009", date_format(self.d))
            self.assertEqual(u"December 2009", date_format(self.d, "YEAR_MONTH_FORMAT"))
            self.assertEqual(u"12/31/2009 8:50 p.m.", date_format(self.dt, "SHORT_DATETIME_FORMAT"))
            self.assertEqual("No localizable", localize("No localizable"))

            settings.USE_THOUSAND_SEPARATOR = True
            self.assertEqual(u"66,666.666", localize(self.n))
            self.assertEqual(u"99,999.999", localize(self.f))

            settings.USE_THOUSAND_SEPARATOR = False
            self.assertEqual(u"66666.666", localize(self.n))
            self.assertEqual(u"99999.999", localize(self.f))
            self.assertEqual(u"Dec. 31, 2009", localize(self.d))
            self.assertEqual(u"Dec. 31, 2009, 8:50 p.m.", localize(self.dt))

            settings.USE_THOUSAND_SEPARATOR = True
            self.assertEqual(u"66,666.666", Template("{{ n }}").render(self.ctxt))
            self.assertEqual(u"99,999.999", Template("{{ f }}").render(self.ctxt))

            settings.USE_THOUSAND_SEPARATOR = False
            self.assertEqual(u"66666.666", Template("{{ n }}").render(self.ctxt))
            self.assertEqual(u"99999.999", Template("{{ f }}").render(self.ctxt))
            self.assertEqual(u"Dec. 31, 2009", Template("{{ d }}").render(self.ctxt))
            self.assertEqual(u"Dec. 31, 2009, 8:50 p.m.", Template("{{ dt }}").render(self.ctxt))
            self.assertEqual(u"66666.67", Template("{{ n|floatformat:2 }}").render(self.ctxt))
            self.assertEqual(u"100000.0", Template("{{ f|floatformat }}").render(self.ctxt))
            self.assertEqual(u"12/31/2009", Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
            self.assertEqual(
                u"12/31/2009 8:50 p.m.", Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt)
            )

            form5 = I18nForm(
                {
                    "decimal_field": u"66666.666",
                    "float_field": u"99999.999",
                    "date_field": u"12/31/2009",
                    "datetime_field": u"12/31/2009 20:50",
                    "time_field": u"20:50",
                    "integer_field": u"1234",
                }
            )
            self.assertEqual(True, form5.is_valid())
            self.assertEqual(decimal.Decimal("66666.666"), form5.cleaned_data["decimal_field"])
            self.assertEqual(99999.999, form5.cleaned_data["float_field"])
            self.assertEqual(datetime.date(2009, 12, 31), form5.cleaned_data["date_field"])
            self.assertEqual(datetime.datetime(2009, 12, 31, 20, 50), form5.cleaned_data["datetime_field"])
            self.assertEqual(datetime.time(20, 50), form5.cleaned_data["time_field"])
            self.assertEqual(1234, form5.cleaned_data["integer_field"])

            form6 = SelectDateForm({"date_field_month": u"12", "date_field_day": u"31", "date_field_year": u"2009"})
            self.assertEqual(True, form6.is_valid())
            self.assertEqual(datetime.date(2009, 12, 31), form6.cleaned_data["date_field"])
            self.assertEqual(
                u'<select name="mydate_month" id="id_mydate_month">\n<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12" selected="selected">December</option>\n</select>\n<select name="mydate_day" id="id_mydate_day">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31" selected="selected">31</option>\n</select>\n<select name="mydate_year" id="id_mydate_year">\n<option value="2009" selected="selected">2009</option>\n<option value="2010">2010</option>\n<option value="2011">2011</option>\n<option value="2012">2012</option>\n<option value="2013">2013</option>\n<option value="2014">2014</option>\n<option value="2015">2015</option>\n<option value="2016">2016</option>\n<option value="2017">2017</option>\n<option value="2018">2018</option>\n</select>',
                SelectDateWidget(years=range(2009, 2019)).render("mydate", datetime.date(2009, 12, 31)),
            )
        finally:
            deactivate()
Ejemplo n.º 5
0
    def test_l10n_enabled(self):
        settings.USE_L10N = True
        # Catalan locale
        activate('ca')
        try:
            self.assertEqual('j \de F \de Y', get_format('DATE_FORMAT'))
            self.assertEqual(1, get_format('FIRST_DAY_OF_WEEK'))
            self.assertEqual(',', get_format('DECIMAL_SEPARATOR'))
            self.assertEqual(u'10:15:48', time_format(self.t))
            self.assertEqual(u'31 de desembre de 2009', date_format(self.d))
            self.assertEqual(u'desembre del 2009',
                             date_format(self.d, 'YEAR_MONTH_FORMAT'))
            self.assertEqual(u'31/12/2009 20:50',
                             date_format(self.dt, 'SHORT_DATETIME_FORMAT'))
            self.assertEqual('No localizable', localize('No localizable'))

            settings.USE_THOUSAND_SEPARATOR = True
            self.assertEqual(u'66.666,666', localize(self.n))
            self.assertEqual(u'99.999,999', localize(self.f))
            self.assertEqual(u'10.000', localize(self.l))
            self.assertEqual(u'True', localize(True))

            settings.USE_THOUSAND_SEPARATOR = False
            self.assertEqual(u'66666,666', localize(self.n))
            self.assertEqual(u'99999,999', localize(self.f))
            self.assertEqual(u'10000', localize(self.l))
            self.assertEqual(u'31 de desembre de 2009', localize(self.d))
            self.assertEqual(u'31 de desembre de 2009 a les 20:50',
                             localize(self.dt))

            settings.USE_THOUSAND_SEPARATOR = True
            self.assertEqual(u'66.666,666',
                             Template('{{ n }}').render(self.ctxt))
            self.assertEqual(u'99.999,999',
                             Template('{{ f }}').render(self.ctxt))
            self.assertEqual(u'10.000', Template('{{ l }}').render(self.ctxt))

            form3 = I18nForm({
                'decimal_field': u'66.666,666',
                'float_field': u'99.999,999',
                'date_field': u'31/12/2009',
                'datetime_field': u'31/12/2009 20:50',
                'time_field': u'20:50',
                'integer_field': u'1.234',
            })
            self.assertEqual(True, form3.is_valid())
            self.assertEqual(decimal.Decimal('66666.666'),
                             form3.cleaned_data['decimal_field'])
            self.assertEqual(99999.999, form3.cleaned_data['float_field'])
            self.assertEqual(datetime.date(2009, 12, 31),
                             form3.cleaned_data['date_field'])
            self.assertEqual(datetime.datetime(2009, 12, 31, 20, 50),
                             form3.cleaned_data['datetime_field'])
            self.assertEqual(datetime.time(20, 50),
                             form3.cleaned_data['time_field'])
            self.assertEqual(1234, form3.cleaned_data['integer_field'])

            settings.USE_THOUSAND_SEPARATOR = False
            self.assertEqual(u'66666,666',
                             Template('{{ n }}').render(self.ctxt))
            self.assertEqual(u'99999,999',
                             Template('{{ f }}').render(self.ctxt))
            self.assertEqual(u'31 de desembre de 2009',
                             Template('{{ d }}').render(self.ctxt))
            self.assertEqual(u'31 de desembre de 2009 a les 20:50',
                             Template('{{ dt }}').render(self.ctxt))
            self.assertEqual(
                u'66666,67',
                Template('{{ n|floatformat:2 }}').render(self.ctxt))
            self.assertEqual(u'100000,0',
                             Template('{{ f|floatformat }}').render(self.ctxt))
            self.assertEqual(
                u'10:15:48',
                Template('{{ t|time:"TIME_FORMAT" }}').render(self.ctxt))
            self.assertEqual(
                u'31/12/2009',
                Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
            self.assertEqual(
                u'31/12/2009 20:50',
                Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(
                    self.ctxt))

            form4 = I18nForm({
                'decimal_field': u'66666,666',
                'float_field': u'99999,999',
                'date_field': u'31/12/2009',
                'datetime_field': u'31/12/2009 20:50',
                'time_field': u'20:50',
                'integer_field': u'1234',
            })
            self.assertEqual(True, form4.is_valid())
            self.assertEqual(decimal.Decimal('66666.666'),
                             form4.cleaned_data['decimal_field'])
            self.assertEqual(99999.999, form4.cleaned_data['float_field'])
            self.assertEqual(datetime.date(2009, 12, 31),
                             form4.cleaned_data['date_field'])
            self.assertEqual(datetime.datetime(2009, 12, 31, 20, 50),
                             form4.cleaned_data['datetime_field'])
            self.assertEqual(datetime.time(20, 50),
                             form4.cleaned_data['time_field'])
            self.assertEqual(1234, form4.cleaned_data['integer_field'])

            form5 = SelectDateForm({
                'date_field_month': u'12',
                'date_field_day': u'31',
                'date_field_year': u'2009'
            })
            self.assertEqual(True, form5.is_valid())
            self.assertEqual(datetime.date(2009, 12, 31),
                             form5.cleaned_data['date_field'])
            self.assertEqual(
                u'<select name="mydate_day" id="id_mydate_day">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31" selected="selected">31</option>\n</select>\n<select name="mydate_month" id="id_mydate_month">\n<option value="1">gener</option>\n<option value="2">febrer</option>\n<option value="3">mar\xe7</option>\n<option value="4">abril</option>\n<option value="5">maig</option>\n<option value="6">juny</option>\n<option value="7">juliol</option>\n<option value="8">agost</option>\n<option value="9">setembre</option>\n<option value="10">octubre</option>\n<option value="11">novembre</option>\n<option value="12" selected="selected">desembre</option>\n</select>\n<select name="mydate_year" id="id_mydate_year">\n<option value="2009" selected="selected">2009</option>\n<option value="2010">2010</option>\n<option value="2011">2011</option>\n<option value="2012">2012</option>\n<option value="2013">2013</option>\n<option value="2014">2014</option>\n<option value="2015">2015</option>\n<option value="2016">2016</option>\n<option value="2017">2017</option>\n<option value="2018">2018</option>\n</select>',
                SelectDateWidget(years=range(2009, 2019)).render(
                    'mydate', datetime.date(2009, 12, 31)))
        finally:
            deactivate()

        # Russian locale (with E as month)
        activate('ru')
        try:
            self.assertEqual(
                u'<select name="mydate_day" id="id_mydate_day">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31" selected="selected">31</option>\n</select>\n<select name="mydate_month" id="id_mydate_month">\n<option value="1">\u042f\u043d\u0432\u0430\u0440\u044c</option>\n<option value="2">\u0424\u0435\u0432\u0440\u0430\u043b\u044c</option>\n<option value="3">\u041c\u0430\u0440\u0442</option>\n<option value="4">\u0410\u043f\u0440\u0435\u043b\u044c</option>\n<option value="5">\u041c\u0430\u0439</option>\n<option value="6">\u0418\u044e\u043d\u044c</option>\n<option value="7">\u0418\u044e\u043b\u044c</option>\n<option value="8">\u0410\u0432\u0433\u0443\u0441\u0442</option>\n<option value="9">\u0421\u0435\u043d\u0442\u044f\u0431\u0440\u044c</option>\n<option value="10">\u041e\u043a\u0442\u044f\u0431\u0440\u044c</option>\n<option value="11">\u041d\u043e\u044f\u0431\u0440\u044c</option>\n<option value="12" selected="selected">\u0414\u0435\u043a\u0430\u0431\u0440\u044c</option>\n</select>\n<select name="mydate_year" id="id_mydate_year">\n<option value="2009" selected="selected">2009</option>\n<option value="2010">2010</option>\n<option value="2011">2011</option>\n<option value="2012">2012</option>\n<option value="2013">2013</option>\n<option value="2014">2014</option>\n<option value="2015">2015</option>\n<option value="2016">2016</option>\n<option value="2017">2017</option>\n<option value="2018">2018</option>\n</select>',
                SelectDateWidget(years=range(2009, 2019)).render(
                    'mydate', datetime.date(2009, 12, 31)))
        finally:
            deactivate()

        # English locale
        activate('en')
        try:
            self.assertEqual('N j, Y', get_format('DATE_FORMAT'))
            self.assertEqual(0, get_format('FIRST_DAY_OF_WEEK'))
            self.assertEqual('.', get_format('DECIMAL_SEPARATOR'))
            self.assertEqual(u'Dec. 31, 2009', date_format(self.d))
            self.assertEqual(u'December 2009',
                             date_format(self.d, 'YEAR_MONTH_FORMAT'))
            self.assertEqual(u'12/31/2009 8:50 p.m.',
                             date_format(self.dt, 'SHORT_DATETIME_FORMAT'))
            self.assertEqual(u'No localizable', localize('No localizable'))

            settings.USE_THOUSAND_SEPARATOR = True
            self.assertEqual(u'66,666.666', localize(self.n))
            self.assertEqual(u'99,999.999', localize(self.f))
            self.assertEqual(u'10,000', localize(self.l))

            settings.USE_THOUSAND_SEPARATOR = False
            self.assertEqual(u'66666.666', localize(self.n))
            self.assertEqual(u'99999.999', localize(self.f))
            self.assertEqual(u'10000', localize(self.l))
            self.assertEqual(u'Dec. 31, 2009', localize(self.d))
            self.assertEqual(u'Dec. 31, 2009, 8:50 p.m.', localize(self.dt))

            settings.USE_THOUSAND_SEPARATOR = True
            self.assertEqual(u'66,666.666',
                             Template('{{ n }}').render(self.ctxt))
            self.assertEqual(u'99,999.999',
                             Template('{{ f }}').render(self.ctxt))
            self.assertEqual(u'10,000', Template('{{ l }}').render(self.ctxt))

            settings.USE_THOUSAND_SEPARATOR = False
            self.assertEqual(u'66666.666',
                             Template('{{ n }}').render(self.ctxt))
            self.assertEqual(u'99999.999',
                             Template('{{ f }}').render(self.ctxt))
            self.assertEqual(u'Dec. 31, 2009',
                             Template('{{ d }}').render(self.ctxt))
            self.assertEqual(u'Dec. 31, 2009, 8:50 p.m.',
                             Template('{{ dt }}').render(self.ctxt))
            self.assertEqual(
                u'66666.67',
                Template('{{ n|floatformat:2 }}').render(self.ctxt))
            self.assertEqual(u'100000.0',
                             Template('{{ f|floatformat }}').render(self.ctxt))
            self.assertEqual(
                u'12/31/2009',
                Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
            self.assertEqual(
                u'12/31/2009 8:50 p.m.',
                Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(
                    self.ctxt))

            form5 = I18nForm({
                'decimal_field': u'66666.666',
                'float_field': u'99999.999',
                'date_field': u'12/31/2009',
                'datetime_field': u'12/31/2009 20:50',
                'time_field': u'20:50',
                'integer_field': u'1234',
            })
            self.assertEqual(True, form5.is_valid())
            self.assertEqual(decimal.Decimal('66666.666'),
                             form5.cleaned_data['decimal_field'])
            self.assertEqual(99999.999, form5.cleaned_data['float_field'])
            self.assertEqual(datetime.date(2009, 12, 31),
                             form5.cleaned_data['date_field'])
            self.assertEqual(datetime.datetime(2009, 12, 31, 20, 50),
                             form5.cleaned_data['datetime_field'])
            self.assertEqual(datetime.time(20, 50),
                             form5.cleaned_data['time_field'])
            self.assertEqual(1234, form5.cleaned_data['integer_field'])

            form6 = SelectDateForm({
                'date_field_month': u'12',
                'date_field_day': u'31',
                'date_field_year': u'2009'
            })
            self.assertEqual(True, form6.is_valid())
            self.assertEqual(datetime.date(2009, 12, 31),
                             form6.cleaned_data['date_field'])
            self.assertEqual(
                u'<select name="mydate_month" id="id_mydate_month">\n<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12" selected="selected">December</option>\n</select>\n<select name="mydate_day" id="id_mydate_day">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31" selected="selected">31</option>\n</select>\n<select name="mydate_year" id="id_mydate_year">\n<option value="2009" selected="selected">2009</option>\n<option value="2010">2010</option>\n<option value="2011">2011</option>\n<option value="2012">2012</option>\n<option value="2013">2013</option>\n<option value="2014">2014</option>\n<option value="2015">2015</option>\n<option value="2016">2016</option>\n<option value="2017">2017</option>\n<option value="2018">2018</option>\n</select>',
                SelectDateWidget(years=range(2009, 2019)).render(
                    'mydate', datetime.date(2009, 12, 31)))
        finally:
            deactivate()
Ejemplo n.º 6
0
    def test_l10n_disabled(self):
        """
        Catalan locale with format i18n disabled translations will be used,
        but not formats
        """
        settings.USE_L10N = False
        activate('ca')
        try:
            self.assertEqual(u'N j, Y', get_format('DATE_FORMAT'))
            self.assertEqual(0, get_format('FIRST_DAY_OF_WEEK'))
            self.assertEqual(u'.', get_format('DECIMAL_SEPARATOR'))
            self.assertEqual(u'10:15 a.m.', time_format(self.t))
            self.assertEqual(u'des. 31, 2009', date_format(self.d))
            self.assertEqual(u'desembre 2009',
                             date_format(self.d, 'YEAR_MONTH_FORMAT'))
            self.assertEqual(u'12/31/2009 8:50 p.m.',
                             date_format(self.dt, 'SHORT_DATETIME_FORMAT'))
            self.assertEqual(u'No localizable', localize('No localizable'))
            self.assertEqual(u'66666.666', localize(self.n))
            self.assertEqual(u'99999.999', localize(self.f))
            self.assertEqual(u'10000', localize(self.l))
            self.assertEqual(u'des. 31, 2009', localize(self.d))
            self.assertEqual(u'des. 31, 2009, 8:50 p.m.', localize(self.dt))
            self.assertEqual(u'66666.666',
                             Template('{{ n }}').render(self.ctxt))
            self.assertEqual(u'99999.999',
                             Template('{{ f }}').render(self.ctxt))
            self.assertEqual(u'des. 31, 2009',
                             Template('{{ d }}').render(self.ctxt))
            self.assertEqual(u'des. 31, 2009, 8:50 p.m.',
                             Template('{{ dt }}').render(self.ctxt))
            self.assertEqual(
                u'66666.67',
                Template('{{ n|floatformat:2 }}').render(self.ctxt))
            self.assertEqual(u'100000.0',
                             Template('{{ f|floatformat }}').render(self.ctxt))
            self.assertEqual(
                u'10:15 a.m.',
                Template('{{ t|time:"TIME_FORMAT" }}').render(self.ctxt))
            self.assertEqual(
                u'12/31/2009',
                Template('{{ d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
            self.assertEqual(
                u'12/31/2009 8:50 p.m.',
                Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(
                    self.ctxt))

            form = I18nForm({
                'decimal_field': u'66666,666',
                'float_field': u'99999,999',
                'date_field': u'31/12/2009',
                'datetime_field': u'31/12/2009 20:50',
                'time_field': u'20:50',
                'integer_field': u'1.234',
            })
            self.assertEqual(False, form.is_valid())
            self.assertEqual([u'Introdu\xefu un n\xfamero.'],
                             form.errors['float_field'])
            self.assertEqual([u'Introdu\xefu un n\xfamero.'],
                             form.errors['decimal_field'])
            self.assertEqual([u'Introdu\xefu una data v\xe0lida.'],
                             form.errors['date_field'])
            self.assertEqual([u'Introdu\xefu una data/hora v\xe0lides.'],
                             form.errors['datetime_field'])
            self.assertEqual([u'Introdu\xefu un n\xfamero sencer.'],
                             form.errors['integer_field'])

            form2 = SelectDateForm({
                'date_field_month': u'12',
                'date_field_day': u'31',
                'date_field_year': u'2009'
            })
            self.assertEqual(True, form2.is_valid())
            self.assertEqual(datetime.date(2009, 12, 31),
                             form2.cleaned_data['date_field'])
            self.assertEqual(
                u'<select name="mydate_month" id="id_mydate_month">\n<option value="1">gener</option>\n<option value="2">febrer</option>\n<option value="3">mar\xe7</option>\n<option value="4">abril</option>\n<option value="5">maig</option>\n<option value="6">juny</option>\n<option value="7">juliol</option>\n<option value="8">agost</option>\n<option value="9">setembre</option>\n<option value="10">octubre</option>\n<option value="11">novembre</option>\n<option value="12" selected="selected">desembre</option>\n</select>\n<select name="mydate_day" id="id_mydate_day">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31" selected="selected">31</option>\n</select>\n<select name="mydate_year" id="id_mydate_year">\n<option value="2009" selected="selected">2009</option>\n<option value="2010">2010</option>\n<option value="2011">2011</option>\n<option value="2012">2012</option>\n<option value="2013">2013</option>\n<option value="2014">2014</option>\n<option value="2015">2015</option>\n<option value="2016">2016</option>\n<option value="2017">2017</option>\n<option value="2018">2018</option>\n</select>',
                SelectDateWidget(years=range(2009, 2019)).render(
                    'mydate', datetime.date(2009, 12, 31)))

            # We shouldn't change the behavior of the floatformat filter re:
            # thousand separator and grouping when USE_L10N is False even
            # if the USE_THOUSAND_SEPARATOR, NUMBER_GROUPING and
            # THOUSAND_SEPARATOR settings are specified
            settings.USE_THOUSAND_SEPARATOR = True
            settings.NUMBER_GROUPING = 1
            settings.THOUSAND_SEPARATOR = '!'
            self.assertEqual(
                u'66666.67',
                Template('{{ n|floatformat:2 }}').render(self.ctxt))
            self.assertEqual(u'100000.0',
                             Template('{{ f|floatformat }}').render(self.ctxt))
        finally:
            deactivate()