Swiftgen: Problems with '%' symbol

Created on 27 Jul 2018  Â·  5Comments  Â·  Source: SwiftGen/SwiftGen

In Localizable.strings I have such value:
"blaString" = "bla bla bla: 90% bla bla";

After generating file in this way:

swiftgen strings <path to dir>/Localizable.strings -t flat-swift4 -o "<output_path>/Strings.swift"

I get such generated code:

internal enum L10n {
// ...
  internal static let blaString = L10n.tr("Localizable", "blaString")
// ...
}

extension L10n {
  private static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String {
    let format = NSLocalizedString(key, tableName: table, bundle: Bundle(for: BundleToken.self), comment: "")
    return String(format: format, locale: Locale.current, arguments: args)
  }
}

private final class BundleToken {}

I use it in code like this:

someLabel.text = L10n.blaString

But the text in label is not "bla bla bla: 90% bla bla" but: "bla bla bla: 90 la bla"
'%' and next letter disappears. I think return String(format: format, locale: Locale.current, arguments: args) is the case. Is there any workaround? I've already tried \U0025 ('%' in UTF-16), same result.

question

All 5 comments

I'm pretty sure this is unrelated to SwiftGen, and is because how NSLocalizedString / NSString do their thing. Have you tried a double % in your strings file?

blaString" = "bla bla bla: 90%% bla bla";

Yep, it's because of String(format:):
https://stackoverflow.com/questions/739682/how-to-add-percent-sign-to-nsstring#739707

To have percent characters in your string, you have to escape it using %%.

@djbe thank you. Yes thank you, sorry for inconvenience.

Given how String(format: ) works if you need to have a literal % you need to escape it using %%. See the printf-style documentation about %… placeholders

Ha once again @djbe was faster to reply ^^

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jrmybrault picture jrmybrault  Â·  7Comments

razor313 picture razor313  Â·  6Comments

filip-zielinski picture filip-zielinski  Â·  3Comments

IanHoar picture IanHoar  Â·  7Comments

taykay08 picture taykay08  Â·  3Comments