Guest User

Untitled

a guest
Nov 21st, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. //First, define a custom bundle class as like as below
  2.  
  3. import Foundation
  4.  
  5. //A key used for exchanging associated object
  6. var _BUNDLE_KEY = 0
  7.  
  8. class BundleEx : Bundle {
  9.  
  10. override func localizedString(forKey key: String, value: String?, table tableName: String?) -> String {
  11.  
  12. let bundle : Bundle? = objc_getAssociatedObject(self, &_BUNDLE_KEY) as? Bundle
  13.  
  14. return bundle != nil ? bundle!.localizedString(forKey: key, value: value, table: tableName) : super.localizedString(forKey: key, value: value, table: tableName)
  15. }
  16. }
  17.  
  18. //Second, in App delegate.application(_ : didFinishLaunchingWithOptions:) try to swizzle the Bundle.main.
  19. //This snippet only runs for the first application execution. In further executions it won't be executed since the langs array will contain
  20. // "fa" language
  21.  
  22. let langs = UserDefaults.standard.value(forKey: "AppleLanguages") as? [String]
  23.  
  24. if langs == nil || !langs!.contains("fa") {
  25.  
  26. UserDefaults.standard.set(["fa-IR", "fa", "en"], forKey: "AppleLanguages" )
  27. UserDefaults.standard.synchronize()
  28.  
  29. object_setClass(Bundle.main, BundleEx.self)
  30.  
  31. let bundle = Bundle(path: Bundle.main.path(forResource: "fa-IR", ofType: "lproj")!)
  32. objc_setAssociatedObject(Bundle.main, &_BUNDLE_KEY, bundle, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  33.  
  34. //Changes the direction of all views. You omit it if the determined language is LTR
  35. UIView.appearance().semanticContentAttribute = .forceRightToLeft
  36.  
  37. let sb = UIStoryboard(name: "Main", bundle: Bundle.main)
  38. let vc = sb.instantiateInitialViewController()
  39. self.window?.rootViewController = vc
  40. }
Add Comment
Please, Sign In to add comment