Reputation: 8977
I have a bunch of the following line of code:
[NSString stringWithFormat:@"%@ and %@", subject.title, secondsubject.title];
[NSString stringWithFormat:@"%@ and %d others", subject.title, [newsfeeditem count] - 1];
and a lot more in the app. Basically I am building a news feed style like facebook where it has string constants. blah liked blah. Where/how should I do these string constants so it's easy to do for internationalization? Should I have a file just for storing string constants?
Upvotes: 0
Views: 299
Reputation: 299633
See the String Resources section of the Resource Programming Guide. The key section for this particular problem is "Formatting String Resources."
You'd have something like:
[NSString stringWithFormat:NSLocalizedString(@"%1$@ and %2$@", @"two nouns combined by 'and'"),
subject.title, secondsubject.title];
The %1$@
is the location of the first substitution. This lets you rearrange the text. Then you would have string resource files like:
English:
/* two nouns combined by 'and' */
"%1$@ and %2$@" = "%1$@ and %2$@";
Spanish:
/* two nouns combined by 'and' */
"%1$@ and %2$@" = "%1$@ y %2$@";
You need to be very thoughtful about these kinds of combinations. First, you can never build up a sentence out of parts of sentences in a translatable way. You're almost always need to translate the entire message in one go. What I mean is that you can't have one string that says @"I'm going to delete"
and another string that says @"%@ and %@"
and glue them together. The word order is too variable between languages.
Similarly, complex lists of things can cause all kinds of headaches due to various agreement rules. Some languages have special plural rules, gender agreements, and similar issues. As much as possible, keep your messages simple, short, and static.
But the above tools are very useful for solving the problem you're discussing. See the docs for more details.
Upvotes: 2