2017-07-07 53 views
0

我要本地化“的9 1”,1和9是INT参数,我的代码是如下Localizable.strings键值对

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

并将所产生的显示Localizable.strings那样

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

我认为“=”左边的东西是关键,“=”右边的东西是值,但我想要的键看起来像“show_page_number”不包括格式“%d “, 我能怎么做?我尝试用“show_page_number”替换“%d的%d”,但它不起作用。有什么建议?

回答

0

NSLocalizedString()替换为在运行时的值。所以关键可以用anystring关键&它将在运行时被替换为“%1 $ d%2 $ d”。 在本地化的文件添加字符串:

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

&使用该密钥

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

添加localizable.string文件中的Xcode项目。

EDIT

+0

我试过了,但它会在屏幕上显示“show_page_number”。 – wangshaoping

+0

请确保您在Localizable.strings文件中添加了“show_page_number”=“%1 $ d of%2 $ d”。 因为我在代码中的各个位置使用带格式的字符串,所以它工作正常。 – Ellen

+0

我想知道你在哪里放置Localizable文件,有没有什么特定的路径我需要把iOS可以检测到的字符串文件? – wangshaoping

0

如果要完全控制密钥及其初始值,则需要使用NSLocalizedStringWithDefaultValue而不是NSLocalizedString(它将密钥用作初始值)。

更改您的代码:

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]; 

当您运行genstrings,您将获得:

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

谢谢你救我的一天。 – wangshaoping