Nicholas
Nicholas

Reputation: 1935

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

Upvotes: 0

Views: 2319

Answers (1)

Antonio
Antonio

Reputation: 72760

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)

Upvotes: 4

Related Questions