Skip to content

Instantly share code, notes, and snippets.

@dodikk
Last active April 1, 2016 16:39
Show Gist options
  • Select an option

  • Save dodikk/a1f6e66fc3e22b32402d577ab1f2bab5 to your computer and use it in GitHub Desktop.

Select an option

Save dodikk/a1f6e66fc3e22b32402d577ab1f2bab5 to your computer and use it in GitHub Desktop.
[localization] Plurals and strings
// Take %1$#@lu_pills@ at %2$@
NSString* format = NSLocalizedString(@"REMINDER_PREVIEW_FORMAT", nil);
NSString txtDate = @"5:42 PM";
NSString* result = [NSString localizedStringWithFormat:format
(unsigned long long)[reminder numberOfPills],
txtDate];
// Actual : Take 1 pill at (null)
// Expected : Take 1 pill at 5:42 PM
<?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>REMINDER_PREVIEW_FORMAT</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>Take %1$#@lu_pills@ at %2$@</string>
<key>lu_pills</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>lu</string>
<key>one</key>
<string>%1$lu pill</string>
<key>other</key>
<string>%1$lu pills</string>
</dict>
</dict>
</dict>
</plist>
@dodikk
Copy link
Author

dodikk commented Apr 1, 2016

    result = [result stringByReplacingOccurrencesOfString:@"(null)"
                                               withString:txtDate];

A very dirty workaround

@dodikk
Copy link
Author

dodikk commented Apr 1, 2016

Solution : types of integers must match.

%1$lu + unsigned long
%1llu + unsigned long long

// Take %1$#@lu_pills@ at %2$@
NSString* format = NSLocalizedString(@"REMINDER_PREVIEW_FORMAT", nil);

NSString txtDate = @"5:42 PM";

NSString* result = [NSString localizedStringWithFormat:format
                                 (unsigned long)[reminder numberOfPills],
                                 txtDate];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment