6

I began integrating localization into my app using this guide. It worked great until I localized a string that included a dynamic variable. The original was this:

let myString = "I have \(countOfMoney) dollars in my wallet."

Then I tried to mimiic this stack answer to localize it. However, I'm getting an EXC_Bad_Access error. Below is how I tried to localize it.

This is in my Localizable.strings English file:

localizedMsg="I have %@ dollars in my wallet.";

This is in my View Controller:

let countOfMoney = moneyInWallet.count
let localizedMsg = String(format: NSLocalizedString("localizedMsg", comment: ""), countOfMoney)

However, this line shows up as an error when I run the app on the simulator. How do I fix it?

1

1 Answer 1

15

Your setup isn't correct. Your code should look like this:

let localizedMsg = String(format: NSLocalizedString("I have %d dollars in my wallet.", comment: ""), countOfMoney)

Now run genstrings to get your updated Localizable.strings file.

That will add the line:

"I have %d dollars in my wallet." = "I have %d dollars in my wallet.";

Also note the change from %@ to %d. This assumes that countOfMoney is an integer type. Only use %@ for object pointers.

Sign up to request clarification or add additional context in comments.

2 Comments

I just mimicked the form of your code and it worked, so thank you. What did you mean by run genstrings though?
genstrings is the tool that scans all of your source code for calls to NSLocalizedString and generates the Localizable.strings file for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.