简体   繁体   English

在自定义表格视图单元格中本地化按钮和标签[swift]

[英]Localize buttons and label in custom tableview cell [swift]

I have a tableview with custom cells. 我有一个带有自定义单元格的tableview。 Each is made of several objects: buttons and texts. 每个对象都由几个对象组成:按钮和文本。 I have some problems localizing the buttons' content. 我在本地化按钮的内容时遇到一些问题。 I have my .strings file with key and translation text defined, for example 我有定义了键和翻译文本的.strings文件,例如

START="Start";

Calling 呼唤

cell.startButton.setTitle(NSLocalizedString("START", comment: ""), forState: .Normal)

Thus instead of having 'Start' as text button I have 'START', why? 因此,为什么没有“开始”作为文本按钮,却有了“开始”,为什么? Of course the text is not localized because I always read 'START'. 当然,该文本未本地化,因为我总是读“ START”。

EDIT: I prepared a test app which shows the same behavior. 编辑:我准备了一个显示相同行为的测试应用程序。 The download link is HERE Someone can help? 下载链接在这里有人可以帮忙吗?

EDIT2: I found the error the string file MUST be called: 'Localizable.strings' I didn't know that... EDIT2:我发现必须将字符串文件称为错误:“ Localizable.strings”,我不知道...

The correct syntax for strings file is: 字符串文件的正确语法为:

"id" = "localized message";

In your case you have to write: 在您的情况下,您必须编写:

"START" = "Start";

whereas you are omitting the double quotes in the identifier: 而您省略了标识符中的双引号:

START = "Start"; // This is wrong

Side note: if you're doing a lot of localization, I recommend adding this string extension: 旁注:如果您要进行很多本地化,建议添加以下字符串扩展名:

extension String {
    var localized: String {
        return NSLocalizedString(self, comment: "")
    }
}

then you can make localization much simpler: 那么您可以简化本地化工作:

cell.startButton.setTitle("START".localized, forState: .Normal)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM