0

I want to localize "1 of 9", and 1 and 9 are int parameters, my code is as below

context = [NSString stringWithFormat:NSLocalizedString(@"%d of %d", 
          "This text will be used to show page number on the screen"), 
          currentPageIndex + 1, pageCount];

And the generated Localizable.strings show like that

/* This text will be used to show page number on the screen */
"%d of %d" = "%1$d of %2$d";

I think the thing on the left of "=" is key and the the thing on the right of "=" is value, but I want the key looks like "show_page_number" not included the format "%d", how can I do? I try to replace "%d of %d" with "show_page_number", but it does not work. Any advice?

wangshaoping
  • 83
  • 13

2 Answers2

1

If you want full control over the key and its initial value you need to use NSLocalizedStringWithDefaultValue instead of NSLocalizedString (which uses the key as the initial value).

Change your code to:

NSString *format = NSLocalizedStringWithDefaultValue(@"show_page_number", @"Localizable", NSBundle.mainBundle, @"%1$d of %2$d", @"This text will be used to show page number on the screen")
context = [NSString stringWithFormat:format, currentPageIndex + 1, pageCount];

When you run genstrings, you will get:

/* This text will be used to show page number on the screen */
"show_page_number" = "%1$d of %2$d";
rmaddy
  • 314,917
  • 42
  • 532
  • 579
1

NSLocalizedString() replaced key with value at runtime .So you can use anystring as key & it will be replaced as "%1$d of %2$d" at runtime. Add string in Localizable file :

"show_page_number" = "%1$d of %2$d";

& in code use that key

context = [NSString stringWithFormat:NSLocalizedString(@"show_page_number", "This text will be used to show page number on the screen"), currentPageIndex + 1, pageCount];

Add localizable.string file in xcode project.

EDIT

Ellen
  • 5,180
  • 1
  • 12
  • 16
  • I tried this, but it will show "show_page_number" on the screen. – wangshaoping Jul 10 '17 at 06:02
  • Please make sure you have added "show_page_number" = "%1$d of %2$d" in Localizable.strings files. Because i am using strings with format at various places in code & it is working fine. – Ellen Jul 10 '17 at 07:37
  • I wonder where do you put the Localizable file, are there any specific path I need to put then iOS can detect the string file? – wangshaoping Jul 10 '17 at 07:42
  • yes.Go into project settings. Select Project in left pane.Add Localizable file .Please see attached screenshot in EDIT section – Ellen Jul 10 '17 at 07:59
  • I am sorry but when I try to use the + add, the list file is null, not included the localizable.strings generated by genstrings. Do you know how to make iOS detect the Localizable.strings generated by command. – wangshaoping Jul 10 '17 at 13:43
  • Add your existing string file in en.lproj folder & rename it to Localizable.strings. – Ellen Jul 10 '17 at 14:09
  • I create a en.lproj folder in my iOS project and put the Localizable.strings in the en.lproj, but when I click + in the localization the list still gets empty. – wangshaoping Jul 11 '17 at 03:22
  • Have you imported Localizable.strings in app target.Either drag & drop OR From right click -> add Files to "Target". It should be added as bundle resources.Check in Build Phases -> Copy bundle resources section. Or refer apple document https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPInternational/LocalizingYourApp/LocalizingYourApp.html#//apple_ref/doc/uid/10000171i-CH5-SW1 – Ellen Jul 11 '17 at 04:42
  • Thank you so much and I know where my problem is, I use the default bundle but my Localizable.strings is not included in the Main Bundle – wangshaoping Jul 11 '17 at 09:13
  • This answer is going to cause big problems. The next time you run `genstrings`, the manual changes you made to Localizable.strings will be lost. This is why you need to use `NSLocalizedStringWithDefaultValue` as shown in my answer. – rmaddy Jul 11 '17 at 15:29
  • I am sorry but this is problem of genstrings which overwrites string file. NSLocalizedStringWithDefaultValue will pick the value from comments..which is not going to help for multi lingual translations. For auto generate string file, i think this link is best to follow https://stackoverflow.com/questions/16806682/ios-localization-updating-localizable-strings-with-just-new-strings – Ellen Jul 12 '17 at 05:30