Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lingui ignores whitespace in multi-item Trans tags #525

Closed
aldeka opened this issue Jun 20, 2019 · 5 comments
Closed

Lingui ignores whitespace in multi-item Trans tags #525

aldeka opened this issue Jun 20, 2019 · 5 comments

Comments

@aldeka
Copy link

aldeka commented Jun 20, 2019

Describe the bug
Any whitespace following a span wrapped by a lingui <Trans> tag is removed, contrary to React's own behavior.

To Reproduce

In React, the following markup

<h2>
        {'Welcome '}
        <span className="name">{name}</span>
        {' to your website.'}
</h2>

produces "Welcome Jane to your website."

When wrapped with a lingui Trans tag, like so:

<h2>
        <Trans>
                {'Welcome '}
                <span className="name">{name}</span>
                {' to your website.'}
        </Trans>
</h2>

the output is "Welcome Janeto your website." -- the space after the span is removed.

Expected behavior

I expect a single space to remain after the node inside the Trans tag.

I've tried &nbsp;ing my way out, trying multiple ways to mark up the strings, and so far I've been unable to get that wanted space to stop getting stripped out of my sentence, even with hacks!

It's both inconsistent with React's behavior and a big pain in the butt when trying to translate any sentences with variables in them.

Additional context

  • lingui version: 2.7.4
  • babel version: 7.3
@tricoder42
Copy link
Contributor

Hi @aldeka, sorry for problems. This issue is already fixed in next branch:

// replace whitespace before/after newline with single space
const keepSpaceRe = /\s*(?:\r\n|\r|\n)+\s*/g
// remove whitespace before/after tag or expression
const stripAroundTagsRe = /(?:([>}])(?:\r\n|\r|\n)+\s*|(?:\r\n|\r|\n)+\s*(?=[<{]))/g
function normalizeWhitespace(text) {
return (
text
.replace(stripAroundTagsRe, "$1")
.replace(keepSpaceRe, " ")
// keep escaped newlines
.replace(/\\n/, "\n")
.replace(/\\s/, " ")
.trim()
)
}

If you want, backport it to master branch and please send a PR. I believe the relevant lines are in babel-plugin-transform-react:

const text = props.text
.replace(nlTagRe, "$1")
.replace(nlRe, " ")
.trim()

This will fix whitespace inconsistencies in JSX macros/plugins.


JS macros/plugins use similar approach, so we might also want to fix that as well:

Fix in next branch:

const keepSpaceRe = /(?:\\(?:\r\n|\r|\n))+\s+/g
const keepNewLineRe = /(?:\r\n|\r|\n)+\s+/g
function normalizeWhitespace(text) {
return text
.replace(keepSpaceRe, " ")
.replace(keepNewLineRe, "\n")
.trim()
}

Relevant code in master:

const text = props.text.replace(nlRe, " ").trim()

If you need any help with setting up the dev environment, please let me know 🙏

@bukku-jye
Copy link

bukku-jye commented Feb 3, 2023

We bump into the same problem with TS.

<Trans>
        {'Welcome '}
        <span className="name">Name</span>
        {' to your website.'}
</Trans>

We got

Welcome Nameto your website.

Any clues?

lingui version: 3.13.3
babel version: 7.18.5

@andrii-bodnar
Copy link
Contributor

@bukku-accounting could you please check if it's reproducible on the latest Lingui version?

@Santalov
Copy link

Reproducible in lingui 4.11.1

            <Trans>
              Friend will get
              {' '}
              {ticketsFromReferralSystem}
              {' '}
              extra tickets
            </Trans>

Produces
Friend will get5extra tickets

@timofei-iatsenko
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants