Automatically convert NSString to NSLocalizedString?

I'm still paying for iOS training, so please be kind. I have an iOS application containing about 400 NSString litterals. I never thought I'd want to localize this application later, so knowing about NSLocalizedString , I decided not to use them for my project.
Now the world has changed, and I need to localize this application. Is there any tool / script that I can use that will work through my .m files and โ€œsearch / replaceโ€ my NSStrings with NSLocalizedStrings before I extract them with genstrings?

Thanks Roger

+6
source share
5 answers

From the question and your comments, it seems that you have about 400 lines, of which 20 should not be localized. With this ratio, as you yourself say, changing them all and then reversing the change to 20 may make sense.

To do this, get TextWrangler or BBEdit and search and replace multiple file templates. You can limit your search to files ending in .m or .h . The task will be quick and easy, except for those 20 ...

NTN

+1
source

You made a mistake by not entering your code correctly for the first time, and now you need to pay the price.

You need to execute your program manually and change the user-visible string literals to calls in NSLocalizedString.

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

Always, ALWAYS use NSLocalizedString to create localized strings. These are just a few characters to enter, and it simplifies the internationalization of DRAMATICALLY code.

The good news is that the pain of this will be a bitter lesson, and you will most likely not make the same mistake.

+6
source

Yes! Find and replace regex will speed it up.

In the search bar, put:

 (".*") 

In the replacement line:

 NSLocalizedString($1,comment:"") 

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

So, go through your code and the ones you want to replace, just click "replace", this is a massive time-break!

+3
source

Usually you do not want to replace ALL NSStrings with NSLocalizedString , since not all rows are necessarily "user-facing". You may have string constants that are used internally that the user never sees and cannot be translated at all. Therefore, blindly replacing all NSStrings with NSLocalizedString is probably not a good idea.

A little manual work is being done, but this is a one-time effort - once you have done this, as soon as you know the correct way to handle any new lines accessing the user, and do it as you go. Having said that, there can be a very useful tool that handles this gracefully, but does not avoid manually selecting which lines to translate and which not.

+2
source

From what I learned and checked, there is no automated method to turn your strings into localized ones as you wish. But there is a plugin for Xcode called Lin that simplifies your process.

When you focus on NSLocalizedString or other functions to get a localized version of a string, Lin displays a localization list containing the key string entered.

Lin

+2
source

All Articles