Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a formatted localized string?

I have a localized string which needs to take a few variables. However, in localization it is important that the order of the variables can change from language to language.

So this is not a good idea:

NSString *text = NSLocalizedString(@"My birthday is at %@ %@ in %@", nil);

In some languages some words come before others, while in others it's reverse. I lack of an good example at the moment.

How would I provide NAMED variables in a formatted string? Is there any way to do it without some heavy self-made string replacements? Even some numbered variables like {%@1}, {%@2}, and so on would be sufficient... is there a solution?

like image 977
dontWatchMyProfile Avatar asked May 15 '10 19:05

dontWatchMyProfile


1 Answers

This is why NSLocalizedString takes two parameters. Use the second parameter to include a comment describing the native language meaning of the variables. Then, translators can reorder them using the $ + number construct. See Apple's Notes for Localizers.

However you cannot skip parameters in one language. For example, if you have 3 parameters in English and 4 in French and you don't need the third in English, you cannot format like %1$@ %2$@ and %4$@. You can only skip the last one.

like image 110
lemnar Avatar answered Nov 02 '22 21:11

lemnar