maidi
maidi

Reputation: 3459

xcode: How to localize strings in code?

With localize I mean translate into different languages. I only found Instructions for localizing the storyboard but I also have strings that I set in Code. (I'm using Swift)

Upvotes: 1

Views: 1113

Answers (1)

rickster
rickster

Reputation: 126107

In short — see the NSLocalizedString function. This is also there in Swift... its declaration looks like this:

func NSLocalizedString(
    key: String,
    tableName: String? = default,
    bundle: NSBundle = default,
    value: String = default,
    #comment: String) -> String

The third through sixth arguments have default values, so you can call it like this:

NSLocalizedString("foo") // -> Maybe「フー」in your Japanese strings file?

Or like this:

NSLocalizedString("foo", comment: "metasyntactic variable")

Or with any other combination of arguments.

Check out the Internationalization and Localization Guide in Apple's documentation.

Upvotes: 3

Related Questions