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

478 views Asked by At

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?

2

There are 2 answers

1
Ali On BEST ANSWER

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. %@";
0
BdR On

The Localizable.strings files are nothing more than key value lists. You are using the Japanese phrases as keys which is unusual but I guess it works. I assume this is because the original app was developed in Japanese(?). I usually use English phrases keys, but whatever.

The point is that if you want two different phrases in even just one translation you have to have two different keys in all your translations. If there is something in your base language that is not distinguished but in the translations it is, then you can just "split" an existing key in two new ones. Just change it slightly or add a number, see below:

/* english .strings file */
"hello_world_key" = "Hello World";
"Yes1" = "Yes"; // same in english but different values in german
"Yes2" = "Yes";

/* german .strings file */
"hello_world_key" = "Hallo Welt";
"Yes1" = "Ja";
"Yes2" = "Jawohl";