EddyVerbruggen / nativescript-i18n

This is a plugin for Nativescript that implements native i18n in an easy manner.
65 stars 30 forks source link

Escape % sign? #53

Closed lukeramsden closed 7 years ago

lukeramsden commented 7 years ago

For some reason, I can't get the % sign to display. I've tried 75%, 75\% and 75%, but none of them seem to work.

rborn commented 7 years ago

@lukeramsden I assume you want to put it in strings.xml, right?

<string formatted="false" name="percent">Increase with %s %% </string>

and then in js

viewModel.message = L('percent', 75);

will results in screen shot 2017-05-09 at 15 45 26

lukeramsden commented 7 years ago

@rborn Is there a way to escape it without using replacement? It's just in some static text. It wasn't working just escaping it with \

rborn commented 7 years ago

@lukeramsden the plugin uses sprintf behind the scenes so a static text would still need %% in it to display the percent sign. So this should work without the replacement

<string formatted="false" name="percent">Increase with 42%% </string>
viewModel.message = L('percent');
lukeramsden commented 7 years ago

So putting a % sign in your text requires formatted to be false? That should go in the docs

lukeramsden commented 7 years ago

So after testing it, that works perfectly (even with XML bindings and not JS ones) so that should 100% go in the docs.

rborn commented 7 years ago

formatted="false" needs to be there only if there are replacements (that's in the docs already). You can use it without formatted if you don't have any replacements, but the key is the double %

https://linux.die.net/man/3/sprintf

% A '%' is written. No argument is converted. The complete conversion specification is '%%'.

lukeramsden commented 7 years ago

@rborn Then why wouldn't the percent signs show up without that?

rborn commented 7 years ago

Here: https://github.com/rborn/nativescript-i18n/blob/master/i18n.ios.js#L10

This uses format which as you can see replicates sprintf

sprintf treats % as a modifier and tries to replace it with something based on the rules in the man page I sent you. If nothing is found it will remove the % sign. So you need to tell it you really want it there by %% 😄

Makes more sense?

lukeramsden commented 7 years ago

@rborn Yes I think so. As far as I could tell, the %% wasn't working without formatted="false", but I don't have time to test that because it requires me to build from scratch and I'm currently working on other things. Either way, what I put in the README works 😄

rborn commented 7 years ago

It works, I tested it, but leave it with formatted because will save us from issues opened in the cases where a user would have both replacement and %%

Thank you for all your help :)

lukeramsden commented 7 years ago

No problem 😄