Dynamic string with placeholders and multiple support in Swift (iOS)

I'm new to Swift and iOS programming, and I came across the question of dynamic placeholder strings and multiple support in Swift

For example, if you have a multilingual application and you do not want to hardcode strings, labels and messages, etc ...

I can easily do this in Android with the following code in XML files in different folders, where the Android device will search for strings according to the current locale:

<string name="name">You have %2$d new messages.</string> <plurals name="name"> <item quantity="one">You have a new message.</item> <item quantity="other">You have %2$d new messages.</item> </plurals> 

Using the above XML and the following Java:

 res.getString(R.string.name, mailCount) res.getQuantityString(R.plurals.name, mailCount, mailCount) 

The work is done in Android, I am looking for a solution corresponding to Swift (iOS)

+6
source share
2 answers

1: Localizations installation project, as shown in the screenshot, the project has English and Russian languages.

enter image description here

2: Create an empty property list file named Localizable.stringsdict and paste the following:

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>NumberOfMessages</key> <dict> <key>NSStringLocalizedFormatKey</key> <string>%#@ value@ </string> <key>value</key> <dict> <key>NSStringFormatSpecTypeKey</key> <string>NSStringPluralRuleType</string> <key>NSStringFormatValueTypeKey</key> <string>d</string> <key>one</key> <string>You have a new message.</string> <key>other</key> <string>You have %d new messages.</string> </dict> </dict> </dict> </plist> 

3: Localize the file above for each language, see the screenshot, the file is localized for English and Russian:

enter image description here

Using:

 String(format: NSLocalizedString("NumberOfMessages", comment: ""), 1) 

Conclusion: You have a new message.

 String(format: NSLocalizedString("NumberOfMessages", comment: ""), 2) 

Conclusion: You have 2 new posts.

Limit rules: In the XML code, pay attention to <key>one</key> and <key>other</key> , here you can use zero , one , two , few , many , other depending on the language and your requirements .

Use the following link for more information on multiple language-specific rules: http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html

+6
source

This blog post explains in detail what the plural is. with code examples for iOS.

in some languages, the plural does not work the same as in English. Some languages ​​do not indicate plurals (for example, Japanese), while for others, the word will change depending on the number (such as Russian).

To make things a little easier, iOS classified the various plural types as follows:

  • Zero. Used to indicate the quantity 0.
  • One. Used to indicate the quantity of exactly 1.
  • Two. Used to indicate the quantity exactly 2.
  • Few. Used to indicate a small amount greater than 2, but it depends on the language.
  • Many. Used to indicate a large number, but it also depends on the language.

Others. Used to indicate each number that is not described above the category.

Not all languages ​​need all categories, since all work differently. At first glance, it is that English will require rules for zero, one and many. However, multiple rules for the English language will be indicated on the other, since all numbers other than 1 are treated the same.

To specify multiple rules, you need to use the string dictionary (Localizable.stringdict) instead of the usual Localizable.strings file. In fact, you will need a .strings file, which will still be for .stringdict to work, even if it is empty.

In Xcode, create a new Plist file and name it Localizable.stringsdict. After filling in the raw data in Plist will look like this:

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>number_of_days</key> <dict> <key>NSStringLocalizedFormatKey</key> <string>%#@ value@ </string> <key>value</key> <dict> <key>NSStringFormatSpecTypeKey</key> <string>NSStringPluralRuleType</string> <key>NSStringFormatValueTypeKey</key> <string>d</string> <key>one</key> <string>%d day remaining</string> <key>other</key> <string>%d days remaining</string> </dict> </dict> </dict> </plist> 

And here's how to reference the plural in code:

 let format = NSLocalizedString("number_of_days", comment: "") let message = String.localizedStringWithFormat(format, numDays) 

Again, the loan goes to Quentin

+3
source

Source: https://habr.com/ru/post/987107/


All Articles