Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get localized Cancel, Done and etc?

UIBarButtonItem have identifiers like Cancel, Done and some others. They are shown as text to user. If user changes language then for example Cancel button will be translated automatically. And as developer you do not need to provide localization string for this buttons. It means that Cancel, Done and other strings already localized and comes together with OS.

Is here a way to get this strings programmatically?

I do not want to add additional strings to localization files. And if it is possible to access then it would be very good.

like image 993
Ramis Avatar asked Aug 29 '12 15:08

Ramis


2 Answers

Here's a little macro I created to get the System UIKit Strings:

#define UIKitLocalizedString(key) [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] localizedStringForKey:key value:@"" table:nil] 

Use it like this:

UIKitLocalizedString(@"Search"); UIKitLocalizedString(@"Done"); UIKitLocalizedString(@"Cancel"); ... 
like image 151
Stephan Heilner Avatar answered Sep 28 '22 02:09

Stephan Heilner


One (admittedly questionable) way of accomplishing this easily is use Apple's framework bundle localizations directly:

To see what they have to offer, open the following directory via the Finder:

/Applications/Xcode/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks

And in your case, you'll subsequently open ./UIKit.framework/English.lproj/Localizable.strings (in TextMate). Here you see a wide variety of translations that Apple uses for things like "Print", "OK", "On", "Off", etc. The real magic is that there are about 35 different language translations that you can copy into your own Localizable.strings files, for free.

If you are incredibly brazen and don't mind putting your app's future stability in question, you could skip the whole "copy into your own Localizable.strings" process and go straight to the source programmatically:

NSBundle *uiKitBundle = [NSBundle bundleWithIdentifier:@"com.apple.UIKit"]; NSString *onText = uiKitBundle ? [uiKitBundle localizedStringForKey:@"Yes" value:nil table:nil] : @"YES"; NSString *offText = uiKitBundle ? [uiKitBundle localizedStringForKey:@"No" value:nil table:nil] : @"NO"; 

Caveat: In no way do I recommend that you actually access these localized resources programmatically in an app that you intend to submit to the App Store. I'm merely illustrating a particular implementation that I've seen which addresses your original question.

like image 32
Greg Combs Avatar answered Sep 28 '22 02:09

Greg Combs