Skip to content

Instantly share code, notes, and snippets.

@ngoclt
Last active April 17, 2019 11:48
Show Gist options
  • Save ngoclt/6c70f203c927d2cec4c4e85031643dde to your computer and use it in GitHub Desktop.
Save ngoclt/6c70f203c927d2cec4c4e85031643dde to your computer and use it in GitHub Desktop.
A good way to manage localized string with swift
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