Debian Codegraph @

tests/messages/test_plurals.py

raw
1 #
2 # Copyright (C) 2007-2011 Edgewall Software, 2013-2025 the Babel team
3 # All rights reserved.
4 #
5 # This software is licensed as described in the file LICENSE, which
6 # you should have received as part of this distribution. The terms
7 # are also available at https://github.com/python-babel/babel/blob/master/LICENSE.
8 #
9 # This software consists of voluntary contributions made by many
10 # individuals. For the exact contribution history, see the revision
11 # history and logs, available at https://github.com/python-babel/babel/commits/master/.
12 import pytest
13
14 from babel import Locale
15 from babel.messages import plurals
16
17
18 @pytest.mark.parametrize(('locale', 'num_plurals', 'plural_expr'), [
19 (Locale('en'), 2, '(n != 1)'),
20 (Locale('en', 'US'), 2, '(n != 1)'),
21 (Locale('si'), 2, '(n > 1)'),
22 (Locale('zh'), 1, '0'),
23 (Locale('zh', script='Hans'), 1, '0'),
24 (Locale('zh', script='Hant'), 1, '0'),
25 (Locale('zh', 'CN', 'Hans'), 1, '0'),
26 (Locale('zh', 'TW', 'Hant'), 1, '0'),
27 ])
28 def test_get_plural_selection(locale, num_plurals, plural_expr):
29 assert plurals.get_plural(locale) == (num_plurals, plural_expr)
30
31
32 def test_get_plural_accepts_strings():
33 assert plurals.get_plural(locale='ga') == (5, '(n == 1 ? 0 : n == 2 ? 1 : n >= 3 && n <= 6 ? 2 : n >= 7 && n <= 10 ? 3 : 4)')
34
35
36 def test_get_plural_falls_back_to_default():
37 assert plurals.get_plural('ba') == (2, '(n != 1)')
38
39
40 def test_get_plural():
41 # See https://localization-guide.readthedocs.io/en/latest/l10n/pluralforms.html for more details.
42 assert plurals.get_plural(locale='en') == (2, '(n != 1)')
43 assert plurals.get_plural(locale='ga') == (5, '(n == 1 ? 0 : n == 2 ? 1 : n >= 3 && n <= 6 ? 2 : n >= 7 && n <= 10 ? 3 : 4)')
44
45 plural_ja = plurals.get_plural("ja")
46 assert str(plural_ja) == 'nplurals=1; plural=0;'
47 assert plural_ja.num_plurals == 1
48 assert plural_ja.plural_expr == '0'
49 assert plural_ja.plural_forms == 'nplurals=1; plural=0;'
50
51 plural_en_US = plurals.get_plural('en_US')
52 assert str(plural_en_US) == plural_en_US.plural_forms == 'nplurals=2; plural=(n != 1);'
53 assert plural_en_US.num_plurals == 2
54 assert plural_en_US.plural_expr == '(n != 1)'
55
56 plural_fr_FR = plurals.get_plural('fr_FR')
57 assert str(plural_fr_FR) == plural_fr_FR.plural_forms == 'nplurals=3; plural=(n == 0 || n == 1) ? 0 : n != 0 && n % 1000000 == 0 ? 1 : 2;'
58 assert plural_fr_FR.num_plurals == 3
59 assert plural_fr_FR.plural_expr == '(n == 0 || n == 1) ? 0 : n != 0 && n % 1000000 == 0 ? 1 : 2'
60
61 plural_pl_PL = plurals.get_plural('pl_PL')
62 assert str(plural_pl_PL) == 'nplurals=3; plural=n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 12 || n % 100 > 14) ? 1 : 2;'
63 assert plural_pl_PL.num_plurals == 3
64 assert plural_pl_PL.plural_expr == 'n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 12 || n % 100 > 14) ? 1 : 2'