简体   繁体   中英

Localize buttons and label in custom tableview cell [swift]

I have a tableview with custom cells. 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

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'.

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...

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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