-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathtest_util_i18n.py
More file actions
212 lines (169 loc) · 8.16 KB
/
test_util_i18n.py
File metadata and controls
212 lines (169 loc) · 8.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""Test i18n util."""
from __future__ import annotations
import datetime
import os
import time
from pathlib import Path
from typing import TYPE_CHECKING
import pytest
from babel.messages.mofile import read_mo
from sphinx.errors import SphinxError
from sphinx.util import i18n
if TYPE_CHECKING:
from sphinx.testing.util import SphinxTestApp
def test_catalog_info_for_file_and_path() -> None:
cat = i18n.CatalogInfo('path', 'domain', 'utf-8')
assert cat.po_file == 'domain.po'
assert cat.mo_file == 'domain.mo'
assert cat.po_path == Path('path', 'domain.po')
assert cat.mo_path == Path('path', 'domain.mo')
def test_catalog_info_for_sub_domain_file_and_path() -> None:
cat = i18n.CatalogInfo('path', 'sub/domain', 'utf-8')
assert cat.po_file == 'sub/domain.po'
assert cat.mo_file == 'sub/domain.mo'
assert cat.po_path == Path('path', 'sub', 'domain.po')
assert cat.mo_path == Path('path', 'sub', 'domain.mo')
def test_catalog_outdated(tmp_path: Path) -> None:
(tmp_path / 'test.po').write_text('#', encoding='utf8')
cat = i18n.CatalogInfo(tmp_path, 'test', 'utf-8')
assert cat.is_outdated() # if mo is not exist
mo_file = tmp_path / 'test.mo'
mo_file.write_text('#', encoding='utf8')
assert not cat.is_outdated() # if mo is exist and newer than po
new_mtime = mo_file.stat().st_mtime_ns - 10_000_000_000
os.utime(mo_file, ns=(new_mtime, new_mtime)) # to be outdated
assert cat.is_outdated() # if mo is exist and older than po
def test_catalog_write_mo(tmp_path: Path) -> None:
(tmp_path / 'test.po').write_text('#', encoding='utf8')
cat = i18n.CatalogInfo(tmp_path, 'test', 'utf-8')
cat.write_mo('en')
mo_path = Path(cat.mo_path)
assert mo_path.exists()
with open(mo_path, 'rb') as f:
assert read_mo(f) is not None
def test_format_date() -> None:
date = datetime.datetime(2016, 2, 7, 5, 11, 17, 0) # NoQA: DTZ001
# strftime format
format = '%B %d, %Y'
assert i18n.format_date(format, date=date, language='') == 'February 07, 2016'
formatted_unknown = i18n.format_date(format, date=date, language='unknown')
assert formatted_unknown == 'February 07, 2016'
assert i18n.format_date(format, date=date, language='en') == 'February 07, 2016'
assert i18n.format_date(format, date=date, language='ja') == '2月 07, 2016'
assert i18n.format_date(format, date=date, language='de') == 'Februar 07, 2016'
# raw string
format = 'Mon Mar 28 12:37:08 2016, commit 4367aef'
assert i18n.format_date(format, date=date, language='en') == format
format = '%B %d, %Y, %H:%M:%S %I %p'
formatted = i18n.format_date(format, date=date, language='en')
assert formatted == 'February 07, 2016, 05:11:17 05 AM'
format = '%B %-d, %Y, %-H:%-M:%-S %-I %p'
formatted = i18n.format_date(format, date=date, language='en')
assert formatted == 'February 7, 2016, 5:11:17 5 AM'
format = '%x'
assert i18n.format_date(format, date=date, language='en') == 'Feb 7, 2016'
format = '%X'
assert i18n.format_date(format, date=date, language='en') == '5:11:17\u202fAM'
format = '%c'
formatted = i18n.format_date(format, date=date, language='en')
assert formatted == 'Feb 7, 2016, 5:11:17\u202fAM'
# timezone
format = '%Z'
assert i18n.format_date(format, date=date, language='en') == 'UTC'
format = '%z'
assert i18n.format_date(format, date=date, language='en') == '+0000'
def test_format_date_timezone() -> None:
dt = datetime.datetime(2016, 8, 7, 5, 11, 17, 0, tzinfo=datetime.UTC)
if time.localtime(dt.timestamp()).tm_gmtoff == 0:
raise pytest.skip('Local time zone is GMT') # NoQA: EM101,TRY003
fmt = '%Y-%m-%d %H:%M:%S'
iso_gmt = dt.isoformat(' ').split('+')[0]
fd_gmt = i18n.format_date(fmt, date=dt, language='en', local_time=False)
assert fd_gmt == '2016-08-07 05:11:17'
assert fd_gmt == iso_gmt
iso_local = dt.astimezone().isoformat(' ')[:19] # strip the timezone
fd_local = i18n.format_date(fmt, date=dt, language='en', local_time=True)
assert fd_local == iso_local
assert fd_local != fd_gmt
@pytest.mark.sphinx('html', testroot='root')
def test_get_filename_for_language(app: SphinxTestApp) -> None:
get_filename = i18n.get_image_filename_for_language
app.env.current_document.docname = 'index'
# language is en
app.config.language = 'en'
assert get_filename('foo.png', app.env) == 'foo.en.png'
assert get_filename('foo.bar.png', app.env) == 'foo.bar.en.png'
assert get_filename('dir/foo.png', app.env) == 'dir/foo.en.png'
assert get_filename('../foo.png', app.env) == '../foo.en.png'
assert get_filename('foo', app.env) == 'foo.en'
# modify figure_language_filename and language is 'en'
app.config.language = 'en'
app.config.figure_language_filename = 'images/{language}/{root}{ext}'
assert get_filename('foo.png', app.env) == 'images/en/foo.png'
assert get_filename('foo.bar.png', app.env) == 'images/en/foo.bar.png'
assert get_filename('subdir/foo.png', app.env) == 'images/en/subdir/foo.png'
assert get_filename('../foo.png', app.env) == 'images/en/../foo.png'
assert get_filename('foo', app.env) == 'images/en/foo'
# new path and basename tokens
app.config.language = 'en'
app.config.figure_language_filename = '{path}{language}/{basename}{ext}'
assert get_filename('foo.png', app.env) == 'en/foo.png'
assert get_filename('foo.bar.png', app.env) == 'en/foo.bar.png'
assert get_filename('subdir/foo.png', app.env) == 'subdir/en/foo.png'
assert get_filename('../foo.png', app.env) == '../en/foo.png'
assert get_filename('foo', app.env) == 'en/foo'
# invalid figure_language_filename
app.config.figure_language_filename = '{root}.{invalid}{ext}'
with pytest.raises(SphinxError):
get_filename('foo.png', app.env)
# docpath (for a document in the top of source directory)
app.config.language = 'en'
app.config.figure_language_filename = '/{docpath}{language}/{basename}{ext}'
assert get_filename('foo.png', app.env) == '/en/foo.png'
# docpath (for a document in the sub directory)
app.env.current_document.docname = 'subdir/index'
assert get_filename('foo.png', app.env) == '/subdir/en/foo.png'
def test_CatalogRepository(tmp_path: Path) -> None:
for po_file in (
(tmp_path / 'loc1' / 'xx' / 'LC_MESSAGES' / 'test1.po'),
(tmp_path / 'loc1' / 'xx' / 'LC_MESSAGES' / 'test2.po'),
(tmp_path / 'loc1' / 'xx' / 'LC_MESSAGES' / 'sub' / 'test3.po'),
(tmp_path / 'loc1' / 'xx' / 'LC_MESSAGES' / 'sub' / 'test4.po'),
(tmp_path / 'loc1' / 'xx' / 'LC_MESSAGES' / '.dotdir' / 'test5.po'),
(tmp_path / 'loc1' / 'yy' / 'LC_MESSAGES' / 'test6.po'),
(tmp_path / 'loc2' / 'xx' / 'LC_MESSAGES' / 'test1.po'),
(tmp_path / 'loc2' / 'xx' / 'LC_MESSAGES' / 'test7.po'),
(tmp_path / 'loc1' / 'xx' / 'LC_MESSAGES' / '.dotdir2' / 'test8.po'),
):
po_file.parent.mkdir(parents=True, exist_ok=True)
po_file.write_text('#', encoding='utf8')
# for language xx
repo = i18n.CatalogRepository(tmp_path, ['loc1', 'loc2'], 'xx', 'utf-8')
assert list(repo.locale_dirs) == [
tmp_path / 'loc1',
tmp_path / 'loc2',
]
assert all(isinstance(c, i18n.CatalogInfo) for c in repo.catalogs)
assert sorted(c.domain for c in repo.catalogs) == [
'sub/test3',
'sub/test4',
'test1',
'test1',
'test2',
'test7',
]
# for language yy
repo = i18n.CatalogRepository(tmp_path, ['loc1', 'loc2'], 'yy', 'utf-8')
assert sorted(c.domain for c in repo.catalogs) == ['test6']
# unknown languages
repo = i18n.CatalogRepository(tmp_path, ['loc1', 'loc2'], 'zz', 'utf-8')
assert sorted(c.domain for c in repo.catalogs) == []
# no languages
repo = i18n.CatalogRepository(tmp_path, ['loc1', 'loc2'], '', 'utf-8')
assert sorted(c.domain for c in repo.catalogs) == []
# unknown locale_dirs
repo = i18n.CatalogRepository(tmp_path, ['loc3'], '', 'utf-8')
assert sorted(c.domain for c in repo.catalogs) == []
# no locale_dirs
repo = i18n.CatalogRepository(tmp_path, [], '', 'utf-8')
assert sorted(c.domain for c in repo.catalogs) == []