Localize strings more beautifully with an extension

September 29, 2017    Swift Blog

If you’ve worked in an App that has multiple languages you’d probably have learn to hate love Apple’s localization API NSLocalizedString. We just want a localize some strings but instead we are forced to provide a comment which in most cases we’ll just pass in nil.

Checkout the documentation here.

I’ve been using for a while a small String extension to shorten the wording while keeping the API flexible for different scenarios. Let’s go straight to the point and show some snippets with the implementation and usage.

Implementation:

public extension String {
  public func localize(tableName tableName: String? = nil, bundle: NSBundle = NSBundle.mainBundle(), comment: String = "") -> String {
    return NSLocalizedString(
      self,
      tableName: tableName,
      bundle: bundle,
      value: "",
      comment: comment)
  }
}

Usage:

Basic:

"MENU".localize()
"MENU".localize(comment: "Menu title")

Or if you have other Localizable.strings files:

"MENU".localize(tableName: "SomeOtherLocalizable")

Or if you are in a different target:

"MENU".localize(tableName: "SomeOtherLocalizableAndTarget", bundle: NSBundle(forClass: DummyClass.self))
...
private class DummyClass { }

In the last example I find it useful to just create another extension inside of the target were the Localizable.strings file is located.

extension String {
  func localizeTargetX(comment: String = "") -> String {
    return localize(tableName: "TargetXLocalizable", bundle: Bundle(for: DummyClass.self), comment: comment)
  }
}

private class DummyClass { }