Skip to content

Instantly share code, notes, and snippets.

@eMdOS
Last active July 18, 2018 21:02
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 eMdOS/c8c1d99b6aa1007e281f576d5b620d0d to your computer and use it in GitHub Desktop.
Save eMdOS/c8c1d99b6aa1007e281f576d5b620d0d to your computer and use it in GitHub Desktop.
Localization (Protocol-Oriented Programming approach)

Examples

  1. Getting the signInButton value from Common.strings file.
Strings.Common.signInButton.localized
protocol Localizable {
var localized: String { get }
static var tableName: String { get }
}
// MARK: - RawRepresentable
extension Localizable where Self: RawRepresentable, Self.RawValue == String {
var localized: String {
return rawValue.localized(bundle: .main, tableName: Self.tableName)
}
static var tableName: String {
return String(describing: self)
}
}
extension String {
func localized(bundle: Bundle, tableName: String) -> String {
return NSLocalizedString(self, tableName: tableName, bundle: bundle, value: "**\(self)**", comment: "")
}
}
enum Strings {
// Mirror of: `Common.strings`
enum Common: String, Localizable {
case signInButton = "SIGN_IN"
}
// Mirror of: `Card.strings`
enum Card: String, Localizable {
case title = "TITLE"
case description = "DESCRIPTION"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment