NSLocalizedString help!

Discussion in 'iOS Programming' started by chipper1csu, Sep 16, 2011.

  1. chipper1csu macrumors newbie

    Joined:
    Sep 16, 2011
    #1
    Hi everybody! NOOB here. I've looked through these forums for issues similar to mine, but didn't see anything. Hoping somebody out there can help me.

    Here is my problem. I have an app that I am adding localization to. I have 3 files (en, es, fr) created with a number of keys and values in each.

    So, I run my app in the simulator...and everything works fine. I can see the expected text for each of my keys. However, if I quit the simulator...and run my app again, ONE of my key values is wrong (displays all kinds of crazy things, not the Key name or text)...or the app crashes when I call the NSLocalizedString() function for this specific key. I put in an entry for NSLog in my AppDelegate file, and the value is ok here. I also put in an entry in my data loading function, and the value is ok here as well. But if I put the same entry in the viewDidLoad function where I am using it, it crashes. Can anybody help explain what might be going on here? Is it possible that this key is somehow being released? How can it be that it works fine the first time through the simulator, but not the next? And why does it only happen to one of my localized strings? Any help would be much appreciated.

    The key in all 3 of my files looks like:
    "AKeyThatDoesNotWork" = "My Text Here";

    and my call that causes the incorrect text to show, or the app to crash:
    NSLocalizedString(@"AKeyThatDoesNotWork", @"")
     
  2. forum user macrumors regular

    Joined:
    Aug 28, 2008
    #2
    "NSLocalizedString(@"AKeyThatDoesNotWork", @"")" is a macro function that is converting a key into a value, it is not an object. Object get released, macros not.

    When you quit the simulator check that in xcode the debugger has stopped. The two can get out of sync sometimes. If the debugger is still active and you exited from your app in the simulator, strange things can happen.

    Still, after all, I assume you might have a typo in your Localized.strings file or the file is not UTF-16 format.
     
  3. chipper1csu thread starter macrumors newbie

    Joined:
    Sep 16, 2011
    #3
    Hey..thanks for the reply. That is kinda what I figured regarding keys, but I still don't understand why it is ok when the app first loads, but then is incorrect when I get to my viewDidLoad function.

    I double checked my simulator, and it is most definitely stopped.

    I also took your advice and checked out what UTF format the files were in. You were right, they were in UTF-8. I converted them, and it still didn't work. So, I created an entirely new project, created the new string files (in UTF-16) format, and added in only the one key that is causing me trouble. No luck. If I put in all the other keys (minus the one causing issues)...it works fine.

    The weird thing is that sometimes writing the key value to the log crashes the app, and other times it doesn't. For example, right now when I ran it...the value is "Dutch.lproj". I don't have a folder, or anything named that in my entire project...so I don't know where that is coming from.

    (I just ran another test, and it came up as "file://...Foundation.framework", it has also been "ro.lproj", among others). I tried changing the name of the Key, as well as the value...and still no luck. Ugh...this is frustrating.

    Thanks for taking the time to help me out!
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Are you using genstrings to generate your Localized.strings file, or at least the English version? Are you editing the keys or just the values in the two translations?

    You should probably delete the app in the Sim and device and do a clean before rebuilding and trying this again.
     
  5. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #5
    Not sure if this will help, but I have ran into many similar problems, (and other, not so similar) with my last project. It seems that some external libraries I used included their own localized files, so that there were 2 localizable strings files for the english language. Once I deleted the duplicates, all were fine.

    Just thought I should mention it.
     
  6. chipper1csu thread starter macrumors newbie

    Joined:
    Sep 16, 2011
    #6
    Soul & Phoney...thanks for the replies.

    Phoney...I don't use genstrings to create my files...and the only thing I edit is the key value. I actually have 3 files, "en", "es", and "fr". I did try changing both the key name, and value and replacing throughout my code...but that didn't work either. I did like you suggested and deleted the app from the sim, and cleaned all targets. Rebuilt and ran...and still the same.

    Soulstorm...where were the duplicate files located that you deleted? I looked in my en.lproj folder, and I only see the one Localizable.strings file.

    Again...thanks for taking the time to try and help me out!
     
  7. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    I don't see the point of using NSLocalizedString unless you also use genstrings.

    It doesn't make sense that you would edit the keys for the translations. The way that NSLocalizedString works is that the keys remain constant for all the translations. Only the values change.

    Are you passing a non-constant key to NSLocalizedString? IOW something like

    Code:
    NSString* somekey = ...;
    NSString* value = NSLocalizedString(somekey, @""); 
    Do you want to show us the actual code that is failing?
     
  8. chipper1csu thread starter macrumors newbie

    Joined:
    Sep 16, 2011
    #8
    I don't have very many text strings in my app that need to be converted, so I'm doing them all by hand. I don't edit the keys, they are the same throughout all my files. Only the text assigned to each one changes. Here is what they look like in each file:

    en.lproj -> Localizable.strings
    "MyTestKey" = "This is a test";

    es.lproj -> Localizable.strings
    "MyTestKey" = "This is a test spanish";

    fr.lproj -> Localizable.strings
    "MyTestKey" = "This is a test french";

    And the code it fails on is simply:
    NSLog(NSLocalizedString(@"MyTestKey", @""));

    I have another call to the log when I'm loading all my data...and it actually shows up correctly in the log. I got rid of all the other keys in each file, and this test key is the only thing left, and yet it still fails on me.

    I'm just so confused at this point...
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    What happens with this

    Code:
    NSString* s = NSLocalizedString(@"MyTestKey", @"");
    NSLog(@"%@", s);
     
  10. chipper1csu thread starter macrumors newbie

    Joined:
    Sep 16, 2011
    #10
    No dice. Still crashes when I try to write it to the log. And I commented out every other call for this key in the code, so writing to the log is the only place this key is used.

    :confused:
     
  11. forum user macrumors regular

    Joined:
    Aug 28, 2008
    #11
    Two suggestions:
    - not sure if this is important but I always use a comment and bracket the macro:
    Code:
    (NSLocalizedString(@"theKey",@"Some comment"))
     
    - your test strings are plain ascii only? That little I know of french and Spanish is that those language have extra characters. Copying & pasting them into Localized.strings might create problems. Typing them in directly inside xcode is ok (well, at least that never gave me problems )


    - Olaf
     
  12. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #12
    It's logical. Only one file with a given username should be in a folder in Finder. The duplicates were in my Xcode project. Open your Xcode project, and search for duplicate localizable strings files. If there are duplicates, then, as you saw, only one of them will be copied in the actual executable, and you can't know who.
     
  13. chipper1csu thread starter macrumors newbie

    Joined:
    Sep 16, 2011
    #13
    Well, I tried wrapping the parens around the NSLocalizedString call, and no luck there either. I am just using plain ascii text. I thought that some of the characters I had in the French file might have been throwing it off, but at this point I've removed all the other keys and values from my files (except for "MyTestKey" = "This is a test" ), and it still bails out on me.

    I also wasn't able to find any duplicate localized files in my XCode project.

    At this point, I'm just not sure what else to look at. It just doesn't make sense to me that with only one key in each file, it won't load correctly (after quitting the sim, and re-running the app). But if I add a second key and value, this loads just fine every time.

    Anyway...thanks again for the replies. I'll keep messin around with it, and hopefully get it workin!
     

Share This Page