Last active
July 31, 2023 11:42
-
-
Save Juanpe/0ba09355229c0afa2ced6609735fd46c to your computer and use it in GitHub Desktop.
String extension to localize more easy way
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
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
What do you do with the Xcode warning "All paths through this function will call itself"?