How to use NSLocalizedString function with variables in Swift?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
NSLocalizedString retrieves translated strings from .strings files for localization. When the translated text includes dynamic values like a username, count, or date, you combine NSLocalizedString with String(format:) to insert variables into placeholders (%@ for strings, %d for integers, %.2f for floats). Swift 5.5+ also provides String(localized:) which supports string interpolation directly. Proper use of format specifiers and .stringsdict files for pluralization ensures translations work correctly across all languages.
Basic NSLocalizedString with Variables
NSLocalizedString returns the localized format string. String(format:) substitutes the placeholders with the provided values. The comment parameter helps translators understand the context.
Format Specifiers
| Specifier | Type | Example |
%@ | String (NSObject) | "Alice" |
%d | Int | 42 |
%f | Double/Float | 3.14159 |
%.2f | Float (2 decimals) | 3.14 |
%ld | Long Int | 1000000 |
%% | Literal % | % |
Positional Arguments for Reordering
Positional specifiers (%1$@, %2$@) allow translators to reorder arguments without changing the code. This is essential because word order varies between languages.
Pluralization with .stringsdict
.stringsdict files handle pluralization rules that vary by language. Russian has different forms for 1, 2-4, and 5+ items. Arabic has six plural forms. The .stringsdict file handles this automatically.
Swift 5.5+ String(localized:)
String(localized:) is the modern replacement for NSLocalizedString. It supports string interpolation and generates the .strings keys automatically from the interpolation pattern.
Common Pitfalls
- Using
%dfor large numbers on 64-bit systems: On 64-bit platforms,Intis 64 bits. Using%d(32-bit) with a SwiftIntcauses undefined behavior. Use%ldor%lldforIntvalues, or use%@withNSNumberwrapping.String(localized:)handles this automatically. - Hardcoding argument order without positional specifiers:
"From %@ to %@"assumes arguments always appear in the same order. Translators for languages with different word orders cannot reorder the arguments. Always use positional specifiers (%1$@,%2$@) in localizable strings. - Forgetting to add the comment parameter: The
commentinNSLocalizedString("key", comment: "...")is shown to translators to explain context. Without it, translators may misinterpret%@placeholders and produce incorrect translations. - Not using
.stringsdictfor plurals: Appending "s" conditionally (count == 1 ? "item" : "items") only works for English. Languages like Russian, Arabic, and Polish have multiple plural forms. Always use.stringsdictfor count-dependent strings. - Testing only in English: Format strings that work in English may break in other languages where arguments are reordered or where number formatting differs (comma vs period for decimals). Test localization with at least one right-to-left language and one language with complex plural rules.
Summary
- Use
String(format: NSLocalizedString("key", comment: "..."), args)to insert variables into localized strings - Format specifiers:
%@(string),%d(int),%.2f(float),%ld(long int) - Use positional specifiers (
%1$@,%2$@) to allow translators to reorder arguments - Use
.stringsdictfiles for proper pluralization across languages - On iOS 15+, use
String(localized:)for cleaner syntax with string interpolation - Always provide meaningful comments for translators
Related reading
- How to use NSURLConnection to connect with SSL for an untrusted cert?
- How to use presentModalViewController to create a transparent view
- How to use pull-to-refresh in Swift?
- How to use pull-to-refresh in Swift?
- How to use putExtra and getExtra for string data
- How to use RecyclerView inside NestedScrollView?
- How to use Runnable in Monodroid for Android
- How to use SCNetworkReachability in Swift
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.