Bundle+Extension.swift 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // Bundle+Extension.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/10/23.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import Foundation
  9. /**
  10. * 当调用onLanguage后替换掉mainBundle为当前语言的bundle
  11. */
  12. //private let _bundle:UnsafePointer<Void> = unsafeBitCast(0,to: UnsafePointer<Void>.self)
  13. class BundleEx: Bundle {
  14. override func localizedString(forKey key: String, value: String?, table tableName: String?) -> String {
  15. if let bundle = languageBundle() {
  16. return bundle.localizedString(forKey: key, value: value, table: tableName)
  17. } else {
  18. return super.localizedString(forKey: key, value: value, table: tableName)
  19. }
  20. }
  21. }
  22. extension Bundle {
  23. // private struct Static {
  24. // static let onceToken: Static = { Static() }()
  25. // }
  26. //
  27. func onLanguage() {
  28. //替换NSBundle.mainBundle()为自定义的BundleEx
  29. DispatchQueue.once(token: "language") {
  30. object_setClass(Bundle.main, BundleEx.self)
  31. }
  32. }
  33. //当前语言的bundle
  34. func languageBundle() -> Bundle? {
  35. return Languager.standardLanguager().currentLanguageBundle
  36. }
  37. //o2 表情包读取
  38. func o2EmojiBundle(anyClass: AnyClass) -> Bundle {
  39. var bundle: Bundle = Bundle.main
  40. if let resource = Bundle(for: anyClass.self).path(forResource: "O2Emoji", ofType: "bundle") {
  41. bundle = Bundle(path: resource) ?? Bundle.main
  42. }
  43. return bundle
  44. }
  45. }