Commit a23fa42
fix: pass grammatical context through naturaldelta for i18n case-aware translations
naturaltime() composes naturaldelta() output with format strings like
'%s ago' / '%s from now'. Languages such as German require different
grammatical cases depending on the surrounding preposition (e.g.
nominative 'eine Stunde' vs dative 'einer Stunde' after 'vor').
The two-step composition (naturaldelta → wrap) made it impossible for
translators to provide case-correct forms because naturaldelta had no
knowledge of the context in which its result would be used.
Changes:
- Add _npgettext() to humanize.i18n (plural + context gettext).
- Add an optional 'context' parameter to naturaldelta(). When set,
all internal gettext/ngettext calls are replaced with their
pgettext/npgettext counterparts, keyed by the supplied context.
- naturaltime() now passes 'naturaltime-past' or 'naturaltime-future'
as context so translators can provide case-aware forms via msgctxt
entries in .po files.
The change is fully backward-compatible: without a context (or without
matching msgctxt translations), output is identical to before.
Fixes #263
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent ce4147b commit a23fa42
3 files changed
Lines changed: 126 additions & 26 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
143 | 163 | | |
144 | 164 | | |
145 | 165 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
| 101 | + | |
101 | 102 | | |
102 | 103 | | |
103 | 104 | | |
| |||
110 | 111 | | |
111 | 112 | | |
112 | 113 | | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
113 | 119 | | |
114 | 120 | | |
115 | 121 | | |
| |||
138 | 144 | | |
139 | 145 | | |
140 | 146 | | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
141 | 159 | | |
142 | 160 | | |
143 | 161 | | |
| |||
165 | 183 | | |
166 | 184 | | |
167 | 185 | | |
168 | | - | |
| 186 | + | |
169 | 187 | | |
170 | 188 | | |
171 | 189 | | |
| |||
174 | 192 | | |
175 | 193 | | |
176 | 194 | | |
177 | | - | |
| 195 | + | |
178 | 196 | | |
179 | 197 | | |
180 | | - | |
| 198 | + | |
181 | 199 | | |
182 | 200 | | |
183 | | - | |
| 201 | + | |
184 | 202 | | |
185 | 203 | | |
186 | | - | |
| 204 | + | |
187 | 205 | | |
188 | 206 | | |
189 | 207 | | |
190 | 208 | | |
191 | | - | |
| 209 | + | |
192 | 210 | | |
193 | 211 | | |
194 | | - | |
| 212 | + | |
195 | 213 | | |
196 | | - | |
| 214 | + | |
197 | 215 | | |
198 | 216 | | |
199 | 217 | | |
200 | 218 | | |
201 | | - | |
| 219 | + | |
202 | 220 | | |
203 | 221 | | |
204 | | - | |
| 222 | + | |
205 | 223 | | |
206 | | - | |
| 224 | + | |
207 | 225 | | |
208 | 226 | | |
209 | 227 | | |
210 | | - | |
| 228 | + | |
211 | 229 | | |
212 | 230 | | |
213 | | - | |
| 231 | + | |
214 | 232 | | |
215 | 233 | | |
216 | | - | |
| 234 | + | |
217 | 235 | | |
218 | 236 | | |
219 | | - | |
| 237 | + | |
220 | 238 | | |
221 | 239 | | |
222 | | - | |
| 240 | + | |
223 | 241 | | |
224 | | - | |
| 242 | + | |
225 | 243 | | |
226 | 244 | | |
227 | 245 | | |
228 | | - | |
| 246 | + | |
229 | 247 | | |
230 | 248 | | |
231 | | - | |
| 249 | + | |
232 | 250 | | |
233 | 251 | | |
234 | 252 | | |
235 | | - | |
| 253 | + | |
236 | 254 | | |
237 | 255 | | |
238 | 256 | | |
239 | | - | |
| 257 | + | |
240 | 258 | | |
241 | 259 | | |
242 | | - | |
| 260 | + | |
243 | 261 | | |
244 | 262 | | |
245 | 263 | | |
246 | | - | |
| 264 | + | |
247 | 265 | | |
248 | | - | |
| 266 | + | |
249 | 267 | | |
250 | 268 | | |
251 | 269 | | |
| |||
291 | 309 | | |
292 | 310 | | |
293 | 311 | | |
294 | | - | |
| 312 | + | |
| 313 | + | |
295 | 314 | | |
296 | 315 | | |
297 | 316 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
852 | 852 | | |
853 | 853 | | |
854 | 854 | | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
0 commit comments