Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Localizable.swift
- // venezia
- //
- // Created by Alessandro Maroso on 19/04/2019.
- // Copyright © 2019 rawfish. All rights reserved.
- //
- import Foundation
- protocol Localizable {
- static func tableName() -> String
- var localized: String { get }
- }
- extension Localizable where Self: RawRepresentable, Self.RawValue == String {
- /// For a Localizable enum the default table name is the name of the enum enclosing class.
- /// If the enum is not enclosed in a class the table name will be the enum name.
- /// e.g.:
- ///
- /// class ExampleViewController: UIViewController {
- /// enum Strings: String, Localizable {
- /// case title = "title"
- /// }
- /// }
- ///
- /// In this case the corresponding filename will be ExampleViewController.strings
- static func tableName() -> String {
- guard let substring = String(reflecting: self).split(separator: ".").dropLast().last else {
- return String(describing: self)
- }
- return String(substring)
- }
- /// Returns the localized string corresponding to the case.
- /// e.g.:
- ///
- /// class ExampleView: UIView {
- /// enum Strings: String, Localizable {
- /// case title = "title"
- /// }
- /// }
- ///
- /// ExampleView.Strings.title.localized
- ///
- /// Will get the localized string with key "title" from the ExampleView.strings file
- var localized: String {
- return rawValue.localized(tableName: Self.tableName())
- }
- }
- extension String {
- func localized(bundle: Bundle = .main, tableName: String = "Localizable") -> String {
- return NSLocalizedString(self, tableName: tableName, value: "**\(self)**", comment: "")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement