-1

My Previous Code without Localization. It worked perfect.

case LOGIN_LOGOUT: ((Cell*)cell).lbl.text = [self isLoggedIn] ?   
[NSString stringWithFormat:@"Logout %@", email]   
:NSLocalizedString(@"Login", @"Message");
 break;  

But when I implement Localization in Logout the email will not show.

case LOGIN_LOGOUT: ((Cell*)cell).lbl.text = [self isLoggedIn] ?   
[NSString stringWithFormat:NSLocalizedString(@"Logout", @"Message") ,"%@",   
email] :NSLocalizedString(@"Login", @"Message");
break;  

I know I am missing some basics in stringWithFormat but can anyone offer some guidance to me?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    I think you are better off localising the complete phrase as there could be a different word order in some languages (or other, more complicated issues). – trojanfoe Mar 10 '16 at 14:25
  • 1
    Everything is good (if that's the whole phrase, not part of bigger phrase), just add object format specifier (%@) to your .strings entry. – MANIAK_dobrii Mar 10 '16 at 14:38
  • @MANIAK_dobrii like below answer?? –  Mar 10 '16 at 14:39

2 Answers2

1

You are looking up the localisation of "Logout". You are using that as a format string. That's not likely to work. Don't make statements that are too complex, it makes it impossible to debug.

I'd write

case LOGIN_LOGOUT: {
    NSString* labelText; 
    if ([self isLoggedIn]) {
        NSString* formatString = NSLocalizedString(@"Logout", @"Message");
        labelText = [NSString stringWithFormat:formatString, "%@", email];
    } else {
        labelText = NSLocalizedString(@"Login", @"Message");
    }

    ((Cell*)cell).lbl.text = labelText;
    break;
}

And now you can actually debug that whole mess. The stringWithFormat parameters look very, very dodgy.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
1

Let's assume, that you have .strings file and it contains entry named "Logout". You have:

[NSString stringWithFormat:NSLocalizedString(@"Logout", @"Message") ,"%@", email]

here you try to load format string via NSLocalizedString and use it with NSString. That means that you have to put correct format string into your .strings file, so, if currently you have:

"Logout" = "Logout";

In order to make it just like before localization, you need:

"Logout" = "Logout %@";

If you don't have a .strings file or don't have entry named "Logout", NSLocalizedString will return the key, i.e.

NSLocalizedString(@"key", @"comment") // returns "key"

That means, that your NSLocalizedString(@"Logout", @"Message") may return "Logout" if NSLocalizedString can't find correct entry in your .strings file.

There are more things that may go wrong, if you want some deeper insides on that, I have written great article on the whole topic: Understanding iOS internationalization.

Also I'd suggest to use +localizedStringWithFormat: instead of just plain +stringWithFormat:, because the former uses current locale.

MANIAK_dobrii
  • 6,014
  • 3
  • 28
  • 55
  • dude it's not working my app crash. -[NSKeyedUnarchiver initForReadingWithData:]: data is NULL –  Mar 10 '16 at 15:04
  • Something is really messed up, I can't even guess what caused that error. The only way we can figure it out is if you provide your code in your question. – MANIAK_dobrii Mar 10 '16 at 15:07
  • And don't just copy paste the code, your problem is deeper than you think. You can't deal internationalization like that, it will bite you bad if you do. – MANIAK_dobrii Mar 10 '16 at 15:11
  • which types of problems will occur in future? Please tell me –  Mar 10 '16 at 15:13
  • @iosDev There are a lot of stuff to deal with, check the link I provided. – MANIAK_dobrii Mar 10 '16 at 15:14