Skip to content

Instantly share code, notes, and snippets.

@jalakoo
Last active August 31, 2018 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jalakoo/34152ea0f744498ff795648e30dc4f99 to your computer and use it in GitHub Desktop.
Save jalakoo/34152ea0f744498ff795648e30dc4f99 to your computer and use it in GitHub Desktop.
Simple localization category for Swift strings
import Foundation
extension String {
// Example usage: "My Text".localized()
func localized() -> String {
return Bundle.main.localizedString(forKey: self,
value: nil,
table: nil)
}
// If using multiple localization files. Can specify a primary filename. Fallback to default bundle
// localization.strings if specified file not found.
func localized(fromFile: String) -> String {
let localizedString = Bundle.main.localizedString(forKey: self,
value: nil,
table: fromFile)
if localizedString == "" {
// If appLocalizationFilename was not set then it will check the main bundle twice... meh.
return Bundle.main.localizedString(forKey: self,
value: nil,
table: nil)
}
return localizedString
}
func localized(bundle: Bundle = .main, tableName: String = "Localizable") -> String {
return NSLocalizedString(self, tableName: tableName, value: "**\(self)**", comment: "")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment