UIBarButtonSystemItem localizable - iphone

How can I make UIBarbuttonItem localizable?
My implementation:
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(cancel)];
Originally I thought that it is automatic, because it looks easy to make it like this, but looks like not.
EDIT1: Official Apple dox says that cancel, done, edit, save buttons are localized, but not sure how to make it.

Alright, I think I know what's going on.
UIKit decides in which language to display strings, based on the value of [[NSBundle mainBundle] preferredLocalizations]. The idea behind this is to use translated resources whenever possible, falling back to English otherwise. For example, if you don't provide the translation of your app in Finnish and Finnish is selected in Settings->General->International->Language, your app will be in English. It may be a bit more complicated (UIKit may go through all the list of languages in the order displayed in Settings.app, trying to find the first language your app has translations for), but the point stands.
The above may be too obvious to miss a crucial nuance. The language determined by the above algorithm is used for the app's whole UI. For example, if the app bundle doesn't contain sk.lproj, nothing will be displayed in Slovak. In fact, it does make sense because otherwise some parts of UI would be in Slovak, other parts, in English.
Open the compiled app's bundle to see which *.lproj folders are there. The same set, sorted according to user preferences, will be returned by [[NSBundle mainBundle] preferredLocalizations]. All localizable strings, including system bar button items, will be displayed in one of those languages. If you don't support, for example, Russian, the whole UI will appear in another language, even if Russian is selected in Settings.app.
If this is the case with your app indeed, there are two right things and one wrong thing to do:
right: provide Slovak translation for each and every string displayed in your app, translate any text-containing nibs to Slovak too;
right: ignore Slovak altogether if you cannot (or don't need to) support it;
wrong: select any strings file or any nib, open Xcode's inspector, click "Add Localization…" (if the button is disabled, first click "Make File Localizable"), type "sk", click "Add" and build the project. This will make UIKit think your app is translated to Slovak, and system bar button items will automatically appear in Slovak when it's selected in Settings.app.
If you see a different behavior, there may be something wrong with the project/build/built app. For example, I noticed that when you make a file localized, its non-localized copy doesn't get deleted from the previously built app bundle.

Bump, but I think Vanya’s problem might be the “Localization native development region”/CFBundleDevelopmentRegion entry in Info.plist. If this is set to English and no localizations are explicitly made available as Costique explains, all system strings will be non-localized. But, set it to sk and - violà.

Not sure I understand your question correctly, but standard (system) bar button items are localized and thus automatically appear in the user-selected language. There are notable exceptions like UISwitch showing 0/1 instead of ON/OFF. What language are you having problems with?
That said, you can always use custom bar button items in place of system ones and provide necessary translations yourself. It's just overkill in most cases.

My implementation in Swift:
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneButtonTapped(button:)))
This uses the system default language for "Done" text.

Related

How to localize custom Done button

