Skip to content

Instantly share code, notes, and snippets.

@Juanpe
Last active July 31, 2023 11:42
Show Gist options
  • Save Juanpe/0ba09355229c0afa2ced6609735fd46c to your computer and use it in GitHub Desktop.
Save Juanpe/0ba09355229c0afa2ced6609735fd46c to your computer and use it in GitHub Desktop.
String extension to localize more easy way
extension String {
var localized: String {
return NSLocalizedString(self, comment: "\(self)_comment")
}
func localized(_ args: [CVarArg]) -> String {
return localized(args)
}
func localized(_ args: CVarArg...) -> String {
return String(format: localized, args)
}
}
/// How to use
// "hello".localized
// "hello %@! you are %d years old".localized("Mike", 25)
// "hello %@! you are %d years old".localized(["Mike", 25])
@PavelGnatyuk
Copy link

What do you do with the Xcode warning "All paths through this function will call itself"?

@Ksen17
Copy link

Ksen17 commented Jan 20, 2023

In order to deal with recursion warning from Xcode, I made a quik fix

import Foundation

extension String {

var localized: String {
    return NSLocalizedString(self, comment: "\(self)_comment")
}

func localized(_ args: [CVarArg]) -> String {
    return String(format: localized, args)
}

func localized(_ args: CVarArg...) -> String {
    return String(format: localized, args)
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment