Is there a way to implement localisations within CLI apps?


It looks like it's not possible to create a binary with localisation support for CLI apps. Are there plans to implement support for this natively, say like gettext? If not, is it possible to submit this as a request?


Out of curiosity, is it done anywhere else? I’ve never seen one before.

When SwiftPM’s localization support is released (SE‐0278), it will work for command line applications the same as anything else.

If you want to localize a command line tool before that comes out, you can use these packages of mine to do localization without separate resources:

Here is an example of a tool using it. (Try out the language switch in the top left.)

Thank you for the reply! I should have checked the proposals before I posted.

There is no urgency, I can wait. Your packages do look neat though.

Hello again. I've implemented localisations in my CLI tool, but it doesn't seem to work out-of-the-box. Normally I'd expect it to work like this:

NSLocalizedString("version ",
                  bundle: .module,
                  comment: "Version info") + "0.3",

But it requires me to add custom code to locate localisations like this:

let bundle = program.localizationBundle(
    forLanguage: Locale.current.languageCode ?? "en")
func localizationBundle(forLanguage language: String) -> Bundle? {
     if let path = Bundle.module.path(forResource: language,
                                      ofType: "lproj") {
         return Bundle(path: path)
     } else {
         return nil

And I need to implement the localised string like this:

NSLocalizedString("version ",
                  bundle: program.bundle ?? .module,
                  comment: "Version info") + "0.3",

Is this a Swift bug or am I doing something wrong?

Terms of Service

Privacy Policy

Cookie Policy