How to deal with plural nouns in iOS project internationalization

How to deal with plural nouns in iOS project internationalization

The way to handle plural nouns in the internationalization process of iOS projects is somewhat different from the way to handle other texts. This article will explain how to handle plural nouns in the project from the following aspects.

  1. .stringsdict

[[399814]]

Go to the official account [iOS Development Stack] to learn more about SwiftUI and iOS development.

Wrong way of handling plural nouns

The internationalization of iOS projects mainly involves localizing the text used in the project. This aspect of knowledge has been covered in the previous articles, such as Apple's official iOS App internationalization workflow and using Xib to make the App internationalized.

Ordinary text can be roughly regarded as a one-to-one translation, for example "iOS development stack" -> "iOS developers stack", this one-to-one translation can be handled by .strings files.

The multilingual processing of plural nouns is a more complex translation, because different languages ​​have different ways of expressing different quantities. Take the English and Chinese expressions of the number of followers of the [iOS Development Stack] public account as an example:

For different expressions of different quantities in different languages, please refer to Language Plural Rules

If you want to use a .strings file to handle noun pluralization, you might write code like this:

  1. /* pseudocode */
  2. let fans = ...
  3. if fans == 0 {
  4. NSLocalizedString( "zero fans number of my offical account" , comment: "The Fans Number of [ios development stack] is zero" )
  5. } else if fans == 1 {
  6. NSLocalizedString( "one fans number of my offical account" , comment: "The Fans Number of [ios development stack] is one" )
  7. } else {
  8. NSLocalizedString( "other fans number of my offical account" , comment: "The Fans Number of [ios development stack] is other" )
  9. }

If every place where a noun is used is written like this, the project will be full of glue code, which is very difficult to maintain. To avoid this practice, we can use the .stringsdict file that comes with Xcode.

stringsdict

The .stringsdict file is a plist file specifically used to handle noun pluralization.

Create a .stringsdict file

  1. Localizable.stringsdict

stringsdict file parsing

On the left is an empty stringsdict file, and on the right is a stringsdict file using the number of public account followers as an example.

  1. Localized String Key is a text constant used in NSLocalizedString macro to match multi-language translations, such as NSLocalizedString("fans number of my official account", comment: "The Fans Number of 【ios开发栈】") . There should be as many Localized String Keys as there are plural nouns that need to be translated in the project.
  2. The Localized Format Key corresponds to sentences containing plural nouns, where the numbers and nouns are between %#@ and @.
  3. VARIABLE is replaced by %#@ and @. This is a key-value pair, and the content is the representation rule of VARIABLE in different quantities.
  • NSStringFormatSpecTypeKey specifies the type of plural rule, which is fixed as NSStringPluralRuleType to represent the plural rule type.
  • NSStringFormatValueTypeKey is used to represent different numeric types, for example, %d for integers and %u for unsigned integers. For details, see String Format Specifiers.
  • Zero / one / two / few, many / other There are different ways to express plural nouns in different quantities. Some languages ​​have only a few of them. For example, in Chinese, almost all quantities can be expressed in one way (1, 2, 100), and in English there are two (one fan / many fans). For more languages, please check Language Plural Rules.

Practical experience

  1. A Localized Format Key is a statement that can contain one or more plural forms, each surrounded by %#@ and @.
  2. For each plural noun that needs to be translated, a Localized String Key should be added.
  3. Set up multi-language for .stringsdict files

  1. Localizable.stringsdict
  2. the File inspector

The above are the translations of iOS development stack has n fans and iOS development stack has n articles and m shares in Chinese and English. The usage of these two translations is as follows:

  1. let fansNumLocalizableString = NSLocalizedString( "fans num in my offical account" , comment: "The Fans Number of [ios development stack]" )
  2. let fanNumStr = String.localizedStringWithFormat(fansNumLocalizableString, 3)
  3.  
  4. let offcialAccountInfo = NSLocalizedString( "official account info" , comment: "" )
  5. let accountInfoStr = String.localizedStringWithFormat(offcialAccountInfo, 10, 5)

Go to the official account [iOS Development Stack] to learn more about SwiftUI and iOS development.

Summarize

Through this article, we have learned why we need to handle the internationalization of plural nouns separately, and the usage of stringsdict files. I believe you have learned about the internationalization of plural nouns. Now let's try it.

<<:  Apple product system comprehensive upgrade IOS15 is coming

>>:  As 5G mobile phones become more popular, sales are declining. What’s the problem?

Recommend

6 good ways to clean up mobile phone garbage, 99% of people don’t know!

The phone memory is always insufficient, and clea...

How to build machine learning models using JavaScript

Currently, the main languages ​​for modeling in t...

How to make a good online event promotion plan?

In marketing psychology, herd mentality, greed fo...

How to place orders and bid on Juliang Qianchuan

Before the launch of Juleliang Qianchuan, the Dou...

Zhu Dan's Basic Sketching Course [General Quality]

Table of contents: 02 How to draw the folds of cl...

TCL plus BlackBerry tells us that 1+1 is likely to be less than 2

In the Chinese and even global mobile phone market...

How can we help our children become more courageous when facing failure?

This article is selected from the Hunzhi comic se...

Nature's GPS: How do animals use the natural world to navigate?

Millions of animals around the world follow the s...