Opened 9 years ago

Closed 3 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: master
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@…> 9 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@…> 9 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@…> 9 years ago.
Original patch could not be applied after rev 6689, this is an update.

Download all attachments as: .zip

Change History (17)

Changed 9 years ago by Dmitri Fedortchenko <zeraien@…>

Attachment: blocktrans_strip.diff added

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

Changed 9 years ago by Dmitri Fedortchenko <zeraien@…>

Attachment: strip_blocktrans_tests.py added

Some unit tests to show and test outcome of stripping process

comment:1 Changed 9 years ago by Simon G <dev@…>

Needs documentation: unset
Needs tests: unset
Patch needs improvement: set
Triage Stage: UnreviewedAccepted

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

comment:2 Changed 9 years ago by Dmitri Fedortchenko <zeraien@…>

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...

Changed 9 years ago by Dmitri Fedortchenko <zeraien@…>

Attachment: blocktrans_strip.2.diff added

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

comment:3 Changed 6 years ago by Gabriel Hurley

Severity: Normal
Type: Cleanup/optimization

comment:4 Changed 5 years ago by Chris Adams

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

comment:5 Changed 5 years ago by Simon Charette

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 Changed 5 years ago by phartig@…

Cc: phartig@… added

comment:7 Changed 5 years ago by Vlada Macek

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 Changed 5 years ago by Danilo Bargen

Cc: Danilo Bargen added

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

comment:9 in reply to:  description Changed 4 years ago by Benedikt Breinbauer

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 explicitly but 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 once.

Last edited 4 years ago by Benedikt Breinbauer (previous) (diff)

comment:10 Changed 4 years ago by Apostolis Bessas

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

comment:11 Changed 3 years ago by Apostolis Bessas

comment:12 Changed 3 years ago by Bouke Haarsma

Owner: changed from Apostolis Bessas to Bouke Haarsma

comment:13 Changed 3 years ago by Bouke Haarsma

Triage Stage: AcceptedReady for checkin

RFC as review comments by aaugustin have been solved

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

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