質問

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

役に立ちましたか?

解決 5

From the question and your comments it seems you have around 400 strings only 20 of which should not be localised. With that ratio, as you yourself say, changing them all and then undoing the change for 20 can make sense.

To do this get TextWrangler, or BBEdit, and perform a multi-file pattern matching search and replace. You can confine the search to files ending in .m or .h. The task will be quick and easy, apart from those 20...

HTH

他のヒント

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.

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!

You generally don't want to replace ALL NSStrings with NSLocalizedString as not all strings are necessarily 'user facing'. You might have string constants that are used internally that the user never sees and these in general should not be translated. Hence, blindly replacing all NSStrings with NSLocalizedString is probably not a great idea.

There is a fair bit of work involved going through and doing this manually, but its a one-time effort - once you've done it once you'll know the correct way to handle any new user-facing strings and do it as you go. Having said that - there may very well be a tool out there somewhere that handles this elegantly, but there's no avoiding the manual picking which strings need to be translated and which don't.

From I have learned and checked out, there no automated method to turn your strings to localized one you wish. But there's a plugin for XCode called Lin, that makes your process easy.

When you are focusing on NSLocalizedString or other functions to get a localized version of a string, Lin shows the list of localizations that contains the inputted key string.

Lin

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top