What's the equivalent of NSLocalizedString in Swift?


In real iOS applications, you often need to support localization to ensure the app's accessibility around the world. By incorporating localization into your app, you can gain more users. In Swift, we use the NSLocalizedString function to create a localized string.

What is localization?

Localization is the process of allowing various language support in your apps. Instead of utilizing the software in a single language, it helps to create a more localized experience for users. Localization will be quite simple to integrate in your application. Apple offers a totally native method for integrating localization in your program.

Syntax

The syntax for using NSLocalizedString in Swift is as follows.

NSLocalizedString("key", comment: "")

In the above syntax, there are two parameters inside the function body. Firstly, there is the key, which needs to be unique. Based on the key you provide in the function, the localized string will be searched within the strings file. Additionally, you can provide additional information about the particular string, which helps other developers to understand what the string does.

The comment argument in the NSLocalizedString function is a string that is used to provide additional information. This information can be helpful for translators and other developers.

As you can see the comment parameter is optional in the function provided. It is highly recommended, however, to pass the short information that provides context to the string being localized. For example, if the string is an error message, the comment could explain the situation in which the error message is displayed. If the string is a button label, the comment could explain what the button does.

NSLocalizedString

The NSLocalizedString method in Swift is used in applications to manage string localization. The string is looked up in the app's string table. This string table contains all of the strings in the project that need to be localized. If you need to add more copies to your project, just add them to the app's string table.

When you pass a string to this method, it looks for it in the app's string table (called a key). It returns to the caller once the specified key is found in the table. If the key is not found in the localization table, the same key is returned as a value.

This method is really simple to develop in order to enable many languages in the application. Each string copy must be placed in each file according to the languages that the code will support. Note that you do not need to write separate codes for each language.

Here is an example of how you can use the NSLocalizedString function to get the localized strings

The localized string for the key "enter_name" should be defined in the string table of the app. Here is an example of what the string table for English, Spanish, and French might look like

English

"enter_name" = "Enter your full name";

Spanish

"enter_name" = "Ingresa tu nombre completo";

French

"enter_name" = "Entrez votre nom complet";

In this example, the localized string is dependent on your preferred language. For example,

  • If you prefer English, NSLocalizedString will return "Enter your full name".

  • If you prefer Spanish, NSLocalizedString will return "Ingresa tu nombre completo".

  • If you prefer French, NSLocalizedString will return "Entrez votre nom complet".

In the NSLocalizedString function, you can also pass arguments to NSLocalizedString to insert dynamic values into a localized string.

The localized string for the key "greeting_message" should contain a placeholder for the user's name. Here is an example of what the string table for English, Spanish, and French might look like

English

"greeting_message" = "Good morning, %@ !!";

Spanish

"greeting_message" = "Buenos días, %@ !!";

French

"greeting_message" = "Bonjour, %@ !!";

Here is an example of how to insert dynamic values in the NSLocalizedString function

let message = NSLocalizedString("greeting_message", comment: "A message to greet the user")
let userName = "Amit"
let greeting = String(format: message, userName)

In this example, NSLocalizedString is being used to retrieve the localized string for the key "greeting_message". The comment "A message to greet the user" provides additional information about the string. The String(format:) method is then used to insert the userName value into the localized string.

In this example, the localized string is dependent on your preferred language. For example,

  • If you prefer English, NSLocalizedString will return "Good morning, Amit !!".

  • If you prefer Espanol, NSLocalizedString will return "Buenos días, Amit !!".

  • If you prefer French, NSLocalizedString will return "Bonjour, Amit !!".

Some of the key benefits of localization in an app include.

  • Improved User Experience − When you integrate localization into your applications, your users will enjoy a great experience. It helps to provide a more accessible experience to users, leading to better engagement and higher user satisfaction.

  • Increased Reach − By adopting the localization in the application, it will help to increase the numbers. An app can reach a wider audience, regardless of their language or location.

  • Competitive Advantage − By supporting many languages, an app may differentiate itself from competitors that only support a single language, and it can also appeal to customers who are seeking for applications that support their language.

  • Cultural Awareness − By supporting many languages, an app may demonstrate respect for diverse cultures while also assisting in the removal of linguistic barriers.

  • Enhanced SEO: − When an app is localized, it may be tailored for multiple locations, increasing its exposure in app stores and search results and making it simpler for consumers to locate.

Conclusion

In summary, NSLocalizedString is a powerful tool in Swift that enables developers to build apps that support multiple languages. By using NSLocalizedString to retrieve strings from the app's string table, developers can provide their users with a localized experience without having to change the app's code for each language. NSLocalizedString also allows inserting dynamic values into localized strings to further customize the user's experience. Whether you're developing an app for a global audience or simply want to support multiple languages in your app, NSLocalizedString is a tool you should have in your arsenal.

Updated on: 11-Apr-2023

363 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements