Localizing Pluralized Phrases

Pluralization rules vary by language, so it's not as easy as just adding an "s". To properly handle these cases, we need to use a Stringsdict file and format our string using a special syntax.

This is a companion discussion topic for the original entry at https://nsscreencast.com/episodes/499-localization-pluralized-phrases

For the Spanish case, couldn’t you have left the NSStringLocalizedFormatKey as it was & used the full text in each of the cardinality cases?

So your “zero” case in Spanish could have said “No has planeado ninguna comidas esta semana.” while the other would have their relevant full text “Has planeado %d comida esta semana.” and “Has planeado %d comidas esta semana.”

It looks like the intent is that you can have multiple replacement phrases in the final string, so the replacement variable can be used for the entire phrase as well as just a piece of it.

yeah that’s a possibility. I have found cases where the format string isn’t enough to specify the intent, so we end up with a conditional in the Swift code and 2 separate localized strings.

The hard part is knowing if this will be problematic in another language you aren’t familiar with :slight_smile:

At around the 5 minute mark when you are using String.localizedStringWithFormat, instead of using NSLocalizedString(…) as the first parameter you could move that to Localizations.swift too. You end up with something a bit easier to read…

infoLabel.text = String.localizableStringWithFormat(.localized.mealsThisWeek, mealsThisWeek.count)

Nice suggestion, I like it!