How to enable framework localization in macOS command line tool?

In a normal macOS .app package, I can get localized text from various frameworks, like

AVCaptureDevice.localisedName
or
NSError
's
NSLocalizedDescriptionKey
, as long as I have added the system's current language as a localization in Xcode. This amounts to an .lproj directory being added inside the app package's Contents/Resources directory.


But command line tools don't have any such directory.


So how can I get the same behaviour from those frameworks in a command line tool?

Replies

Weird. This just came up on Swift Forums, so I assumed it was a duplicate but that’s clearly not the case.

Personally, if I need to localise a tool I just embed the tool in an app-like structure and use all the standard bundle localisation techniques.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
It’s often useful to implement a tool by putting most of its logic in a framework, and just making the tool a thin front-end. This makes it very simple to localize, because you can use the same techniques to localize the framework as you would to localize an application. It also makes the logic in the tool more testable, because you can call it from unit tests built against the framework. Finally, it makes it easy to transition your tool into an application, because the logic can be cleanly separated from the front-end. The downside is that you have more than one thing to install wherever you’re going to use it.

As quinn said, you may be better off with an application structure, or possibly even an application: One approach might be to create a very simple application that has both a framework and a command line tool inside it. The framework would contain most of the logic, while the applciation and the tool would both be front-ends to that logic. The application, for example, could work via drag-and-drop, or present a window allowing you to perform some very common functionality, while the command line tool could provide a more complex interface.