据我所知,本地化Storyboard对象非常简单:你有一个看起来像这样的文件:
/* Class = "NSMenuItem"; title = "Superscript"; ObjectID = "Rqc-34-cIF"; */
"Rqc-34-cIF.title" = "Superscript";
/* Class = "NSMenuItem"; title = "Select All"; ObjectID = "Ruw-6m-B2m"; */
"Ruw-6m-B2m.title" = "Select All";
/* Class = "NSTextFieldCell"; placeholderString = "0"; ObjectID = "RxW-oS-gX3"; */
"RxW-oS-gX3.placeholderString" = "0";
/* Class = "NSTextFieldCell"; title = "0"; ObjectID = "RxW-oS-gX3"; */
"RxW-oS-gX3.title" = "0";
/* Class = "NSMenuItem"; title = "Jump to Selection"; ObjectID = "S0p-oC-mLd"; */
"S0p-oC-mLd.title" = "Jump to Selection";
/* Class = "NSButtonCell"; title = "Play"; ObjectID = "qjC-b0-dMe"; */
"qjC-b0-dMe.title" = "Play";
要本地化每个字符串,只需将右侧的文本替换为其本地化的等效项即可。很容易。
但是,如果代码显示标签或对话框,其中字符串是在代码中定义的,该怎么办?例如:
if (dialogOKCancel("Invalid Path", text: "The file/path you selected does not exist, is not a valid sound file, or cannot be accessed by the program.")) {
self.dismissController(self)
}
或
let result = dialogOKCancel("Discard Changes?", text: "Press Cancel to close this window and save your changes to \(filename), or OK to create a new file.")
我知道有很多不同的方法可以做到(根据应用程序检测到的本地化设置在启动时定义字符串是我想到的一件事),但我想知道是否有' sa '纠正' /官方的方式来做我想要完成的事情,或者如果不是,那么最好/最有效的方法是什么。
答案 0 :(得分:2)
旧的Objective C访问方式是:
NSString *name = "Craig";
NSString *msg = [NSString stringWithFormat:NSLocalizedString(@"Hello %@", nil),name];
Swift版本如下:
let name = "Craig"
let msg = String(format: NSLocalizedString("Hello %@", comment: ""), name)