Last active
August 31, 2018 16:59
-
-
Save jalakoo/34152ea0f744498ff795648e30dc4f99 to your computer and use it in GitHub Desktop.
Simple localization category for Swift strings
This file contains 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
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