Skip to content

Instantly share code, notes, and snippets.

@JasonCanCode
Created August 3, 2018 15:56
Show Gist options
  • Save JasonCanCode/ce65b38679141dbae4f7be0ad5291523 to your computer and use it in GitHub Desktop.
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.
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