Last active
April 17, 2019 11:48
-
-
Save ngoclt/6c70f203c927d2cec4c4e85031643dde to your computer and use it in GitHub Desktop.
A good way to manage localized string with swift
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(tableName: String = "Localizable") -> String { | |
return NSLocalizedString(self, tableName: tableName, value: "**\(self)**", comment: "") | |
} | |
} | |
protocol Localizable { | |
var tableName: String { get } | |
var localized: String { get } | |
} | |
extension Localizable where Self: RawRepresentable, Self.RawValue == String { | |
var localized: String { | |
return rawValue.localized(tableName: tableName) | |
} | |
} | |
enum LocalizableString: String, Localizable { | |
/// Invalid request | |
case invalid_request = "invalid_request" | |
/// Unexpected error | |
case unexpected_error = "unexpected_error" | |
var tableName: String { | |
return "Default" | |
} | |
} | |
/// Default.strings file | |
/** | |
"invalid_request" = "Invalid request"; | |
"unexpected_error" = "Unexpected error"; | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment