Skip to content

Instantly share code, notes, and snippets.

@vakhramoff
Last active September 16, 2023 03:40
Show Gist options
  • Save vakhramoff/1753e1d825771267a8d51576153b3a7c to your computer and use it in GitHub Desktop.
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
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