I have an application that downloads localized strings from a server. It creates a DownloadedBundle/[lang].lproj directory in application's cache directory and saves the downloaded Localizable.strings file there. Then it creates a NSBundle using the DownloadedBundle directory as the path, and loads the resources from that bundle.
Some code snippets, omitting the actual downloading part:
// Returns the path of the downloaded bundle:
- (NSString*) downloadedBundlePath {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString* path = [paths objectAtIndex:0];
return [path stringByAppendingPathComponent:@"DownloadedBundle"];
}
// Returns the downloaded bundle:
- (NSBundle*) downloadedBundle {
if (downloadedBundle == nil) {
downloadedBundle = [[NSBundle bundleWithPath:self.downloadedBundlePath] retain];
}
return downloadedBundle;
}
// Returns a string from the downloaded bundle:
- (NSString*) localizedStringForKey:(NSString*)key {
return [self.downloadedBundle localizedStringForKey:key value:nil table:nil];
}
// Saves the downloaded strings file in the bundle:
- (void) saveDownloadedFile:(NSString*)downloadedFilePath inLanguage:(NSString*)language {
NSFileManager* fileManager = [[NSFileManager alloc] init];
NSError* error = nil;
NSString* lprojPath = [[self downloadedBundlePath]
stringByAppendingPathComponent:[language stringByAppendingString:@".lproj"]];
if (![fileManager createDirectoryAtPath:lprojPath withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@"Failed to create a directory at %@: %@", lprojPath, error);
return;
}
// Move the downloaded file to be the new strings file:
NSString* stringsFilePath = [lprojPath stringByAppendingPathComponent:@"Localizable.strings"];
if ([fileManager fileExistsAtPath:stringsFilePath]) {
if (![fileManager removeItemAtPath:stringsFilePath error:&error]) {
NSLog(@"Failed to remove the old strings file at %@: %@", stringsFilePath, error);
return;
}
}
if (![fileManager moveItemAtPath:downloadedFilePath toPath:stringsFilePath error:&error]) {
NSLog(@"Failed to move the new strings file from %@ to %@: %@", moveItemAtPath:downloadedFilePath, stringsFilePath, error);
return;
}
}
Disclaimer: This code is a simplified version of what I actually have and not tested in this form. But hopefully it gives some idea.