Swift
NSLocalizedString
localization
iOS development
SwiftUI

What's NSLocalizedString equivalent in Swift?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Localization is a crucial aspect of developing apps that cater to a global audience. It allows your app to adapt its user interface and content to different languages logically and consistently. In the world of iOS development, NSLocalizedString has long been a staple for this purpose in Objective-C. In Swift, we aim for the same effect, but with Swift's modern syntax and functionalities.

NSLocalizedString in Objective-C

In Objective-C, you typically use NSLocalizedString as follows:

objective-c
NSString *helloString = NSLocalizedString(@"Hello", @"A simple greeting");

This function takes two arguments: the key to be localized and a comment that serves as a reminder to whoever is translating the application about the purpose of the text. When the app runs, NSLocalizedString fetches the translation for the key based on the current locale.

Swift Equivalent

In Swift, the concept remains largely the same with some syntactical differences. Here's how you can use NSLocalizedString in Swift:

swift
let helloString = NSLocalizedString("Hello", comment: "A simple greeting")

However, Swift provides more advanced and flexible options for localization. Let's explore some of these options.

Using String Interpolation

Swift's string interpolation allows you to build strings incorporating various elements such as variable values, constants, and expressions. Here's how you use it in combination with localized strings:

swift
let filename = "document"
let errorString = String(format: NSLocalizedString("File not found: %@", comment: "Error message for missing file"), filename)

In this example, %@ is a placeholder in the localized string, which String(format:) replaces with the filename variable.

Custom Localization Function

To streamline the localization process in Swift, consider creating a custom function. This can reduce repetitive code and improve code readability:

swift
1func localizedString(forKey key: String, comment: String = "") -> String {
2    return NSLocalizedString(key, comment: comment)
3}
4
5let welcomeMessage = localizedString(forKey: "WelcomeMessage", comment: "Welcome message to the user")

This function can also simplify the process of adding additional features like logging missing keys.

Localizing Pluralized Strings

Localization can be further complicated by variables, such as when dealing with pluralized forms. Here's an example of how Swift handles pluralized strings using String.localizedStringWithFormat:

swift
let photoCount = 5
let photoString = String.localizedStringWithFormat(NSLocalizedString("You have %d photos", comment: "The number of uploaded photos"), photoCount)

Key Points

Below is a table summarizing the key points and differences when dealing with localization in Swift:

FeatureDetails
Function CallNSLocalizedString("key", comment: "comment")
String InterpolationUse String(format:) with placeholders for dynamic values
Custom FunctionCreate a helper function to streamline and organize localization logic
PluralizationHandle plural forms with String.localizedStringWithFormat

Additional Considerations

While NSLocalizedString is suitable for most purposes, larger applications might benefit from using a more powerful localization framework. For instance, .strings files are often managed by tools like ibtool or genstrings, but you might need more advanced table-driven solutions for some applications.

Third-Party Libraries

  • SwiftGen: It's a code generator that can generate Swift enums to ensure that your localization keys are correctly spelled and can help reduce runtime errors.
  • Lokalise: This provides a point-and-click web interface for localization management, which can further streamline localization tasks.

Conclusion

Swift retains the powerful localization capabilities present in Objective-C with NSLocalizedString, but it also extends them with modern Swift features. By understanding and utilizing these Swift-specific features, you can create a polished and more user-friendly app that appeals to a global audience. Proper implementation of localization strategies ensures your app is accessible in various languages, providing end-users with a tailored experience that feels native and intuitive.


Course illustration
Course illustration

All Rights Reserved.