Swift
NSLocalizedString
Localization
iOS Development
Programming

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.

swift
let title = NSLocalizedString("home_title", comment: "Home screen title")
print(title)

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:

text
"home_title" = "Home";
"settings_title" = "Settings";

French example:

text
"home_title" = "Accueil";
"settings_title" = "Paramètres";

When device language changes, the runtime resolves matching localized values automatically.

Modern Swift API Option

In newer Swift code, String(localized:) provides cleaner syntax.

swift
let greeting = String(localized: "welcome_message")
print(greeting)

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:).

swift
let format = NSLocalizedString("items_count", comment: "Number of items")
let text = String(format: format, locale: Locale.current, 5)
print(text)

String file entry:

text
"items_count" = "%d items";

Avoid manual concatenation because word order differs across languages.

SwiftUI Localization

SwiftUI can localize text keys automatically with LocalizedStringKey behavior.

swift
1import SwiftUI
2
3struct ContentView: View {
4    var body: some View {
5        VStack {
6            Text("home_title")
7            Text(String(localized: "welcome_message"))
8        }
9    }
10}

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:

swift
1enum L10n {
2    static let homeTitle = NSLocalizedString("home.title", comment: "Home title")
3    static let logoutButton = NSLocalizedString("settings.logout.button", comment: "Logout button")
4}

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:

  1. run app in at least two languages
  2. check long-text layout in constrained UI
  3. verify placeholders and formatting
  4. 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

  • 'NSLocalizedString remains 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.

Course illustration
Course illustration

All Rights Reserved.