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
NSLocalizedString is still the standard localization entry point in Swift and remains fully supported. Swift also offers newer localization APIs that fit better with modern SwiftUI and strongly typed workflows. The practical choice depends on your app architecture, localization tooling, and team conventions.
Direct Swift Equivalent
The simplest answer is that the equivalent is still NSLocalizedString.
It reads from Localizable.strings by default and behaves similarly to Objective C usage.
Localization File Structure
Typical project layout:
- '
en.lproj/Localizable.strings' - '
fr.lproj/Localizable.strings'
Example entries:
French example:
When device language changes, the runtime resolves matching localized values automatically.
Modern Swift API Option
In newer Swift code, String(localized:) provides cleaner syntax.
This is often preferred in modern codebases, especially where typed resource workflows are used.
Parameterized Localized Strings
For values that include numbers or user data, localize the format string and then interpolate with String(format:).
String file entry:
Avoid manual concatenation because word order differs across languages.
SwiftUI Localization
SwiftUI can localize text keys automatically with LocalizedStringKey behavior.
This is concise and works well with standard localization resources.
Organize Keys for Scale
As projects grow, key naming conventions become essential. Use namespaced keys such as home.title and settings.logout.button.
A typed wrapper helps avoid typos:
Centralizing keys also simplifies audit and cleanup.
Handle Missing Keys and QA
If a key is missing, apps may show the key itself in UI. Add checks in CI to detect missing translations and key drift across languages.
Practical QA steps:
- run app in at least two languages
- check long-text layout in constrained UI
- verify placeholders and formatting
- ensure no raw keys appear in screens
Localization quality is both language and layout correctness.
Pluralization and Advanced Cases
For plural rules, use string dictionaries instead of hand-coded singular versus plural branching. Many languages have plural categories beyond one and many. String dictionaries provide language-aware plural behavior and reduce localization logic bugs.
Also include translator comments for ambiguous strings. Context helps translators choose correct wording.
Build Pipeline Integration
Localization can regress silently when keys are renamed without updates. Add automated checks for key parity across language files and unused keys. Keep translator notes versioned with code changes. A small localization validation step in CI saves expensive post-release fixes.
Common Pitfalls
A common pitfall is assuming NSLocalizedString is obsolete in Swift. Another is hardcoding user-facing text and bypassing localization resources. Teams often concatenate translatable fragments in code, producing awkward grammar in many languages. Missing translator comments lead to incorrect phrasing for ambiguous keys. Finally, localization testing is postponed until late release stages, when layout issues are costly.
Summary
- '
NSLocalizedStringremains the direct and valid Swift equivalent.' - '
String(localized:)is a modern option for newer Swift code.' - Keep keys structured and centralized to reduce mistakes.
- Localize format strings, not assembled fragments.
- Use pluralization resources for count-based text.
- Add localization QA and CI checks to catch issues early.

