lingui / js-lingui

🌍 📖 A readable, automated, and optimized (3 kb) internationalization for JavaScript
https://lingui.dev
MIT License
4.49k stars 378 forks source link

Interpolation breaking if variable is surrounded by single quotes #421

Closed hatton closed 3 years ago

hatton commented 5 years ago

Describe the bug Interpolated variables surrounded by single quotation marks are treated as literal text.

To Reproduce

If I compile

msg = i18n._(
        t`There is already a ${folderKind} with the name or code '${value}'.`
      );

then messages.js has:

"There is already a {folderKind} with the name or code '{value}'.":
function(a){return["There is already a ",a("folderKind")," with the name or code {value}."]}

Note that "value" has lost its surrounding single quotes and has not been identified as an interpolated value.

One workaround is to use double quotes instead:

msg = i18n._(
        t`There is already a ${folderKind} with the name or code "${value}".`
      );

which compiles to:

"There is already a {folderKind} with the name or code \"{value}\".":
function(a){return["There is already a ",a("folderKind")," with the name or code \"",a("value"),"\"."]}
          {
            loader: "babel-loader",
            options: {
              presets: [
                "@babel/preset-typescript",
                "@babel/preset-react",
                "@lingui/babel-preset-react",

                [
                  "@babel/preset-env",
                  {
                    targets: {
                      // else we get regeneratorRuntime is not defined
                      browsers: ["chrome 61"] // set to match whatever chromium electron is using
                    }
                  }
                ]
              ],
              plugins: [
                "babel-plugin-macros",
                ["@babel/plugin-proposal-decorators", { legacy: true }],
                ["@babel/plugin-proposal-class-properties", { loose: true }]
              ]
            }
          }
tricoder42 commented 5 years ago

Hi @hatton, thank you. I can confirm this is a bug. extract works fine, but compile is a problem. Interested is sending a PR?

hjylewis commented 5 years ago

Try using '' double apostrophes i.e.

"There is already a ${folderKind} with the name or code ''${value}''."

The ICU MessageFormat actually uses ' as escape characters hence why the text in-between is treated as literal text.

Instead, a pair of apostrophes '' will be converted to a single one '

A pair of ASCII apostrophes always represents one ASCII apostrophe, similar to %% in printf representing one %, although this rule still applies inside quoted text. ("This '{isn''t}' obvious" → "This {isn't} obvious")

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.