Xcode 7 - /n vs. /u2028 in Strings within plist - ios

Having a weird issue that I can't seem to figure out. I have a plist that contains strings with line breaks. I'm using the Xcode plist editor. To perform the line break, I pressed option+return within strings where I want them, and it shows up fine in the editor.
The weird behavior:
When I have my app parse these strings, some of the strings will show with \n where I put the break, and other strings will show \u2028 instead. I understand from reading that these are both escapes, but I can't figure out why Xcode gives me one or the other. Is there a way I can force it to give me \n instead of \u2028? I'd rather not have to manually process the strings after-the-fact to change it
example:
"Hello\nWorld!" - I want and sometimes get this...
"Hello\U2028World!" - I don't want and sometimes get this...
Pulling my hair out. Thanks!
edit:
Here's how I'm getting the strings.
In .plist editor:
In the .plist, I have a dictionary. In the dictionary are the strings. They were "created and defined" in the editor.
In code:
//Get the config variables
NSString *path = [[NSBundle mainBundle] pathForResource:#"Variables" ofType:#"plist"];
vars = [[NSDictionary alloc] initWithContentsOfFile:path];
NSString *blah = [vars objectForKey:#"someString"];
NSLog(#"%#", blah);

Related

App localization showing the key instead of the value in iOS

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.

Localizing XML file parsed

I made an ios / cocoa app a few months ago and now I'm trying to localize it. I have already localized XIBs, Strings, etc. successfully.
The problem that I am facing now is that I am parsing an XML (XML-SAX) file to build a list and retrieve more information over the itens, like a tree of information.
The parser simply extract data from the lines.xml file. I tried to localize the file and change every string inside the the just created versions of lines.xml, but I can only see the original strings being displayed.
Please, let me know if you need any other information.
A little help here would be really appreciated.
Thanks.
I found some not so clever way to do so..... it works but I think it is ugly:
if([language isEqual: #"en"]){
s = [[NSString alloc] initWithFormat:#"lines-en"];
}else{
s = [[NSString alloc] initWithFormat:#"lines"];
}
NSString *path = [[NSBundle mainBundle] pathForResource:s ofType:#"xml"];

iOS: strings.xml equivalent in iOS app dev?

In the app I'm making I have a lot of huge strings. I don't want to hardcode these into my code because it makes the code unbearably messy. When I made a similar android app, it was a simple matter of declaring the string in strings.xml as
<string name="hello_world">Hello World!!</string>
and accessing it in the java file with
getString(R.string.hello_world);
How can I do something like this with iOS?
You could put them into a .plist file and load them using the following code (which assumes a file called strings.plist which has been copied into the app bundle):
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"strings" ofType:#"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
NSString *string1 = [dict objectForKey:#"string1"];
However if you want to internationalize your app, then using Apple's Internationalization and Localization technology might be something to look at instead.
EDIT: you'll want to load that file/dictionary only once and keep it around the entire app lifetime; don't use the above code for every string access!

iOS - NSLocalizedString is only returning the key string

I'm having some trouble debugging my NSLocalizedString implementation. Should be simple, but whatever I do, the function only returns the KEY string.
I'm using XCode 4.5 and iOS6, so I:
Added a new file called File.strings.
In my project settings I added English and Spanish as language settings.
Clicked "Make Localized" in the file inspector, and made sure that both English and Spanish options were selected, and also that the Target membership to my target was selected.
Added "KEY" = "TestEnglish"; to my english File.strings
Added "KEY" = "TestSpanish"; to my spanish File.strings
Added NSLog(#"Localization: %#\n", NSLocalizedString(#"KEY", nil)); to my .m file.
When I run the app, the value "KEY" is always displayed printed in the NSLog.
To jump into this a bit more, I tried this as well:
NSString *path = [[NSBundle mainBundle] pathForResource:#"en" ofType:#"lproj"];
NSString *str = [[NSBundle bundleWithPath:path] localizedStringForKey:#"KEY" value:#"" table:nil];
NSLog(#"Localization: %#\n", str);
and still the value "KEY" is printed, yet, path is a valid path.
Does anyone have any clue how to debug this? I feel like I've read every SO question/answer out there, but none of the suggestions help.
I realize that NSLocalizedString returns the KEY string when it cannot match a key, but I don't see how I can debug why my app might not be matching the KEY.
I've also deleted/cleaned the app about a dozen times.
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);
Rename the InfoPlist.strings file to Localizable.strings (double clic) and then you will get the correct string for that key.
In my case the issue was with the case of the string:
"bla.bla.blabla.BookSlot" whereas the Localizable.strings had it defined as "bla.bla.blabla.Bookslot"
So, double-check that the key string is in the correct case. Better yet, copy-paste.

stringWithContentsOfFile and initWithContentsOfFile return null after several runs

I am creating an iOS app which reads in a text file and displays the contents in a UIText field.
For the 1st three consecutive runs of thee app (Restarting a new session without exiting),
the data is read in fine. However on the fourth attempt, the data returned from the file is all nulls.
I've verified the file integrity. The issue exists when using stringWithContentsOfFile or initWithContentsOfFile.
After many hours of troubleshooting, I believe the issue is somehow related to a buffer being cleared within the above mentioned methods.
Any insight regarding this issue is greatly appreciated. I've tried many things with no luck.
Here's the code I use to read in the file:
TheString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:#"My_TextFile" ofType:#"txt"] encoding:NSUTF8StringEncoding error:NULL];
Here's the code I use to display certain contents of the file (The contents are placed in an array of type NSArray):
NSArray *My_Array;
My_Array= [TheString componentsSeparatedByString:#"\n"];
/* Obtain specific data to display */
DisplayedData = [My_Array objectAtIndex:M[l]-1];
:
:
/* Display the data in the view */
MyUITextView.text = DisplayedData;
/* Log the data */
NSLog(#"%#", MyUITextView.text);
On the 4th invocation of the code above, the data returned is blank and NSLOG is returning nulls
Thanks so much for any help!
Maybe I'm a little bit late with answer, but, anyway, maybe somebody will find it useful.
OK, I have also spent a day trying to figure out why my custom class for scrollable view is working 3 times and refuse at the 4-th time... I found that the problem has quite the same attributes as yours: nested NSString objects unexpectedly disappear. Though pointers point to the same address in memory, memory is already filled with quite arbitrary objects instead my NSStrings.
And I paid attention that I created these NSStrings using the following class method:
+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error
So, I'm not the owner of these NSStrings.
And I assumed that to be the owner can be a solution, so I created my NSStrings through alloc and
- (id)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error
instance method.
App was repaired!

Resources