Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically transforming NSString into NSLocalizedString?

I am still paying dearly for learning iOS development, so please be kind.
I have an iOS application containing around 400 NSString litterals. I never thought that I would want to localize this app later on, so while being aware of NSLocalizedString I decided to not use them for my project.
Now the world has changed and I need to localize this application. Is there any tool/script I can use that will run through my .m files and "search/replace" my NSStrings with NSLocalizedStrings before I extract them with genstrings?

Thanks Roger

like image 474
theremin Avatar asked Apr 23 '14 13:04

theremin


2 Answers

You made a mistake not writing your code correctly the first time, and now you have to pay the price.

You need to go through your program manually and change user-visible string literals to calls to NSLocalizedString.

Note that you do NOT want to globally change all string literals. Things like dictionary keys should not be localized.

Always, ALWAYS, use NSLocalizedString to create localized strings. It's only a few more characters to type, and it makes internationalizing your code DRAMATICALLY easier.

The good news is that the pain of doing this will serve as a bitter lesson and you likely won't make the same mistake again.

like image 124
Duncan C Avatar answered Nov 14 '22 23:11

Duncan C


Yes! A find and replace regex will speed up this up.

In the find bar put:

(".*")

In the replace bar:

NSLocalizedString($1,comment:"")

This will change "normalString" to NSLocalizedString("normalString",comment:"")

So go through your code and on the ones you want to replace just press replace, this is a massive timesaver!

like image 27
Pbk Avatar answered Nov 14 '22 23:11

Pbk