Last active
September 16, 2023 03:40
-
-
Save vakhramoff/1753e1d825771267a8d51576153b3a7c to your computer and use it in GitHub Desktop.
String extension which allows you to get a localized version of a string
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 { | |
func localized(language: String = Locale.current.languageCode!, comment: String = "") -> String { | |
guard let path = Bundle.main.path(forResource: language, ofType: "lproj") else { | |
return self | |
} | |
guard let bundle = Bundle(path: path) else { | |
return self | |
} | |
let localized : String? = NSLocalizedString(self, tableName: nil, bundle: bundle, value: "", comment: comment) | |
return localized ?? self | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment