Decybel07 / L10n-swift

Localization of the application with ability to change language "on the fly" and support for plural form in any language.
MIT License
315 stars 32 forks source link

Wrong result/missing support for multiple arguments in plural function #16

Closed malcommac closed 5 years ago

malcommac commented 5 years ago

Hi guys, I would to use this lib but I had found there is not support for multiple arguments with plurals right?

For example, this is the value in Localizable.strings:

"__id_runs" = "%d runs";

and and Localizable.stringsdict:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>NSStringLocalizedFormatKey</key>
    <string>%1$#@lu_completed_runs@</string>
    <key>lu_completed_runs</key>
    <dict>
        <key>NSStringFormatSpecTypeKey</key>
        <string>NSStringPluralRuleType</string>
        <key>NSStringFormatValueTypeKey</key>
        <string>lu</string>
        <key>zero</key>
        <string>No runs completed yet</string>
        <key>one</key>
        <string>One %2$#@lu_total_runs@</string>
        <key>other</key>
        <string>%lu %2$#@lu_total_runs@</string>
    </dict>
    <key>lu_total_runs</key>
    <dict>
        <key>NSStringFormatSpecTypeKey</key>
        <string>NSStringPluralRuleType</string>
        <key>NSStringFormatValueTypeKey</key>
        <string>lu</string>
        <key>one</key>
        <string>run completed</string>
        <key>other</key>
        <string>of %lu runs completed</string>
    </dict>
</dict>
</plist>

This is the native call to use it:

let completedRuns = 1
let totalRuns = 2
let format = NSLocalizedString("__id_runs", comment: "")
let translatedString = String(format: format, completedRuns, totalRuns)

The result is correct: One of 2 runs completed

The same operation with L10n-swift ends with a wrong result because I can pass just one value as arg parameter for plural() function

let value = l10n.plural(for: "__id_runs", resource: nil, fittingWidth: nil, arg: 1, converting: { $0 })

which results in a wrong string: One %2$#@lu_total_runs@

Decybel07 commented 5 years ago

This is a bit complicated issue, but you're right. I do not support multiple arguments for the plural form. I will try to add this functionality in the next version