Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using plural strings without number

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?

like image 601
hpique Avatar asked Feb 14 '14 19:02

hpique


2 Answers

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.

like image 125
Localizer Avatar answered Nov 20 '22 09:11

Localizer


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

like image 26
nemissm Avatar answered Nov 20 '22 09:11

nemissm