Search code examples
ioscocoalocalizationnslocalizedstring

iOS strings file collision - Same string in source langage, different in target


So, I'm localizing an app from japanese to english.

In japanese, there is no distinction between (say) "Mr." and "Ms."(/"Mrs."), so I need to do something like this:

/* Salutation format for user (male) */
"%@様" = "Mr. %@";

/* Salutation format for user (female) */
"%@様" = "Ms. %@";

As you can see, the Japanese localization uses the same string in both cases. I was under the impression that, when the strings file contains more than one entry with the same 'source' (i.e., left side) string, the 'comment' was used to determine which one was employed. It turns out I was wrong, and the comment parameter is just an aid for human translators, totally ignored by NSLocalizedString().

So if I run the app, both of the code paths below produce the same string, regardless of the user's gender:

NSString* userName;
BOOL userIsMale;

/* userName and userIsMale are set... */

NSString* format;

if(userIsMale){
    // MALE
    format = NSLocalizedString(@"%@様", 
                               @"Salutation format for user (male)");
}
else{
    // FEMALE
    format = NSLocalizedString(@"%@様", 
                               @"Salutation format for user (female)");
}

NSString* salutation = [NSString stringWithFormat:format, userName];

So, how should I deal with a case like this?


Solution

  • Well, actually “left side” of the NSLocalizedString is a key. So you could do something like:

    NSLocalizedString(@"SalutForUserMale", @"Salutation format for user (male)");
    NSLocalizedString(@"SalutForUserFemale", @"Salutation format for user (female)");
    

    Then in your base *.strings file (Japanese I presume) you would have:

    "SalutForUserMale" = "%@様";
    "SalutForUserFemale" = "%@様";
    

    And in your English *.strings file you would have:

    "SalutForUserMale" = "Mr. %@";
    "SalutForUserFemale" = "Ms. %@";