Graphics Programs Reference
In-Depth Information
For the More Curious: NSBundle's Role in
Internationalization
The real work of adding a localization is done for you by the class NSBundle . For ex-
ample, when a UIViewController is initialized, it is given two arguments: the name of
a XIB file and an NSBundle object. The bundle argument is typically nil , which is inter-
preted as the application's main bundle . (The main bundle is another name for the applica-
tion bundle - all of the resources and the executable for the application. When an applica-
tion is built, all of the lproj directories are copied into this bundle.)
When the view controller loads its view, it asks the bundle for the XIB file. The bundle, be-
ing very smart, checks the current language settings of the device and looks in the appropri-
ate lproj directory. The path for the XIB file in the lproj directory is returned to the
view controller and loaded.
NSBundle knows how to search through localization directories for every type of re-
source using the instance method pathForResource:ofType: . When you want a
path to a resource bundled with your application, you send this message to the main
bundle. Here's an example using the resource file myImage.png :
NSString *path = [[NSBundle mainBundle] pathForResource:@"myImage"
ofType:@"png"];
The bundle first checks to see if there is a myImage.png file in the top level of the ap-
plication bundle. If so, it returns the full path to that file. If not, the bundle gets the device's
language settings and looks in the appropriate lproj directory to construct the path. If no
file is found, it returns nil .
This is why you must delete and clean an application when you localize a file. The previ-
ous un-localized file will still be in the root level of the application bundle because Xcode
will not delete a file from the bundle when you re-install. Even though there are lproj
folders in the application bundle, the bundle finds the top-level file first and returns its
path.
Search WWH ::




Custom Search