Skip to content

Instantly share code, notes, and snippets.

@brkphp
Created April 11, 2019 11:57
Show Gist options
  • Select an option

  • Save brkphp/843893f0181f193f594c3e9930147a84 to your computer and use it in GitHub Desktop.

Select an option

Save brkphp/843893f0181f193f594c3e9930147a84 to your computer and use it in GitHub Desktop.
swift easy localizable
//
// Localizable.swift
// AnimatedLivePaper
//
// Created by Burak on 4/11/19.
// Copyright © 2019
//
import Foundation
import UIKit
protocol Localizable {
var localized: String { get }
}
protocol StoryboardLocalizable {
var localizationKey: String? { get set }
}
extension String: Localizable{
var localized: String {
return NSLocalizedString(self, comment: self)
}
func localize(_ args: CVarArg...) -> String {
return String(format: self.localized,arguments: args)
}
}
extension UILabel: StoryboardLocalizable {
@IBInspectable public var localizationKey: String? {
get { return nil }
set(key) {
text = key?.localized
}
}
}
extension UIButton: StoryboardLocalizable {
@IBInspectable public var localizationKey: String? {
get { return nil }
set(key) {
setTitle(key?.localized, for: .normal)
}
}
}
extension UINavigationItem: StoryboardLocalizable {
@IBInspectable public var localizationKey: String? {
get { return nil }
set(key) {
title = key?.localized
}
}
}
extension UIBarItem: StoryboardLocalizable {
@IBInspectable public var localizationKey: String? {
get { return nil }
set(key) {
title = key?.localized
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment