question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow escaping placeholders

See original GitHub issue

Describe the bug

Both react FilePond and lingui.js use {} chars to mark variables. When you try to add FilePonf label for translations you get an error because lingui expect you will pass a value of the variable, but it actually FilePond’s variable.

To Reproduce Steps to reproduce the behavior, possibly with minimal code sample, e.g:

import { t } from '@lingui/macro';
import { I18n } from '@lingui/react';
import { FilePond } from 'react-filepond';

export default function App() {
   return (<I18n>
        {({ i18n }) => (<FilePond
                  …
                  labelMaxFileSize={i18n._(t`Maximum file size is {filesize}`)} />))}
        </I18n>);
}

You will get TypeError: Cannot read property 'filesize' of undefined — it came from /node_modules/@lingui/core/cjs/core.development.js:134. I suppose that lingui.js think that filesize is lingui.js variable and expect filesize value should be passed. But it’s should be passed by FilePhond.

Expected behavior It would be nice to escape {} somehow. I have tried / and // but I either get an error, either it just doesn’t translate it.

Additional context Add any other context about the problem here.

  • jsLingui version lingui --version 1.4.5
  • Babel version npm list babel-core
$ npm list babel-core
instapro-client@1.0.0 /Users/silentimp/Work/instapro-client
└─┬ next@7.0.3
  └── babel-core@7.0.0-bridge.0 
  • Your Babel config (e.g. .babelrc) or framework you use (Create React App, Meteor, etc.)
{
  "presets": [
    [
      "@babel/preset-env",
      {
        "modules": false,
        "debug": false,
        "useBuiltIns": "usage",
        "targets": {
          "browsers": [">0.2%", "not dead", "not ie < 11", "not op_mini all"]
        }
      }
    ],
    "next/babel"
  ],
  "plugins": ["macros"]
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
hjylewiscommented, Jun 7, 2019

Actually, I think this is an actual non-production bug with how the lingui compiler parses escaping characters that would exist even after upgrading to messageformat-parser@4.0.0.

In messageformat-parser@2.0.0, using \\ instead of ' should escape the curly braces (i.e. Maximum file size is \\{filesize\\}).

In non-production env, messages that are compiled to strings are compiled twice, once on I18n.load and again on I18n._.

Since the messageformat-parser removes the escape characters, the redundant parsing causes the escape characters to be ignored.

parse(parse('Maximum file size is \\{filesize\\}'))
↓↓↓↓↓↓↓↓
parse('Maximum file size is {filesize}')
↓↓↓↓↓↓↓↓
compiled message function that treats 'filesize' as an argument

A solution would be to try to avoid compiling messages twice, either by always compiling to a functiion instead of sometimes a string or avoiding compiling on I18n._ if this.messages[id] exists.

1reaction
hjylewiscommented, Jun 5, 2019

Looks like this is present in the newer version of messageformat-parser but not in version 2.0.0 that @lingui/core uses.

Here’s the messageformat-parser changelog

Read more comments on GitHub >

github_iconTop Results From Across the Web

DBD::Pg escaping placeholders with backslashes
First, what are placeholders? They are special characters within a SQL statement that allow you to defer adding actual values until a later...
Read more >
Re: Escaping placeholders - nntp.perl.org
1. I propose that there be no modification to the DBI spec related to > new escaping whether optional or not, so leave...
Read more >
Template literals (Template strings) - JavaScript | MDN
In normal template literals, the escape sequences in string literals are all allowed. Any other non-well-formed escape sequence is a syntax ...
Read more >
Escaping jinja template for javascript place holder
I want to pass a javascript variable ${channels[i]} to my jinja2 template as a placeholder for the href attribute of the link tag....
Read more >
String Interpolation in JavaScript - Dmitri Pavlutin
3. Escaping placeholders ... Because the placeholder format ${expression} has a special meaning in the template literals, you cannot use the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found