Created
August 3, 2018 15:56
-
-
Save JasonCanCode/ce65b38679141dbae4f7be0ad5291523 to your computer and use it in GitHub Desktop.
Used to mark text for localization. Keys include the class using the text and the first 10 characters of the default text. Translators will thank you later.
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
import Foundation | |
/// Used to mark text for localization. Keys include the class using the text and the first 10 characters of the default text. | |
protocol Localizable {} | |
extension Localizable { | |
static func localize(_ text: String) -> String { | |
let caller = String(describing: Self.self) | |
let prefixIndex = text.index(text.startIndex, offsetBy: min(15, text.count)) | |
let prefix = (text[..<prefixIndex]).replacingOccurrences(of: " ", with: "_") | |
let key = "\(caller):\(prefix)" | |
return NSLocalizedString(key, value: text, comment: "") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment