Skip to content

Feature request: Keeping spaces #62

@stevengunneweg

Description

@stevengunneweg

I use this library for a while already and it works great but I need one additional feature. For this reason I created a fork but it feels ridiculous to maintain a fork for 1 tiny change.

The issue:

When I have a text that I want to cut to apply some additional HTML for example, the spaces are trimmed even though this is not desired.

E.g.
for the following HTML

<div>
  {{ 'translation' | translate | translateCut: 0 }}
  <a href="#">{{ 'translation' | translate | translateCut: 1 }}</a>
  {{ 'translation' | translate | translateCut: 2 }}  
</div>

"translation": "This is a |cut| sentence.",
will work fine since my HTML is responsible for placing the correct spaces.

However when my cut is at the end of a sentence like this
"translation": "This is a sentence that is |cut|.",
the HTML does not know whether a space is needed between the cuts so there will be either spaces on both sides of cut or none at all, neither is correct.

My fix

To fix this I made the following change to /projects/ngx-translate-cut/src/lib/ngx-translate-cut.pipe.ts (which could be optimised further)

-    const result = phrase ? phrase.trim() : '';
+    const result = phrase ? phrase : '';

This will keep the existing spaces from the original translation and just needs small adjustment to my html to work without any issues

<div>
  {{ 'translation' | translate | translateCut: 0 }}<a
    href="#">{{ 'translation' | translate | translateCut: 1 }}</a
  >{{ 'translation' | translate | translateCut: 2 }}  
</div>

If this is too much of a risk to just apply without it breaking for other users of this library it could become a configuration option in the forRoot implementation.
Personally I doubt that this change will break anything for other users since they are already responsible for the correct spaces in their HTML but it would help me a lot. Please consider my request.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions