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

Toutiao Information Flow Luban Introductory Course [Basic Concepts] Tutorial

Chapter 1: Basic Concepts 1.1: What is informatio...

12 Fanstong case studies to unlock creative information flow routines!

In recent years, traditional home furnishing and ...

Exclusive "Douyin Live Streaming" DY Empowerment System Star

Course Contents: 1. Preparations for newcomers to...

Is iPhone XR really cost-effective? Here are 3 reasons to give it up

This week, the iPhone XR, which is known as the &...

Swallowing gold is a form of "suicide", why do some people still eat gold?

Many businesses will add some gold foil to foods ...

If the earth does not wander, where will humans go?

Excerpted from: "Inside and Outside the Clas...

How to enable night mode in earlier versions of Windows 10

Since the first appearance of Win10 system, there...

Douyin monetization 2020, quickly create a profitable Douyin

Whether it is operating a full-platform self-medi...

8 cases reveal the secrets of content marketing. Have you learned them?

Hi, Marketing Officers: As an editor of a marketi...

What is the "Sky Eye" looking at? Could it have received alien signals?

Guizhou Sky Eye is the nickname of the 500-meter ...