Cannot localise infoplist.strings - iphone

I tried to add a localised value to Info.plist using the recommended InfoPlist.strings
Do I need to keep the key also in Info.plist?
My Info.plist
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
My localized strings
en.proj
-> InfoPlist.strings
/* Localized versions of Info.plist keys */
CFBundleName = "ABC-EN";
it.proj
-> InfoPlist.strings
/* Localized versions of Info.plist keys */
CFBundleName = "ABC-IT";
If I keep the key in Info.Plist as above, in the code, a variable which should have the key value
_localisedName = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleName"];
is returning the string which is in Info.plist (myApp)
If I remove CFBundleName from Info.plist the string is < nil >
There is another setting which I am missing?
I have tried adding and removing both file from the copy bundle (without any change).

It was rather "complicated" to search for it, but I eventually find it out, I hope it will be useful for someone else
_localisedName = [[[NSBundle mainBundle] localizedInfoDictionary] objectForKey:#"CFBundleName"];
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/Reference/Reference.html#//apple_ref/occ/instm/NSBundle/localizedInfoDictionary
localizedInfoDictionary
Returns a dictionary with the keys from the bundle’s localized property list.
(NSDictionary *)localizedInfoDictionary
Return Value
A dictionary with the keys from the bundle’s localized property list (InfoPlist.strings).
Discussion
This method uses the preferred localization for the current user when determining which resources to return. If the preferred localization is not available, this method chooses the most appropriate localization found in the bundle.

You're not missing a setting, you're calling the wrong method. You need to call one of the localized string methods, such as:
_localisedName = NSLocalizedString(#"CFBundleName", nil);

Related

xcode NSArrayM insertObject:atIndex:]: object cannot be nil

I'm new to Xcode. what i want is read plist data locally in my program. i got
NSArrayM insertObject:atIndex:]: object cannot be nil error. Here is what i declared and my plist file. Thanks
NSMutableArray *phoneArray;
NSString *path=[[NSBundle mainBundle] pathForResource:#"PhoneList" ofType:#"plist"];
phoneArray= [[NSMutableArray alloc]initWithContentsOfFile:path];
key type
Item 0 Dictionary
Item 1 Dictionary
Items have description, name keys as type of String.
Verify your plist, there is most likely a bad value. Items in an NSArray or NSDictionary can not be null. You can use Xcode to open the plist file to verify it.
Personally I use "PlistEdit Pro", it can provide somewhat better diagnostics when the plist is invalid. There is a Free Trial here of PlistEdit Pro.
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index
The object to add to the array's content. This value must not be nil.
before any input from the plist file, please check to see that the element is not nil, to avoid the excetion

Loading text from a file

I am making an Iphone drinking card game app.
All the card mean something different and i want the user to be able to press an info button and then show a new screen with information about the current card. How can i make a document to load text from instead of using a bunch og long strings?
Thanks
You could look into plist files - they can be loaded quite easily into the various collection objects and edited with the plist editor in Xcode.
For instance, if you organize your data as a dictionary, the convenience constructor
+ (id)dictionaryWithContentsOfURL:(NSURL *)aURL
from NSDictionary would provide you with as many easily accessible strings as you need.
This method is useful if you consider your strings primarily data as opposed to UI elements.
Update:
As #Alex Nichol suggested, here is how you can do it in practice:
To create a plist file:
In your Xcode project, for instance in the Supporting Files group, select New File > Resource > Property List
You can save the file in en.lproj, to aid in localization
In the Property list editing pane, select Add Row (or just hit return)
Enter a key name (for instance user1) and a value (for instance "Joe")
To read the contents:
NSURL *plistURL = [[NSBundle mainBundle] URLForResource:#"Property List" withExtension:#"plist"];
NSLog(#"URL: %#", plistURL);
NSDictionary *strings = [NSDictionary dictionaryWithContentsOfURL:plistURL];
NSString *user1 = [strings objectForKey:#"user1"];
NSLog(#"User 1: %#", user1);
A plist, a JSON string, and an SQLite database walked into a bar ...
Oops!! I mean those are the three most obvious alternatives. The JSON string is probably the easiest to create and "transport", though it's most practical to load the entire thing into an NSDictionary and/or NSArray, vs read from the file as each string is accessed.
The SQLite DB is the most general, and most speed/storage efficient for a very large number (thousands) of strings, but it takes some effort to set it up.
In my other answer, I suggest the use of a dictionary if your texts are mostly to be considered as data. However, if your strings are UI elements (alert texts, window titles, etc.) you might want to look into strings files and NSBundle's support for them.
Strings files are ideally suited for localization, the format is explained here.
To read them into you app, use something like this:
NSString *text1 = NSLocalizedStringFromTable(#"TEXT1", #"myStringsFile", #"Comment");
If you call your file Localizable.strings, you can even use a simpler form:
NSString *str1 = NSLocalizedString(#"String1", #"Comment on String1");
A useful discussion here - a bit old, but still useful.

Localization of header files iOS

Is it possible to localize a header with macros in Xcode?
Lets say for English i might want a size of a font to be 17.0f, but 13.0f for Spanish.
Can this be done?
You can anyway put a PLIST file (let's say "constants.plist") in your .lproj localized folders (put the PLIST file aside you Localizable.strings files, in en.lproj/fr.lproj/es.lproj/...).
The PLIST may then contain an NSDictionary of key/value pairs for each value you need to customize according to the user locale (e.g. your font size).
Then you can use:
NSString* plistPath = [[NSBundle mainBundle] pathForResource:#"constants" ofType:#"plist"]; // will return the path of the plist in the right language-specific .lproj directory)
NSDictionary* constants = [NSDictionary dictionaryWithContentsOfFile:plistPath];
float fontSize = [[constants objectForKey:#"fontSize"] floatValue]; // or whatever key you use in your plist for this constant
This is then very easy to have a different constants.plist for each language of your app.
Figured it out. What I asked for is not possible. The header files are evaluated during build time but the localization is set during run time.

Localization of a variable value on iOS

I have around 72 html files in my resource folder and I need to localize them. Now I have translated them in french. A file selection depends on user input so file name is created by a variable. Now the problem is how to localize the value of a variable.
For example, I have following 3 files in resource folder.
AAAAA0.html
BBBBB28.html
CCCCC33.html
I also have these files with in french in resource folder.
AAAAA0-French.html
BBBBB28-French.html
CCCCC33-French.html
Here is my code that is working fine without localization.
// ViewController.m File
appDelegate2=[[[UIApplication sharedApplication] delegate] retain];
NSString *getSign2=[appDelegate2.globalString stringByAppendingString:appDelegate2.globalindex];
NSString *filePath=[[NSBundle mainBundle] pathForResource:getSign2 ofType:#"html" ];
If user input causes the selection of AAAAA0.html then globalString will be "AAAAA",globalIndex will be "0" and getSign2 will Be "AAAAA0".
If user input causes the selection of BBBBB28.html then globalString will be "BBBBB",globalIndex will be "28" and getSign2 will Be "BBBBB28".
If user input causes the selection of CCCCC33.html then globalString will be "CCCCC",globalIndex will be "33" and getSign2 will Be "CCCCC33".
Now I already have a Localizable.string file and my .xib files are already localized(all labels,images and datepicker etc.).
I want to change the value of getSign2 from AAAAA0.html to AAAA0-French.html if user language is french. Similarly it should change to BBBB28-French.html and CCCC33-French.html for BBBB28.html and CCCC33.html respectively.
What should I add in localizable.string to do this? What change should I make in ViewController.m file?
I know I can use following
NSLocale *locale=[NSLocale currentLocale];
NSString *currentlocale =[locale displayNameForKey:NSLocaleIdentifier value:[locale localeIdentifier]];
NSLog(#"Complete Locale: %#",currentlocale);
if (currentocale==#"French") {
NSString *getSignNew=[getSign2 stringByAppendingString:#"-French"];
NSString *filePath=[[NSBundle mainBundle] pathForResource:getSignNew ofType:#"html" ];
}
But this method check conditions in ViewController.m file while I have done all other localization related coding in localizable.string. Will it cause problem when I will submit my application? If this is not the correct solution then please tell me how to fix this problem.
Please reply as soon as possible.
Thanks.
Localized file should be placed in the structure like this:
Your.app/
English.lproj/
AAAAA0.html // English localized file
....
French.lproj/
AAAAA0.html // French localized file
...
zh_TW.lproj/
AAAAA0.html // Chinese localized file
...
and then -pathForResource:ofType: will automatically look up the correct file matching the current locale.

Localization of strings in static lib

I have a project that uses a static library (SL). In that SL, there are a couple of strings I'd like to localize and the project includes all of the localization files. The localization works just fine when storing all text translations in the same file. The thing is that I'd like to separate the SL strings from the other strings. I have tried to put two different *.strings files (Localizable.strings and Localizable2.strings) in the language folder of interest but that did not work. I have also tried to use two *.strings file with the same name (Localizable.strings) but with different paths. It didn't work either. It seems that only one localization file is supported, right? Could anyone suggest a good way of doing this? I'm using SDK 3.2 beta 2.
It is not possible to bundle it in a static lib, but you can create new bundle like "MyStaticLibraryName.bundle", put inside all localizations and use the code below instead "NSLocalizedString()". All you need to do: add a static library and resource bundle.
NSString *MyLocalizedString(NSString* key, NSString* comment) {
static NSBundle* bundle = nil;
if (!bundle) {
NSString* path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"MyStaticLibraryName.bundle"];
bundle = [[NSBundle bundleWithPath:path] retain];
}
return [bundle localizedStringForKey:key value:key table:nil];
}
Putting files with the same name intro one project never works, because in the resulting app they end up all in the same location. (Xcode doesn't preserve your directory structure.)
But you can put part of your localization into Localizable2.strings and then use:
NSLocalizedStringFromTable(#"key", #"Localizable2", #"")
Make the localizable string for the static library, then place that string file in a folder "YourLibraryResource".
Rename the folder "YourLibraryResource.bundle".
Now you include this bundle also in the project along with the library. Then use the code given by abuharsky.