SHARE
TWEET

Untitled

a guest Apr 19th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  Localizable.swift
  3. //  venezia
  4. //
  5. //  Created by Alessandro Maroso on 19/04/2019.
  6. //  Copyright © 2019 rawfish. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10.  
  11. protocol Localizable {
  12.     static func tableName() -> String
  13.     var localized: String { get }
  14. }
  15.  
  16. extension Localizable where Self: RawRepresentable, Self.RawValue == String {
  17.     /// For a Localizable enum the default table name is the name of the enum enclosing class.
  18.     /// If the enum is not enclosed in a class the table name will be the enum name.
  19.     /// e.g.:
  20.     ///
  21.     ///     class ExampleViewController: UIViewController {
  22.     ///         enum Strings: String, Localizable {
  23.     ///             case title = "title"
  24.     ///         }
  25.     ///     }
  26.     ///
  27.     /// In this case the corresponding filename will be ExampleViewController.strings
  28.     static func tableName() -> String {
  29.         guard let substring = String(reflecting: self).split(separator: ".").dropLast().last else {
  30.             return String(describing: self)
  31.         }
  32.         return String(substring)
  33.     }
  34.    
  35.     /// Returns the localized string corresponding to the case.
  36.     /// e.g.:
  37.     ///
  38.     ///     class ExampleView: UIView {
  39.     ///         enum Strings: String, Localizable {
  40.     ///             case title = "title"
  41.     ///         }
  42.     ///     }
  43.     ///
  44.     ///     ExampleView.Strings.title.localized
  45.     ///
  46.     /// Will get the localized string with key "title" from the ExampleView.strings file
  47.     var localized: String {
  48.         return rawValue.localized(tableName: Self.tableName())
  49.     }
  50. }
  51.  
  52. extension String {
  53.     func localized(bundle: Bundle = .main, tableName: String = "Localizable") -> String {
  54.         return NSLocalizedString(self, tableName: tableName, value: "**\(self)**", comment: "")
  55.     }
  56. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top