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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better fallback for placeholders that are only used in the variables but not in the format key #1068

Open
sewerynplazuk opened this issue Jul 5, 2023 · 1 comment

Comments

@sewerynplazuk
Copy link

Hello everyone 馃憢
In my project we use SwiftGen (SwiftGen v6.6.2 (Stencil v0.15.1, StencilSwiftKit v2.10.1, SwiftGenKit v6.6.2)) and Lokalise.

I am trying to make plural translations generated from Lokalise work with SwiftGen. I know that SwiftGen does not support plural keys such as the following:

    <key>some.translation</key>
    <dict>
      <key>NSStringLocalizedFormatKey</key>
      <string>%#@format@</string>
      <key>format</key>
      <dict>
        <key>NSStringFormatSpecTypeKey</key>
        <string>NSStringPluralRuleType</string>
        <key>NSStringFormatValueTypeKey</key>
        <string>li</string>
        <key>one</key>
        <string>Some %1$@ text %3$@ with 2 arguments</string>
        <key>other</key>
        <string>Some %1$@ text %2$li with 3 - %3$@ - arguments</string>
      </dict>
    </dict>

but, unfortunately, we have bunch of these. At the moment, such a translation leads to code such as this:

/// Plural format key: "%#@format@"
internal static func translation(_ p1: Int) -> String {...}

This is obviously wrong therefore, I've written my own template, which detect plurals based on the SwiftGen implementation detail - each plural is preceded by a "Plural format key". In short, I ended up with the generated code looking like this:

/// Plural format key: "%#@format@"
internal static func translation(_ args: CVarArg...) -> String {...}

but this is not an ideal solution either. Firstly, developers need to be constantly aware of what types and in what order the translation accepts, and secondly, the whole approach relies on implementation details. Therefore, I have 3 questions:

  1. Would it make sense to add a Boolean flag or other tag to make the templates know which key is plural or singular?
  2. Are there any plans to support this type of plural out of the box? Lokalise is a fairly popular translation management service, so I expect this is a common problem.
  3. What's your opinion about this in general? Do you recommend any other approach?
@wleivsco
Copy link

I'm also facing a similar issue where the fallback for stringsdict isn't working as expected. In swiftgen official README example, it shows that the fallback shouldn't be "Pural format key ....". Here is the example from swiftgen.

internal enum L10n {
  /// Some alert body there
  internal static let alertMessage = L10n.tr("Localizable", "alert__message", fallback: #"Some alert body there"#)
  /// Title for an alert
  internal static let alertTitle = L10n.tr("Localizable", "alert__title", fallback: #"Title of the alert"#)
  internal enum Apples {
    /// You have %d apples
    internal static func count(_ p1: Int) -> String {
      return L10n.tr("Localizable", "apples.count", p1, fallback: #"You have %d apples"#)
    }
  }
  internal enum Bananas {
    /// A comment with no space above it
    internal static func owner(_ p1: Int, _ p2: Any) -> String {
      return L10n.tr("Localizable", "bananas.owner", p1, String(describing: p2), fallback: #"Those %d bananas belong to %@."#)
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants