Skip to content

Instantly share code, notes, and snippets.

public enum HttpMethod: String {
case options = "OPTIONS"
case get = "GET"
case head = "HEAD"
case post = "POST"
case put = "PUT"
case patch = "PATCH"
case delete = "DELETE"
case trace = "TRACE"
case connect = "CONNECT"
@damijanracel
damijanracel / StringExtensionForLocalization.swift
Last active October 9, 2018 10:42
String extension for localization
func localized() -> String {
let localizedString = NSLocalizedString(self, comment: "")
return localizedString
}
@damijanracel
damijanracel / LocalizationKeyEnum.swift
Last active October 15, 2018 08:10
Localization key enum
enum LocalizationKey: String {
case hello = "hello"
case bye = "bye"
case progress = "progress"
var string: String {
return rawValue.localized()
}
}
@damijanracel
damijanracel / LocalizationKeyEnumParameters.swift
Last active October 15, 2018 10:22
LocalizationKey enum with parameters
enum LocalizationKey {
case hello
case bye(String)
case progress(Int, Int)
var string: String {
switch self {
case .hello:
return "hello".localized()
case .bye(let name):
class LocalizedLabel: UILabel {
var localizationKey: LocalizationKey?
func set(key: LocalizationKey) {
localizationKey = key
text = key.string
}
}
@damijanracel
damijanracel / UILabelLocalizationExtension.swift
Created October 9, 2018 11:27
UILabel extension for localization
extension UILabel {
func set(key: LocalizationKey) {
localizationKey = key
text = key.string
}
private struct AssociatedKeys {
static var localizationKey = "nsh_localizationKey"
}
@damijanracel
damijanracel / LocalizationStruct.swift
Last active October 15, 2018 08:16
Localization struct
struct Localization {
let string: String
private init(string: String) {
self.string = string
}
static func bye(name: String) -> Localization {
return Localization(string: String(format: LocalizationKey.bye.string, name))
}
@damijanracel
damijanracel / Localizable.string
Created October 11, 2018 14:07
Localizable strings
"hello" = "hello";
"bye" = "bye %@";
"progress" = "Finished %d of %d";
@damijanracel
damijanracel / LocalizedLabel.swift
Created October 11, 2018 14:37
Localized label for struct
class LocalizedLabel: UILabel {
var localizationKey: LocalizationKey?
func set(key localization: Localization) {
self.text = localization.string
self.localizationKey = localization.key
}
}
@damijanracel
damijanracel / LocalizationStruct.swift
Created October 11, 2018 14:40
Localization struct upgrade
struct Localization {
let key: LocalizationKey
let string: String
private init(string: String, key: LocalizationKey) {
self.string = string
self.key = key
}
static func bye(name: String) -> Localization {