NSLocalizedString in Swift: Shorten It and Use Modern Alternatives

NSLocalizedString in Swift: Shorten It and Use Modern Alternatives

Written by

in

Quick answer: NSLocalizedString works in Swift unchanged — you just drop the parameters that have defaults and pass the comment: NSLocalizedString(“key”, comment: “comment”). If you want something shorter, wrap it in a String extension (“key”.localized) or use the Swift-native String(localized:) on iOS 15 and later.

Why this happens

NSLocalizedString is a C function that Swift imports directly, so it exists in Swift exactly as it does in Objective-C. Nothing was replaced or renamed. What confuses people is the generated interface, which shows the full signature with every parameter spelled out:

func NSLocalizedString(
    _ key: String,
    tableName: String? = nil,
    bundle: Bundle = .main,
    value: String = "",
    comment: String
) -> String

The = default you see in Xcode’s generated interface is not something you type — it is how the interface file marks “this parameter has a default value”. tableName, bundle and value all have defaults, so you can omit them. comment does not, which is the only reason the call still feels long.

One more source of confusion: older snippets write #comment:. That is Swift 2 syntax for an external parameter label. Since Swift 3 the label is simply comment:, and the call has not changed since.

How to fix it

1. Just omit the defaulted parameters

The shortest correct call is the key plus the comment:

let title = NSLocalizedString("welcome_title", comment: "Greeting on the home screen")

That looks up welcome_title in Localizable.strings inside the main bundle. If the key is missing, the key itself is returned, because value defaults to an empty string.

If you keep your strings in a different table, pass only that:

let title = NSLocalizedString("welcome_title", tableName: "Onboarding", comment: "Greeting on the home screen")

The comment is not decoration. It is what genstrings and Xcode’s string extraction write into the .strings or .xcstrings file as a translator note, so keep it short and descriptive.

2. Add a String extension for a .localized property

If typing the function name bothers you, wrap it once and use a property everywhere:

import Foundation

extension String {
    var localized: String {
        NSLocalizedString(self, comment: "")
    }

    func localized(comment: String) -> String {
        NSLocalizedString(self, comment: comment)
    }
}

With "Hi" = "Вітаю"; in Localizable.strings, both of these work:

myLabel.text = "Hi".localized
myLabel.text = "Hi".localized(comment: "Shown on the welcome card")

This is the most popular approach because it reads well and needs no other changes to your project. Two caveats:

  • The extension applies to every String in your app, including user-generated text and API responses. Calling .localized on a value that is not a key just returns that value unchanged, which hides typos instead of failing loudly.
  • A computed property cannot carry a comment, so the version without arguments passes an empty comment. Use the method form when the comment matters for translators.

If you prefer not to extend String, Foundation has the same lookup at the bundle level:

let title = Bundle.main.localizedString(forKey: "welcome_title", value: nil, table: nil)

3. Use String(localized:) on iOS 15 and later

Swift gained a native initializer that does the same lookup and returns a String:

let title = String(localized: "welcome_title", comment: "Greeting on the home screen")

It also handles interpolation, which NSLocalizedString cannot do without String(format:):

let greeting = String(localized: "Hello, \(name)", comment: "Personal greeting")

The interpolated part becomes a format placeholder in the catalog, so translators can move it around in languages with different word order. This is the cleanest option for new code targeting iOS 15 or later; keep NSLocalizedString if you still support older deployment targets.

4. In SwiftUI, Text localizes for you

Text takes a LocalizedStringKey, so a string literal is looked up automatically:

Text("welcome_title")

Use Text(verbatim:) when the string must not be localized, and pass an already-localized String when you built it yourself:

Text(verbatim: user.nickname)
Text("welcome_title".localized)

A common bug is passing a String variable into Text and expecting localization — a String uses the non-localizing initializer, so the key is shown raw.

5. Let a String Catalog collect the keys

In Xcode 15 and later, a .xcstrings String Catalog can replace hand-maintained .strings files. Keys used through NSLocalizedString, String(localized:) and SwiftUI Text are all picked up at build time, so you do not have to run genstrings yourself. Add the catalog to the target, make sure the languages you ship are listed in the project’s Info settings, and translate in the editor.

How to prevent it

  • Keep the comment: argument filled in for anything a translator could misread; empty comments are the main cause of bad translations.
  • Never call .localized on user content, network data or identifiers — only on keys you control.
  • Check that Localizable.strings (or the .xcstrings catalog) is a member of the app target, not just of the project.
  • Test with a pseudolanguage or a long-word language such as German to catch truncated labels early.
  • Prefer stable, descriptive keys (welcome_title) over English sentences as keys, so rewording the copy does not break the lookup.

FAQ

Does NSLocalizedString still work in Swift 5 and Swift 6?

Yes. It is not deprecated and the signature has not changed since Swift 3. Only the #comment: label from Swift 2 snippets is gone; use comment:.

Why does my localized string show the raw key instead of the translation?

The lookup failed and returned the key. Usually the key is misspelled, the string lives in a different table than the one you passed, or the .strings file is not in Copy Bundle Resources for the current target.

What is the difference between NSLocalizedString and String(localized:)?

Both perform the same bundle lookup. String(localized:) is the Swift-native version, supports string interpolation directly, and requires iOS 15 or later. NSLocalizedString works on every deployment target you are likely to support.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.