I am trying to get a strings file table for use with NSLocalizedStringFromTableInBundle.
I am using this method:
+(NSBundle*)getBundleForLang:(NSString*)lang{
//get the path to the bundle
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"localizable" ofType:#"strings" inDirectory:nil forLocalization:lang];
NSLog(#"bundlePath = %#",bundlePath);
//load the bundle
NSBundle *langBundle = [[NSBundle alloc] initWithPath:[bundlePath stringByDeletingLastPathComponent]];
NSLog(#"langBundle = %#",langBundle);
return langBundle;
}
While it is working great on the simulator, when i try to use it on an iPhone device i get this NSLog:
2011-12-09 00:40:14.533 MyApp[12754:707] langBundle = NSBundle
</var/mobile/Applications/915E6BCB-EC44-4F1D-891B-EF68E2FA89C2/MyApp.app/he.lproj>
(not yet loaded)
Why isn't it loaded and where is the problem?
Thanks
Shani
Check the case of your file paths.
The simulator (by default) is not case sensitive, whereas the device is.
This could cause the simulator to successfully find the file, but the device to fail.
It's not an error. Strings bundle, such as en.lproj, does not include executable file. When you try to [bundle loadAndReturnError:], it will fail, and loadAndReturnError:'s document will tell you why.
Make sure the bundle contains a bundle identifier.
If it does not, despite the path is available, the resources can't load from there.
Related
I've been using localization in my app, but for some reason, some of the strings (not all of them) won't translate, I see the key instead the value. I've tried to check if the app finds the localization files by doing this:
NSString *enPath = [[NSBundle mainBundle] pathForResource:#"en" ofType:#"lproj"];
NSString *hePath = [[NSBundle mainBundle] pathForResource:#"he" ofType:#"lproj"];
NSString *ruPath = [[NSBundle mainBundle] pathForResource:#"ru" ofType:#"lproj"];
NSString *esPath = [[NSBundle mainBundle] pathForResource:#"es" ofType:#"lproj"];
NSString *frPath = [[NSBundle mainBundle] pathForResource:#"fr" ofType:#"lproj"];
NSString *arPath = [[NSBundle mainBundle] pathForResource:#"ar" ofType:#"lproj"];
And none of them is nil.
I've checked the name of the localization file and it's Localizable.strings as it should be.
Also checked if the key exists inside the Localizable.strings files and it does.
I've also tried:
Empty Cache
Cleaning all targets
Delete Derived Data folder
Restart
Reset simulator
Convert to UTF-16
Remove all localization files and recreate them.
Also tried to do everything that is in this question.
It's important to say that this is not just a Simulator/Cache problem. It's also showing on devices which download the app. (I have Enterprise account).
What more can I do in order to identify nor fix the problem?
So I found the problem, I guess who translated the Localizable.strings files for me is an asshole. In 4 places in my strings file there was a row as followed:
"KEY" ;= "Value"
This line cause some kind of a crash, but let the compiler to build successfully for some reason. That's why I couldn't find the bug, only when I decided to take the last Key and Value which are not translate and move them to the top of the Localizable.strings file. Then I was able to understand and see that the problem is somewhere in the middle of the file and the top Keys and Values are translated fine.
One thing that you can do catch these kind of errors is to make a copy of the strings file, change the extension to plist and try to open it in Xcode. If there is any problem in the strings file it will show in Xcode since the dictionary will contain only the keys till the point where there is an error. You can then do a Find operation and find the error until you are sure that all strings appear in the plist file. You can then rename the file back to .strings
If you specify table:nil, then NSBundle will try to fetch the localization from the default table (the one in SOMELANG.lproj/Localizable.strings). If you have the localization elsewhere, you should explicitly specify the table using table:#"File" (or use the NSLocalizedStringFromTable() macro in a similar manner:
NSString *value = NSLocalizedStringFromTable(#"key", #"File", nil);
Also,
Double check that the Localizable.strings file is being added to
Targets -> BuildPhases -> Copy Bundle Resources
It hadn't been added automatically for me.
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?
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]];
Suppose I have a file data.txt in the Resource bundle of an extension (not the Resource bundle of the application). How do I access it? I have tried:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"txt"];
But it returns nil.
It turns out all I need to do is to add this file to the Application's target, and not only the Extension's target.
Add your resource file to main app bundle which contains the extension as embedded binary and then call this method. It will return correct path.
I try to get resource from bundle inside framework. As you can see bundlePath is nil. But NSLog prints right path. And bundle is nil. Why?
Not sure why it's happening, but the code is inefficient given you get a bundle in order to allocate a new one. This is clearly unnecessary:
NSBundle *bundle = [NSBundle mainBundle];
NSURL *modelURL = [bundle URLForResource:... ];
I missed the framework-element when answering this question. As you are calling this code from within the framework code I assume that [NSBundle mainBundle] is returning the bundle of the framework and not the app.
The answer lies in the app providing the framework code with the app's bundle via some method. The framework code should then be able to access files within the app bundle.
I'm trying to implement a way to change language inside the application.
I think I have it all figured it out, but for some reason the folder is not laded as a bundle
I have a folder called kh.lproj
NSString *path = [[NSBundle mainBundle] pathForResource:newLanguage ofType:#"lproj"];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
bundle = [NSBundle bundleWithPath:path];
DLOG(#"Language: %# from path %# (%#)", newLanguage, path, bundle);
}
The output of my dlog is
Language: kh from path ......../F1AA1E74-A014-4331-BD1B-D05D0E54AFF3/console.app/kh.lproj
(NSBundle <......./F1AA1E74-A014-4331-BD1B-D05D0E54AFF3/console.app/kh.lproj>
(not yet loaded))
On both the iPhone and in the simulator (with different paths of course).
I have checked in the .app folder and the kh.lproj folder is there. (In lower case).
Does any one have an idea of why this is happening? If i try to load a file that doesn't exist it just ignores it and don't try to load it.
EDIT
If its to any help, when I'm using loadAndReturnError:(NSError) i get the message:
NSLocalizedFailureReason=The bundle’s executable couldn’t be located.,
NSLocalizedDescription=The bundle “kh.lproj” couldn’t be loaded
because its executable couldn’t be located.,
NSBundlePath=......./A65E8399-6CDB-4CAE-9074-803125E78BBA/storeconsole.app/kh.lproj
What does this error message mean
Looks like I get what I want anyway when calling
[bundle localizedStringForKey:key value:NULL table:nil];
a possible pitfall is also, when the string file has a bug, like a missing semicolon or incomplete comment marker.
Xcode dont check the file for syntax errors.