2

I have a tiny question regarding NSStrings. I'm currently making an app, the development language is English. I have a string like this:

NSLocalizedString(@"cancel", @"cancel string");

I have intentions of localising this app to Spanish afterwards so I need to do that. Now, the problem I have is that I need to compare this specific string to another string. So suppose I have this:

NSString *cancelString = NSLocalizedString(@"cancel", @"cancel string");

Can I just compare this string to the other one like this?:

[cancelString isEqualToString:@"cancel"];

And it will work fine? Or what will happen if the user is using my app in Spanish (so cancelString says "cancelar" instead of "cancel")? Does equalToString only compare to the base language of the app?

Hope I'm being clear.

Andy Ibanez
  • 12,104
  • 9
  • 65
  • 100
  • You should use NSLocalizedString only to get strings that are going to displayed in the user interface. So there should never be any reason to compare that localised string with anything. – gnasher729 Mar 20 '14 at 14:49

2 Answers2

4

Despite the fact that there's probably a better way to achieve the comparison you're trying to do here, you should compare against NSLocalizedString(@"cancel", @"whatever") because that will end up changing after the preprocessor runs and your 'isEqualToString' will only compare the two results of the methods (string against string).

NSString *cancelString = NSLocalizedString(@"cancel", @"cancel string");

[cancelString isEqualToString:NSLocalizedString(@"cancel", @"cancel string")];

should work

Ignacio Inglese
  • 2,605
  • 16
  • 18
2

You could try localizedCompare

NSString *cancelString = NSLocalizedString(@"cancel", @"cancel string");
bool areEqual = ([cancelString localizedCompare:@"cancel"] == NSOrderedSame);

Reference: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/localizedCompare:

plasmaTonic
  • 345
  • 2
  • 7
  • 22