XCode本地化字符串Swift

5

我正在尝试本地化一个字符串列表。

我有一些照片,每张照片都有描述,我想将描述翻译成另一种语言。

这是代码:

var imageList:[String] = ["new_york_city_sky_house_skyscraper_59212_640x1136.jpg","79506M1pZO4U6d12i0Xzf27765gWo71P5061732uthh68xHQ8Dq1yTQ0Bj8p9F45.jpg","iphone 5 wallpaper new york.jpg","New-York-Vintage-Effect-iphone-5-wallpaper-ilikewallpaper_com.jpg","Superb-View-Over-New-York-iphone-5-wallpaper-ilikewallpaper_com.jpg","New-York-Empire-State-Building-1136x640.jpg","New-York-City-iphone-5s-wallpaper-ilikewallpaper_com.jpg","new-york-city.jpg","New-York-By-Day-iphone-5-wallpaper-ilikewallpaper_com.jpg","The-Empire-State-Building-New-York-1136x640.jpg"]

var nameList:[String] = ["1","2", "3","4","5","6","7","8","9","10"]

var sentMessage: [String] = ["Description here 1", "Description here 2", "Description here 3", "Description here 4", "Description here 5", "Description here 6", "Description here 7", "Description here 8", "Description here 9", "Description here 10"]

我希望你能翻译namelist类别和sent message类别,就像这样:"Description here 1"变为"Descrizione qui 1","Description here 2"变为"Descrizione qui 2"...

我想知道该怎么做。我正在自学如何创建应用程序,因为我有一些想法,但我在这里卡住了......谢谢!

1个回答

4
你可以在项目信息中添加所有本地化内容。文件是一个“键/值”数组。在你的代码中,你可以使用Objective-C调用。
NSLocalizedString(key:tableName:bundle:value:comment:)

var description = String(format: "%@ %d", arguments: NSLocalizedString("descriptionHere", comment: ""), 2)

这里是一个带有本地化功能的Swift项目。 http://rshankar.com/internationalization-and-localization-of-apps-in-xcode-6-and-swift/


我不知道我是否做对了。我已经更改了之前写的sentMessage:var sentMessage = String(format: "%@ %d", NSLocalizedString("descriptionHere", comment: "Description here"), 2)但是我遇到了一个错误: 'NSInteger'在这里无法转换为'String.Index':@IBAction func showView(){ let vc = SecondViewController(nibName: "SecondViewController", bundle: nil) vc.sentMessage = sentMessage[imageIndex] showViewController(vc, sender: self)我该怎么办? - MatteoAB
%@ %@?我尝试了,但是它给了我相同的错误。在 "Localizable.string" 中放入 "descriptionHere = "Description here";" 是正确的吗? - MatteoAB
GOOD_MORNING="Guten Morgen"; 就像在网址演示中一样。 - houguet pierre
textLabel.text = NSLocalizedString("GOOD_MORNING", comment: "早上好")也许是您的参数imageIndex。尝试不直接使用textLabel.text = NSLocalizedString("descriptionHere", comment: "此处描述"); - houguet pierre
你是否按照这个教程 http://rshankar.com/internationalization-and-localization-of-apps-in-xcode-6-and-swift/ 进行操作了?并且下载了示例,你会发现示例项目可以正常运行。 - houguet pierre
是的,我已经尝试了很多次了,大概两天来一直在努力,但是我无法将它应用到我的项目中。 - MatteoAB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接