martinandert / counterpart

A translation and localization library for Node.js and the browser.
MIT License
242 stars 25 forks source link

Translation of a text with a % character causes "SyntaxError: [sprintf] unexpected placeholder" #37

Closed sleepy-angio closed 7 years ago

sleepy-angio commented 7 years ago

I'm using counterpart together with react translate and I am using it as such:

import counterpart from 'counterpart'; counterpart.translate('TOOLTIP_CHECKBOX')

if the TOOLTIP_CHECKBOX equals "About 25% of people..." the %-character will cause

SyntaxError: [sprintf] unexpected placeholder

Anybody know how to solve this?

yanndinendal commented 7 years ago

@sleepy-angio : Yes, the % should be escaped as it's the format placeholder (https://en.wikipedia.org/wiki/Printf_format_string#Format_placeholder_specification).

So you should write %% instead (https://en.wikipedia.org/wiki/Printf_format_string#Type_field) (to "[print] a literal % character").

So:

  const TOOLTIP_CHECKBOX = "About 25%% of people...";

and the double percent should be kept as-is in all translations.

martinandert commented 7 years ago

Thanks for responding so quickly, @yanndinendal !

martinandert commented 7 years ago

See also #2 and #23. It's a sprintf quirk.

sleepy-angio commented 7 years ago

Ok, thanks!