|
| 1 | +import sys |
| 2 | + |
| 3 | +import locale |
1 | 4 | import os |
2 | 5 | import base64 |
3 | 6 | import gettext |
|
6 | 9 | from functools import partial |
7 | 10 |
|
8 | 11 | from test import support |
9 | | -from test.support import os_helper |
10 | | - |
| 12 | +from test.support import os_helper, run_with_locale |
11 | 13 |
|
12 | 14 | # TODO: |
13 | 15 | # - Add new tests, for example for "dgettext" |
@@ -736,32 +738,63 @@ def create_mo_file(self, lang): |
736 | 738 | f.write(GNU_MO_DATA) |
737 | 739 | return mo_file |
738 | 740 |
|
739 | | - def _for_all_vars(self, mo_file, locale): |
| 741 | + def _for_all_vars(self, mo_file, locale, expected=True): |
740 | 742 | for var in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): |
741 | 743 | self.env.set(var, locale) |
742 | 744 | result = gettext.find("mofile", |
743 | 745 | localedir=os.path.join(self.tempdir, "locale")) |
744 | | - self.assertEqual(mo_file, result) |
| 746 | + if expected: |
| 747 | + self.assertEqual(mo_file, result) |
| 748 | + else: |
| 749 | + self.assertIsNone(result) |
745 | 750 | self.env.unset(var) |
746 | 751 |
|
747 | | - @unittest.mock.patch("locale.setlocale", return_value=(None, None)) |
| 752 | + @unittest.mock.patch("locale.setlocale", return_value='') |
748 | 753 | def test_find_with_env_vars(self, patch_getlocale): |
749 | 754 | # test that find correctly finds the environment variables |
750 | 755 | # when languages are not supplied |
| 756 | + mo_file = self.create_mo_file("ca_ES") |
| 757 | + self._for_all_vars(mo_file, "ca_ES") |
| 758 | + self._for_all_vars(mo_file, "ca_ES.UTF-8") |
| 759 | + self._for_all_vars(mo_file, "ca_ES.UTF-8.mo") |
| 760 | + self._for_all_vars(mo_file, "es_ES:ca_ES:fr_FR") |
| 761 | + self._for_all_vars(mo_file, "ca_ES@euro") |
| 762 | + self._for_all_vars(mo_file, "ca_ES.UTF-8@euro") |
| 763 | + self._for_all_vars(mo_file, "ca_ES@valencia") |
| 764 | + self._for_all_vars(mo_file, "C", expected=False) |
| 765 | + self._for_all_vars(mo_file, "C.UTF-8", expected=False) |
| 766 | + |
| 767 | + @unittest.mock.patch('gettext._expand_lang') |
| 768 | + def test_encoding_not_ignored(self, patch_expand_lang): |
| 769 | + self.env.set('LANGUAGE', 'ga_IE.UTF-8') |
| 770 | + gettext.find("mofile") |
| 771 | + patch_expand_lang.assert_any_call('ga_IE.UTF-8') |
| 772 | + self.env.unset('LANGUAGE') |
| 773 | + |
| 774 | + def test_find_LANGUAGE_priority(self): |
| 775 | + orig = locale.setlocale(locale.LC_MESSAGES) |
| 776 | + self.addCleanup(lambda: locale.setlocale(locale.LC_MESSAGES, orig)) |
| 777 | + self.env.set('LANGUAGE', 'ga_IE') |
| 778 | + self.env.set('LC_ALL', 'pt_BR') |
| 779 | + locale.setlocale(locale.LC_MESSAGES, 'pt_BR') |
751 | 780 | mo_file = self.create_mo_file("ga_IE") |
752 | | - self._for_all_vars(mo_file, "ga_IE") |
753 | | - self._for_all_vars(mo_file, "ga_IE.UTF-8") |
754 | | - self._for_all_vars(mo_file, "es_ES:ga_IE:fr_FR") |
755 | | - self._for_all_vars(mo_file, "ga_IE@euro") |
| 781 | + |
| 782 | + result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale")) |
| 783 | + self.assertEqual(result, mo_file) |
| 784 | + locale.setlocale(locale.LC_MESSAGES, orig) |
756 | 785 |
|
757 | 786 | def test_process_vars_override(self): |
758 | | - mo_file = self.create_mo_file("ga_IE") |
759 | | - with unittest.mock.patch("locale.setlocale", return_value=('ga_IE', 'UTF-8')): |
| 787 | + orig = locale.setlocale(locale.LC_MESSAGES) |
| 788 | + self.addCleanup(lambda: locale.setlocale(locale.LC_MESSAGES, orig)) |
| 789 | + mo_file = self.create_mo_file("ca_ES") |
| 790 | + for loc in ("ca_ES", "ca_ES.UTF-8", "ca_ES@euro", "ca_ES@valencia"): |
| 791 | + locale.setlocale(locale.LC_MESSAGES, loc) |
760 | 792 | result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale")) |
761 | 793 | self.assertEqual(mo_file, result) |
762 | | - with unittest.mock.patch("locale.setlocale", return_value=('ga_IE', None)): |
| 794 | + for loc in ("C", "C.UTF-8"): |
| 795 | + locale.setlocale(locale.LC_MESSAGES, loc) |
763 | 796 | result = gettext.find("mofile", localedir=os.path.join(self.tempdir, "locale")) |
764 | | - self.assertEqual(mo_file, result) |
| 797 | + self.assertIsNone(result) |
765 | 798 |
|
766 | 799 | def test_find_with_languages(self): |
767 | 800 | # test that passed languages are used |
|
0 commit comments