I use Done button in my custom view and I'd like to make its title localizable. I know that system Done button from UINavigationBar is already localized and it would be perfect to get it's localized strings somehow. Is there a way to do this?
Using the whole UINavigationBar only because of localized Done button seems to be inappropriate.
Clarification: the point is to use the same localized strings, that system uses.
There isn't an official way to get standard strings from the OS, but it's highly likely that all the strings will be in localized strings files, and most of those are present on the simulator (it won't have strings from apps that aren't present on the simulator, but should have almost all the strings from frameworks).
The simulator's framework directory is relative to your Xcode install directory and something like this (typing it from memory, change the version as appropriate):
Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/UIKit.framework/
To start with, I'd look in UIKit.framework/en.lproj/*.strings (or English.lproj or en_US.lproj). Strings files are plists and will generally be binary plists on device (such files start with "bplist00"). There are various ways to open these: TextWrangler will automatically display plists as XML, and the command-line tool plutil will convert to other formats. I use the bash alias
alias plist-dump='plutil -convert xml1 -o /dev/stdout --'
When you've found the string, you have a couple of options:
Load it directly from the framework at runtime with something like
[[NSBundle bundleWithIdentifier:...] localizedStringForKey:#"DONE" value:nil table:...]
This is not recommended. Yhere's no guarantee that it will work on a different OS version. (In practice, a string like "Done" probably won't move, but the alternative is easy enough that it's not worth the fuss.)
Copy the strings into your project. I don't know of a tool to automatically do this and merge with your existing Localizable.strings, but it probably exists somewhere and would not be too difficult to write. Slightly legally dubious (but not much more so than copying strings by eye for the languages you care about); I would definitely avoid doing it on a prerelease SDK to avoid concerns about Apple's NDA.
Copy the whole strings file into e.g. UIKit.strings and use something like NSLocalizedStringFromTable(#"DONE",#"UIKit",nil). Likely to be a copyright infringement!
In practice, I think Apple is unlikely to care about copying a handful of strings/images from iOS into an iOS app. Copying them into an Android app is another matter entirely...
Just add needed localization in project settings, all system stuff like done button will be localized by device locale if you added it, else it willbe on default language which you also should set up.
For custom button:
[self.myButton setTitle:NSLocalizedString(#"my localizable title", #"") forState:UIControlStateNormal];
It also valid for any NSString in your project.
NSString *someText = NSLocalizedString(#"my localizable title", #"");
self.myLabel.text = someText;
You should make your app localizable anyway by adding needed localizations.
After that just follow this instructions:
http://www.raywenderlich.com/2876/how-to-localize-an-iphone-app-tutorial
But I really recommend to you to do this in the end of project.

Steps for internationalisation in Xcode

I've been trying to add a Portuguese translation to my app. At the moment it's all in English. (well, it started that way).
I'm using a Storyboard for the main part of the UI. There are also a couple of additional xib files for reusable UI in table etc...
Anyway, I set out on the path of trying to internationalise the app and managed to convert the Storyboard to Portuguese.
I then tried to set up some strings (just a couple to begin with) to make translations of those also.
I've now got an storyboard that's only in Portuguese (I lost the English version) and none of the strings are being translated properly anyway.
I've set all the string back to just use #"blah" now (I'd put NSLocalizedString in a couple of places).
So I should be back to square one (once I fix the storyboard).
Anyway, is there a list of steps somewhere of how to go about making an app localised?
Any help appreciated.
There are couple of tutorials available - this is one of the good ones: http://www.raywenderlich.com/2876/how-to-localize-an-iphone-app-tutorial
This will definitely help you to get started real quick!
However, if you are on iOS6 xCode 4.5 you should also check the new features regarding localization. Apple now has also gon the route to have just one XIB (not like before multiple XIBs, one per language - which was impossible to maintain)
Probably the most important point is to always use NSLocalizedString, so there is no need for multiple XIBs. And then it's real easy to just add a new strings file for each language.
All you have to do then is in the Project Editor select your project -> select the Info tab on the right -> scroll all the way down -> there you find the localizations, press the little + on the buttom to add a new language. That's it.

Change iOS app's language on the fly

I'm writing an iOS app, where I want the user to be able to change the UI language independent of the iPhone's or iPad's language. The question is, how do I reload the appropriate NIB file for the currently displayed view when the language changes and how do I load the appropriate .strings file so that NSLocalizedString works appropriately?
This should do the trick, assuming that de is the new language selected by the user. Also assure that you are reinitiating the current view.
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"de", nil]
forKey:#"AppleLanguages"];
Personally, I don't like the idea of giving NIB files to translators. Instead, I design the NIBs so there is enough space for non-English languages (typically they will require 50% more room for text), and then I store all text resources in Localizable.strings files.
Then, in my code I set the text for each UI control.
[_myButton setTitle:NSLocalizedString(#"My button title",
#"My description for translation file, e.g. including chars limit")
forState:UIControlStateNormal];
I find this makes the translation process easier to manage.
To answer your original question, you would put this code in the viewWillAppear function of your UIView to ensure it is reloaded each time the UI reappears.

how to do localization for iphone

i got button....
displays on button
Search...
when i selected localization.....
i need to display
Zoeken
for search button its displays Zoeken...
#All thanks in advance.
You should look at NSLocalizedStringWithDefaultValue:
NSString *buttonTitle = NSLocalizedStringWithDefaultValue(#"KEY", nil, [NSBundle mainBundle], #"VISIBLE_DESCRIPTION", #"DEVELOPER_DESCRIPTION");
[aButton setTitle:buttonTitle forState:UIControlState...];
You'll then need to provide the relevant localization files in your project for the languages you intended on targeting.
As a top level skim, you can create per-locale NIB and string resource files using the built-in internationalisation capabilites.
However, this is quite a broad topic (there's an entire section of the Apple developer site dedicated entirely to internationalisation, complete with sample code, etc.), so what you need to do it read the documents there, look at the sample code and then ask a more targeted question if you get stuck/have a specific issue.

Localization of views

I am trying to make my app localized. I have followed this procedure http://www.switchonthecode.com/tutorials/a-simple-localization-example-for-the-iphone. And have deleted all localization and done it over again. I've seen other tutorials with exactly the same procedure.
My view to localize is named InfoView.xib.
I have:
rigth-clicked InfoView.xib->Get info -> Make File Localizable
added Localization and named it to "sv" (for Swedish language)
edited the sv-xib
When the simulator is set to English or any other language, I do get the xib loaded and can present it. But when setting the simulator to Swedish language, the view is not instantiated.
When doing
NSLog(#"Language: %# ",[NSLocale preferredLanguages]);
I do get "sv" as the top language code.
("NSLog(#"Locale: %# ",localeString);" gives "sv_SE")
When trying this
[[NSBundle mainBundle] loadNibNamed:#"InfoView" owner:self options:nil];
NSLog(#"InfoView in loadView %#", infoView);
It prints "(null)" when simulator is set to "Swedish" but instanstiate with any other language.
This is my setting if I do Get info on my sv-xib, in case it matters:
I have done the cleant targets and did reset the simulator.
What can the problem be? And how is the mapping done between the name I choose when adding a locale (sv) and the simultar/device language setting? Is it the acutal "sv" I named my locale to that must match the device's language code ("sv" is one)?
And 30s after I posted this a thought struck me; "Why don't I check if the localized view in IB is linked to the outlet in my File's Owner"...
It wasn't... Perhaps the tutorials should mention that the new localized xib must be linked as well. And really, why isn't this done automatically...?
"Thank you Nicsoft!"
"You're welcome!"