Using plural strings without number
Asked Answered
B

4

7

I'm a bit confused by the .stringsdict documentation (scroll to "Localized Property List File").

Given a number of files, I want to show Save File or save Save Files accordingly. I thought the following would work, but doesn't.

In code:

NSString *string = [NSString localizedStringWithFormat:NSLocalizedString(@"%Save Files", @""), (long)files.count];

In Localizable.stringsdict:

<key>Save Files</key>
<dict>
    <key>NSStringLocalizedFormatKey</key>
    <string>Save %#@files@</string>
    <key>files</key>
    <dict>
        <key>NSStringFormatSpecTypeKey</key>
        <string>NSStringPluralRuleType</string>
        <key>NSStringFormatValueTypeKey</key>
        <string>ld</string>
        <key>one</key>
        <string>File</string>
        <key>other</key>
        <string>Files</string>
    </dict>
</dict>

Always shows Save Files, no matter the count.

What am I doing wrong?

Berkman answered 14/2, 2014 at 19:27 Comment(0)
C
3

The problem is that your code lacks the number of files saved. In order for a plural line to be localized using a stringsdict file, the line MUST have a number variables. So where it says @"%Save Files", it should say @"Save %ld File(s)". That %ld is the number required by Xcode to understand which plural rule to use at run time.

Then, in your Localizable.stringsdict file your plist must look like this:

<key>Save %ld File(s)</key>
<dict>
  <key>NSStringLocalizedFormatKey</key>
  <string>%#@files@</string>
  <key>files</key>
  <dict>
    <key>NSStringFormatSpecTypeKey</key>
    <string>NSStringPluralRuleType</string>
    <key>NSStringFormatValueTypeKey</key>
    <string>ld</string>
    <key>one</key>
    <string>Save %ld File</string>
    <key>other</key>
    <string>Save %ld Files</string>
  </dict>
</dict>

Problems like this can be really helped using a Localizable.stringsdict generator/tutorial like this one:

iOS Stringsdict Plurals Generator

It's also important that you leave as complete lines for the translator to rework the phrase. Instead of "Save %#@files@" give them the whole line to work with "%#@files". Why? Because the word "Save" in some languages may need to be conjugated depending on the number of files or might need to appear on the other side of the number (i.e. 3 files to save). Never assume English grammar will work and let the translators translate complete lines.

Congregational answered 20/5, 2015 at 19:2 Comment(1)
There are a few things you're mentioning which are not necessarily correct. First: You do not have to have "%ld" in your key, i often use keys like "TASK_EDIT_BUTTON" and it is working fine. Second: Your point with the grammar. You're correct that maybe the translator has an easier job, if he gets the whole line in plural types. But you do not need to do have that in that way to have the correct grammar in all languages. You can change the LocalizedFormatKey from language to language.Fullblown
N
2

Works if you remove "%" from NSLocalizedString(@"%Save Files", @"")

Nostology answered 12/4, 2017 at 3:26 Comment(0)
J
0

For anyone looking at this later in swift. Yes you can use plurals without a number like this:

let string = String.localizedStringWithFormat(NSLocalizedString("Save Files", comment: ""), files.count)

the output of this string will be:

File (for one)

Files (for more)

Janniejanos answered 10/3, 2023 at 14:57 Comment(0)
T
-1

You missed %ld inside NSLocalizesString:

NSString *string = [NSString localizedStringWithFormat:NSLocalizedString(@"%ld Save Files", nil), (long)files.count];

plist dict should be:

<key>%ld Save Files</key>
<dict>
    <key>NSStringLocalizedFormatKey</key>
    <string>%ld Save %#@files@</string>
    <key>files</key>
    <dict>
        <key>NSStringFormatSpecTypeKey</key>
        <string>NSStringPluralRuleType</string>
        <key>NSStringFormatValueTypeKey</key>
        <string>ld</string>
        <key>one</key>
        <string>File</string>
        <key>other</key>
        <string>Files</string>
    </dict>
</dict>
Torpor answered 28/3, 2014 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.