Opened 17 years ago

Closed 11 years ago

#5849 closed New feature (fixed)

Strip indentation and leading/trailing spaces/linebreaks from contents of blocktrans block.

Reported by: Dmitri Fedortchenko <zeraien@…> Owned by: Bouke Haarsma
Component: Internationalization Version: dev
Severity: Normal Keywords: blocktrans strip indentation
Cc: chris@…, phartig@…, macek@…, Danilo Bargen, bbr@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The idea is to strip whitespace indentation from the contents of the blocktrans tag, as well as leading and trailing whitespace and linebreaks.

Blocktrans tags allow you to split your translation strings onto multiple lines and this is a good feature, especially if using the "plural" mode.

Picture this:

{% blocktrans number as count %}
There is {{count}} message
waiting for you
{% plural %}
There are {{count}} messages
waiting for you
{% endblocktrans %}

The biggest issue is if you indent the above block. Suddenly those msgids are obsolete and you need to edit all your language files....

Indentation is often changed in HTML since you might nest that block in a new DIV element or whatnot, and the ability to spread the msgids along many lines makes for cleaner looking code.

Furthermore, the current blocktrans tag implementation generates some ugly translation strings when using make-messages if you have indented your msgid block.
Since most HTML is somehow indented, the above can easily look like this:

msgid "\n"
"\t\t\t\t\tThere are %(count)s messages\n"
"\t\t\t\t\twaiting for you\n"
"\t\t\t\t\t"

Note the last line of tabs, since it includes the indentation of the "endblocktrans" tag as well.

See this thread for more:
http://groups.google.com/group/django-developers/browse_thread/thread/60f0a0ac2347c5f5

Attachments (3)

blocktrans_strip.diff (3.4 KB ) - added by Dmitri Fedortchenko <zeraien@…> 17 years ago.
Patch that adds stripping of indentation from contents of blocktrans block.
strip_blocktrans_tests.py (3.8 KB ) - added by Dmitri Fedortchenko <zeraien@…> 17 years ago.
Some unit tests to show and test outcome of stripping process
blocktrans_strip.2.diff (3.4 KB ) - added by Dmitri Fedortchenko <zeraien@…> 17 years ago.
Original patch could not be applied after rev 6689, this is an update.

Download all attachments as: .zip

Change History (17)

by Dmitri Fedortchenko <zeraien@…>, 17 years ago

Attachment: blocktrans_strip.diff added

Patch that adds stripping of indentation from contents of blocktrans block.

by Dmitri Fedortchenko <zeraien@…>, 17 years ago

Attachment: strip_blocktrans_tests.py added

Some unit tests to show and test outcome of stripping process

comment:1 by Simon G <dev@…>, 17 years ago

Patch needs improvement: set
Triage Stage: UnreviewedAccepted

Accepted, with a possible improvements depending on MT's judgement :)

comment:2 by Dmitri Fedortchenko <zeraien@…>, 17 years ago

I would like to start by pointing out that as it is right now, the patch breaks a few blocktrans msgids from the admin app.
However I feel that it might be worth it anyway, and I am sure there can be a workaround. Perhaps a transitional period where two checks are made, one with stripping and then a second AS IS if the first fails...

by Dmitri Fedortchenko <zeraien@…>, 17 years ago

Attachment: blocktrans_strip.2.diff added

Original patch could not be applied after rev 6689, this is an update.

comment:3 by Gabriel Hurley, 14 years ago

Severity: Normal
Type: Cleanup/optimization

comment:4 by Chris Adams, 13 years ago

Cc: chris@… added
Easy pickings: unset
UI/UX: unset

comment:5 by Simon Charette, 13 years ago

I happen to use blocktrans template tag in a non-html template where indentation is important.

What about adding a spaceless_blocktrans block? It would maintain backward compatibility while fixing this issue.

comment:6 by phartig@…, 13 years ago

Cc: phartig@… added

comment:7 by Vlada Macek, 13 years ago

Cc: macek@… added
Component: Template systemInternationalization
Type: Cleanup/optimizationNew feature

I also hate those \t's and \n's in blocktrans! When passing PO's to 3rd party translation agencies, they're not always able to handle escapes correctly.

To avoid \t's and \n's, I'm forced to have blocktranses on a single line. With plural form and variable interpolation it can easily become 300 chars line!

If we want to stay backwards compatible and not adding any new tag, wouldn't it be possible to add a "trimmed" option inside the blocktrans option?

Something like: {% blocktrans trimmed ... %}

It would:

  • strip all whitespace from start and end of the string,
  • do s/[\t\n]+/ /g

Just as the browser does.

Thanks for consideration. (I also dare to change the ticket properties.)

comment:8 by Danilo Bargen, 13 years ago

Cc: Danilo Bargen added

I think both a blocktrans argument or a new tag would be acceptable.

in reply to:  description comment:9 by Benedikt Breinbauer, 12 years ago

Cc: bbr@… added

Replying to Dmitri Fedortchenko <zeraien@…>:

The biggest issue is if you indent the above block. Suddenly those msgids are obsolete and you need to edit all your language files....

Indentation is often changed in HTML since you might nest that block in a new DIV element or whatnot, and the ability to spread the msgids along many lines makes for cleaner looking code.

Another problem that I haven't seen mentioned, that is implied above: The same blocktrans text could be used at different indentation levels and gets interpreted as different msgids, thus needs to be translated more than necessary.

Version 0, edited 12 years ago by Benedikt Breinbauer (next)

comment:10 by Apostolis Bessas, 12 years ago

Owner: changed from nobody to Apostolis Bessas
Patch needs improvement: unset
Status: newassigned

comment:11 by Apostolis Bessas, 12 years ago

comment:12 by Bouke Haarsma, 11 years ago

Owner: changed from Apostolis Bessas to Bouke Haarsma

comment:13 by Bouke Haarsma, 11 years ago

Triage Stage: AcceptedReady for checkin

RFC as review comments by aaugustin have been solved

comment:14 by Anssi Kääriäinen <akaariai@…>, 11 years ago

Resolution: fixed
Status: assignedclosed

In 7a7c789d5a7ce1c62e3739cb2624454189410ece:

Fixed #5849 -- Strip whitespace from blocktrans

Add the trimmed option to the blocktrans tag to trim any newlines and
whitespace from its content.

This allows the developer to indent the blocktrans tag without adding
new lines and whitespace to the msgid in the PO file.

Thanks to mpessas for the initial patch and Dmitri Fedortchenko for the
report.

Note: See TracTickets for help on using tickets.
Back to Top