NSBundle pathForResource doesn't work for subdirectory localization?

1.1k views Asked by At

I saw quite a few questions regarding [NSBundle MainBundle] pathForResource (using inDirectory or not), but my case seems different.

My problem is: Yes, it works fine for whatever files in subdirectory if without localization. For example, it returns the correct path for the file data/a/words.txt when I use

[[NSBundle mainBundle] pathForResource:@"words.txt" ofType:nil inDirectory:@"data/a"]

However, after I localized the words.txt, let's say the real path becomes: data/a/en.lproj/words.txt, then the above code cannot find the path anymore.

I checked the file in the .app package and the file has been copied into the correct path (data/a/en.lproj), it's just somehow the code cannot find it.

I'm using XCode 5.1.1

Isn't pathForResource supposed to find the text automatically?

1

There are 1 answers

0
thomers On

Try adding forLocalization to pathForResource, like this:

NSArray* availableLocalizations = [[NSBundle mainBundle] localizations];
NSArray* userPrefered = [NSBundle preferredLocalizationsFromArray:availableLocalizations forPreferences:[NSLocale preferredLanguages]];

NSString *indexPath = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"txt" inDirectory:@"data/a" forLocalization:[userPrefered objectAtIndex:0]];