| 746 | |
| 747 | |
| 748 | class MultipleLocaleActivationTests(TestCase): |
| 749 | """ |
| 750 | Tests for template rendering behavior when multiple locales are activated |
| 751 | during the lifetime of the same process. |
| 752 | """ |
| 753 | def setUp(self): |
| 754 | self._old_language = get_language() |
| 755 | |
| 756 | def tearDown(self): |
| 757 | activate(self._old_language) |
| 758 | |
| 759 | def test_single_locale_activation(self): |
| 760 | """ |
| 761 | Simple baseline behavior with one locale for all the supported i18n constructs. |
| 762 | """ |
| 763 | activate('fr') |
| 764 | self.assertEqual(Template("{{ _('Yes') }}").render(Context({})), 'Oui') |
| 765 | self.assertEqual(Template("{% load i18n %}{% trans 'Yes' %}").render(Context({})), 'Oui') |
| 766 | self.assertEqual(Template("{% load i18n %}{% blocktrans %}Yes{% endblocktrans %}").render(Context({})), 'Oui') |
| 767 | |
| 768 | # Literal marked up with _() in a filter expression |
| 769 | |
| 770 | def test_multiple_locale_filter(self): |
| 771 | activate('de') |
| 772 | t = Template("{% load i18n %}{{ 0|yesno:_('yes,no,maybe') }}") |
| 773 | activate(self._old_language) |
| 774 | activate('nl') |
| 775 | self.assertEqual(t.render(Context({})), 'nee') |
| 776 | |
| 777 | def test_multiple_locale_filter_deactivate(self): |
| 778 | activate('de') |
| 779 | t = Template("{% load i18n %}{{ 0|yesno:_('yes,no,maybe') }}") |
| 780 | deactivate() |
| 781 | activate('nl') |
| 782 | self.assertEqual(t.render(Context({})), 'nee') |
| 783 | |
| 784 | def test_multiple_locale_filter_direct_switch(self): |
| 785 | activate('de') |
| 786 | t = Template("{% load i18n %}{{ 0|yesno:_('yes,no,maybe') }}") |
| 787 | activate('nl') |
| 788 | self.assertEqual(t.render(Context({})), 'nee') |
| 789 | |
| 790 | # Literal marked up with _() |
| 791 | |
| 792 | def test_multiple_locale(self): |
| 793 | activate('de') |
| 794 | t = Template("{{ _('No') }}") |
| 795 | activate(self._old_language) |
| 796 | activate('nl') |
| 797 | self.assertEqual(t.render(Context({})), 'Nee') |
| 798 | |
| 799 | def test_multiple_locale_deactivate(self): |
| 800 | activate('de') |
| 801 | t = Template("{{ _('No') }}") |
| 802 | deactivate() |
| 803 | activate('nl') |
| 804 | self.assertEqual(t.render(Context({})), 'Nee') |
| 805 | |
| 806 | def test_multiple_locale_direct_switch(self): |
| 807 | activate('de') |
| 808 | t = Template("{{ _('No') }}") |
| 809 | activate('nl') |
| 810 | self.assertEqual(t.render(Context({})), 'Nee') |
| 811 | |
| 812 | # Literal marked up with _(), loading the i18n template tag library |
| 813 | |
| 814 | def test_multiple_locale_loadi18n(self): |
| 815 | activate('de') |
| 816 | t = Template("{% load i18n %}{{ _('No') }}") |
| 817 | activate(self._old_language) |
| 818 | activate('nl') |
| 819 | self.assertEqual(t.render(Context({})), 'Nee') |
| 820 | |
| 821 | def test_multiple_locale_loadi18n_deactivate(self): |
| 822 | activate('de') |
| 823 | t = Template("{% load i18n %}{{ _('No') }}") |
| 824 | deactivate() |
| 825 | activate('nl') |
| 826 | self.assertEqual(t.render(Context({})), 'Nee') |
| 827 | |
| 828 | def test_multiple_locale_loadi18n_direct_switch(self): |
| 829 | activate('de') |
| 830 | t = Template("{% load i18n %}{{ _('No') }}") |
| 831 | activate('nl') |
| 832 | self.assertEqual(t.render(Context({})), 'Nee') |
| 833 | |
| 834 | # trans i18n tag |
| 835 | |
| 836 | def test_multiple_locale_trans(self): |
| 837 | activate('de') |
| 838 | t = Template("{% load i18n %}{% trans 'No' %}") |
| 839 | activate(self._old_language) |
| 840 | activate('nl') |
| 841 | self.assertEqual(t.render(Context({})), 'Nee') |
| 842 | |
| 843 | def test_multiple_locale_deactivate_trans(self): |
| 844 | activate('de') |
| 845 | t = Template("{% load i18n %}{% trans 'No' %}") |
| 846 | deactivate() |
| 847 | activate('nl') |
| 848 | self.assertEqual(t.render(Context({})), 'Nee') |
| 849 | |
| 850 | def test_multiple_locale_direct_switch_trans(self): |
| 851 | activate('de') |
| 852 | t = Template("{% load i18n %}{% trans 'No' %}") |
| 853 | activate('nl') |
| 854 | self.assertEqual(t.render(Context({})), 'Nee') |
| 855 | |
| 856 | # blocktrans i18n tag |
| 857 | |
| 858 | def test_multiple_locale_btrans(self): |
| 859 | activate('de') |
| 860 | t = Template("{% load i18n %}{% blocktrans %}No{% endblocktrans %}") |
| 861 | activate(self._old_language) |
| 862 | activate('nl') |
| 863 | self.assertEqual(t.render(Context({})), 'Nee') |
| 864 | |
| 865 | def test_multiple_locale_deactivate_btrans(self): |
| 866 | activate('de') |
| 867 | t = Template("{% load i18n %}{% blocktrans %}No{% endblocktrans %}") |
| 868 | deactivate() |
| 869 | activate('nl') |
| 870 | self.assertEqual(t.render(Context({})), 'Nee') |
| 871 | |
| 872 | def test_multiple_locale_direct_switch_btrans(self): |
| 873 | activate('de') |
| 874 | t = Template("{% load i18n %}{% blocktrans %}No{% endblocktrans %}") |
| 875 | activate('nl') |
| 876 | self.assertEqual(t.render(Context({})), 'Nee') |