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

Accessing string with percentage symbol strips it #859

Closed
igorkulman opened this issue Jul 16, 2021 · 4 comments
Closed

Accessing string with percentage symbol strips it #859

igorkulman opened this issue Jul 16, 2021 · 4 comments

Comments

@igorkulman
Copy link

I am using SwiftGen to generate strings with the structured swift5 template

strings:
  inputs:
    - Source/Localization/en.lproj/Localizable.strings
    - Source/Localization/en.lproj/Localizable.stringsdict
  outputs:
    params:
      publicAccess: true
      enumName: LocalizedString
    templateName: structured-swift5
    output: Source/LocalizedString.swift

If I have a string that contains the percentage symbol, like

"percentage_symbol_test" = "Result is 100%";

accessing LocalizedString.percentageSymbolTest gives me Result is 100 with the percentage being stripped.

@harryblam
Copy link

harryblam commented Aug 5, 2021

I'm seeing this issue as well.

I've found a workaround however, which i'm using whenever I need to use the % symbol in my strings. I simple substitute the symbol with a %@ placeholder, which allows me to input the % symbol as a string in code.
I will give an example using the above code:
"percentage_symbol_workaround" = "Result is %@";

and the usage in Swift code:
L10n.percentageSymbolWorkaround("100%");

@igorkulman
Copy link
Author

My workaround us doubling the percentage symbol, so "percentage_symbol_test" = "Result is 100%%";

@djbe
Copy link
Member

djbe commented Aug 5, 2021

Duplicate of quite a few issues:

This is an effect of all translations being run through String(format:). At some point we tried being "smarter" about this, i.e. detect if there were any parameters in the translation, and if not, to not run it through String(format:), but that led to a whole bunch of bugs for other users, so we quickly had to revert it.

TL;DR: you have to escape the %, so your string becomes: Result is 100%%.

@djbe djbe closed this as completed Aug 5, 2021
@djbe djbe added the duplicate label Aug 5, 2021
@harryblam
Copy link

Duplicate of quite a few issues:

This is an effect of all translations being run through String(format:). At some point we tried being "smarter" about this, i.e. detect if there were any parameters in the translation, and if not, to not run it through String(format:), but that led to a whole bunch of bugs for other users, so we quickly had to revert it.

TL;DR: you have to escape the %, so your string becomes: Result is 100%%.

Thanks @djbe for the info! It makes sense now that you've explained the String(format:) side effects. Perhaps it would be worth updating the README with this info as "gotcha" or something?

I guess I could have found this out by searching the closed issues but I don't normally go to that much effort before raising an issue tbh 😅 .

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

3 participants