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:
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:
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:
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:
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:
Key Points
Below is a table summarizing the key points and differences when dealing with localization in Swift:
| Feature | Details |
| Function Call | NSLocalizedString("key", comment: "comment") |
| String Interpolation | Use String(format:) with placeholders for dynamic values |
| Custom Function | Create a helper function to streamline and organize localization logic |
| Pluralization | Handle 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.

