diff --git a/genshi/filters/i18n.py b/genshi/filters/i18n.py
|
a
|
b
|
|
| 131 | 131 | gettext = self.translator.ugettext |
| 132 | 132 | except AttributeError: |
| 133 | 133 | gettext = self.translator.gettext |
| | 134 | try: |
| | 135 | # Unicode aware version first |
| | 136 | ngettext = self.translator.ungettext |
| | 137 | except AttributeError: |
| | 138 | ngettext = self.translator.ngettext |
| 134 | 139 | |
| 135 | 140 | if not self.extract_text: |
| 136 | 141 | search_text = False |
| 137 | 142 | skip = 0 |
| 138 | | i18n_msg = I18N_NAMESPACE['msg'] |
| | 143 | i18n_msg = I18N_NAMESPACE['msg'] |
| | 144 | i18n_choose = I18N_NAMESPACE['choose'] |
| 139 | 145 | ns_prefixes = [] |
| 140 | 146 | xml_lang = XML_NAMESPACE['lang'] |
| 141 | | |
| | 147 | |
| 142 | 148 | for kind, data, pos in stream: |
| 143 | 149 | |
| 144 | 150 | # skip chunks that should not be localized |
| … |
… |
|
| 184 | 190 | params = attrs.get(i18n_msg) |
| 185 | 191 | msgbuf = MessageBuffer(params) |
| 186 | 192 | attrs -= i18n_msg |
| | 193 | elif i18n_choose in attrs: |
| | 194 | params = attrs.get(i18n_choose) |
| | 195 | msgbuf = MessageBuffer(params) |
| | 196 | attrs -= i18n_choose |
| 187 | 197 | |
| 188 | 198 | yield kind, (tag, attrs), pos |
| 189 | 199 | |
| … |
… |
|
| 202 | 212 | elif not skip and msgbuf and kind is END: |
| 203 | 213 | msgbuf.append(kind, data, pos) |
| 204 | 214 | if not msgbuf.depth: |
| 205 | | for event in msgbuf.translate(gettext(msgbuf.format())): |
| 206 | | yield event |
| | 215 | if msgbuf.singular or msgbuf.plural: |
| | 216 | singular, plural, expr = msgbuf.format() |
| | 217 | events = ngettext(singular, plural, expr.evaluate(ctxt)) |
| | 218 | for event in msgbuf.translate(events): |
| | 219 | yield event |
| | 220 | else: |
| | 221 | for event in msgbuf.translate(gettext(msgbuf.format())): |
| | 222 | yield event |
| 207 | 223 | msgbuf = None |
| 208 | 224 | yield kind, data, pos |
| 209 | 225 | |
| … |
… |
|
| 298 | 314 | if name in self.include_attrs: |
| 299 | 315 | text = value.strip() |
| 300 | 316 | if text: |
| 301 | | yield pos[1], None, text |
| | 317 | yield pos[1], None, text, [] |
| 302 | 318 | else: |
| 303 | 319 | for lineno, funcname, text, comments in self.extract( |
| 304 | 320 | _ensure(value), gettext_functions, |
| … |
… |
|
| 328 | 344 | msgbuf.append(kind, data, pos) |
| 329 | 345 | if not msgbuf.depth: |
| 330 | 346 | if msgbuf.singular or msgbuf.plural: |
| 331 | | yield msgbuf.lineno, 'ngettext', msgbuf.format(), \ |
| | 347 | singular, plural, num = msgbuf.format() |
| | 348 | yield msgbuf.lineno, 'ngettext', (singular, plural), \ |
| 332 | 349 | msgbuf.comments |
| 333 | 350 | else: |
| 334 | 351 | yield msgbuf.lineno, None, msgbuf.format(), \ |
| … |
… |
|
| 374 | 391 | self.params.append(param.strip()) |
| 375 | 392 | elif entry[0] == 'EXPR': |
| 376 | 393 | self.choose_numeral = entry[1] |
| | 394 | elif isinstance(params, basestring): |
| | 395 | self.params = [p.strip() for p in params.split(',')] |
| 377 | 396 | else: |
| 378 | 397 | self.params = params |
| 379 | 398 | self.singular_params = self.params[:] |
| … |
… |
|
| 384 | 403 | self.plural = [] |
| 385 | 404 | self.events = {} |
| 386 | 405 | self.values = {} |
| 387 | | self.singular_values = {} |
| 388 | | self.plural_values = {} |
| 389 | 406 | self.depth = 1 |
| 390 | 407 | self.order = 1 |
| 391 | 408 | self.choose_order = 1 |
| … |
… |
|
| 414 | 431 | elif kind is EXPR: |
| 415 | 432 | if self.choose_singular: |
| 416 | 433 | param = self.singular_params.pop(0) |
| 417 | | self.singular.append('%%(%s)s' % param) |
| 418 | | self.singular_values[param] = (kind, data, pos) |
| | 434 | self.singular.append('%%(%s)s' % param) |
| 419 | 435 | elif self.choose_plural: |
| 420 | 436 | param = self.plural_params.pop(0) |
| 421 | 437 | self.plural.append('%%(%s)s' % param) |
| 422 | | self.plural_values[param] = (kind, data, pos) |
| 423 | 438 | else: |
| 424 | 439 | param = self.params.pop(0) |
| 425 | 440 | self.string.append('%%(%s)s' % param) |
| 426 | | self.values[param] = (kind, data, pos) |
| | 441 | self.values[param] = (kind, data, pos) |
| 427 | 442 | self.events.setdefault(self.stack[-1], []).append(None) |
| 428 | 443 | else: |
| 429 | 444 | if kind is START: |
| 430 | 445 | if self.choose_singular: |
| 431 | | self.ordered_singular = True |
| 432 | 446 | self.singular.append(u'[%d:' % self.choose_order) |
| | 447 | self.events.setdefault(self.choose_order, |
| | 448 | []).append((kind, data, pos)) |
| 433 | 449 | elif self.choose_plural: |
| 434 | | self.ordered_plural = True |
| 435 | 450 | self.plural.append(u'[%d:' % self.choose_order) |
| | 451 | self.events.setdefault(self.choose_order, |
| | 452 | []).append((kind, data, pos)) |
| | 453 | self.stack.append(self.choose_order) |
| 436 | 454 | self.choose_order += 1 |
| 437 | 455 | elif self.i18n_choose_singular in data[1]: |
| 438 | 456 | self.choose_singular = True |
| … |
… |
|
| 442 | 460 | self.choose_singular = False |
| 443 | 461 | else: |
| 444 | 462 | self.string.append(u'[%d:' % self.order) |
| | 463 | self.events.setdefault(self.order, |
| | 464 | []).append((kind, data, pos)) |
| | 465 | self.stack.append(self.order) |
| 445 | 466 | self.order += 1 |
| 446 | | self.events.setdefault(self.order, []).append((kind, data, pos)) |
| 447 | | self.stack.append(self.order) |
| 448 | 467 | self.depth += 1 |
| 449 | 468 | elif kind is END: |
| 450 | 469 | self.depth -= 1 |
| … |
… |
|
| 453 | 472 | self.choose_singular = False |
| 454 | 473 | if self.choose_order > 1: |
| 455 | 474 | self.singular.append(u']') |
| 456 | | self.ordered_singular = False |
| 457 | 475 | elif self.choose_plural: |
| 458 | 476 | self.choose_plural = False |
| 459 | 477 | if self.choose_order > 1: |
| … |
… |
|
| 469 | 487 | """ |
| 470 | 488 | if self.singular or self.plural: |
| 471 | 489 | return (u''.join(self.singular).strip(), |
| 472 | | u''.join(self.plural).strip()) |
| | 490 | u''.join(self.plural).strip(), self.choose_numeral) |
| 473 | 491 | return u''.join(self.string).strip() |
| 474 | 492 | |
| 475 | 493 | def translate(self, string, regex=re.compile(r'%\((\w+)\)s')): |