Where do I put text, instructions, description etc. in an iPhone program? - iphone

When an iPhone program has something like a paragraph or a set of instructions that need to be displayed on screen--basically anything that is longer than two sentences, where does it go?
Should it be stored as an NSLocalizedString (is this stored in the info.plist? I tried searching documentation--I'm a beginner and don't quite understand where that goes)?
Should it simply be hard-coded into the UITextView or UILabel that contains it?
Should it be defined as a constant then referenced in the UITextView/UILabel?
Should it be placed in a .txt file then referenced? (http://stackoverflow.com/questions/2506594/where-to-put-text-files-for-iphone-uitextview)

If you are thinking about localizing the text at all, I'd put it into a strings file:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/LoadingResources/Strings/Strings.html
You could also put it into a plist and read the plist file at runtime from the bundle. Generally try to keep the info.plist clean.

I would put such items in a plist file ad read it in.

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.

iPhone UI internationalization

When I do translation for web apps, usually I have a script that extracts the strings from the code to a .po file that then I give to the translator, and he has a neat tool that allows him to easily translate all the strings.
On iPhone, is kind of the same thing with the Localizable.strings when it comes to strings in the .m files.
My question is: for translating the UI (the XIB files), my translator will have to have a Mac in order to edit the XIBs or is there a way to extract all the string neatly out of the XIB into a more friendly file format ?
I'm thinking on re-initializing all the XIB elements that have string in viewDidLoad but looks to me like overkill ...
I have seen ppl use ibtool. This might help you.

iphone: How do I make my app support two or more languages?

I have my app with english version. I want it to run with french text also.
What steps I need to perform? or what API or extra code I will need?
Any examples or tutorial will help me more.
You need to add localizable strings for all the languages. And also you need to add the .lproj along with the localized file for each language you want to provide the support for.
Hope this helps you.
EDIT:
I have some of these links useful for you.
http://www.icanlocalize.com/site/tutorials/iphone-applications-localization-guide/
http://adeem.me/blog/2009/05/09/tutorial-iphone-localization-in-xib-nib-files/
How simplify iPhone localization?
http://www.raywenderlich.com/2876/how-to-localize-an-iphone-app-tutorial
I feel the last one which is from http://www.raywenderlich.com is the best one I would recommend.
Hope this helps you.
Thanks
The tutorials are good for starters but you should consider following points before you start:
"normal" localization where you localize XIB files is not really recommendable when the XIB might change, since you have to maintain multiple XIB files (for each language there will be a standalone XIB) Honestly this becomes a pain after a while
Therefore I suggest (even though it is more work at the beginning) to set the labels and button titles programmatically:
mylabel.text = NSLocalizedString(#"text:", #"text:");
[mybtn setTitle: NSLocalizedString(#"textbtn", #"textbtn") forState:UIControlStateNormal];
Then use: genstrings -o en.lproj *.m to create the strings file which will look for all those NSLocalizedString and create a file Localizable.strings in the folder en.lproj
If you follow this advice it will make live easier for you in the future - though it's not really 100% comfortable yet. If you need to add a new NSLocalizedString (eg because you have a new label) you need to create a completely new file Localizable.strings. Make sure you have a backup copy of this file where you have the translation, otherwise it gets overwritten and lost. I haven't yet come accross a good solution how to build up the strings to be translated...
ps there is no need to add localized XIB files anymore.. otherwise you end up having multiple XIB files which one wants to avoid in the first place...

What are UILabel's initWithCoder encoded keys?

I have a subclass of a UILabel that overloads initWithCoder and I was wondering if anyone has any documentation on how the coder is encoded so that I might be able to get information that comes from IB myself.
Thanks.
EDIT
Reason for doing this: I would like the font name given in the xib file. Apple's implementation of initWithCoder disregards the font name given in the file if it's a custom font and when you go to access the label's font, it returns the system font. Therefore I'd like to catch the font name before the original initWithCoder ignores it.
Looking through the documentation, there does not appear to be a way to retrieve all the keys used in an NSKeyedArchiver. One way "around" this is to archive your custom UILabel to an NSData object, write the data object out to a file, and then pop it open in a text editor and see if you can find some of the keys that way.
What information do you need from the UILabel that you can't access through its normal accessors?
If you look at the nib file compiled from the xib file for the interface you wanna decode, you'll be able to make out some of the keys you can use. It's kind of hard to find stuff, but it's better than nothing.

modifying plist file

i want to modify entries in custom plist file programmatically.
can anybosy suggest me a way to do that? i have tried modifying values of keys but file is not getting saved..
You don't provide much information as to what you've tried, but I assume from the tag that this is on the iPhone. In addition to Pat's suggestion, make sure that the plist you are trying to edit is not within your app bundle, but located within an editable location like Documents or Library within your sandbox.
I would look at the NSDictionary method:
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
I'm sure there's a similar method for NSArray.