Languager.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // Languager.swift
  3. // icar
  4. //
  5. // Created by gongkai on 15/5/18.
  6. // Copyright (c) 2015年 anytracking. All rights reserved.
  7. //
  8. import UIKit
  9. private let kUserLanguage = "AppleLanguages"
  10. /**
  11. * 国际化工具
  12. */
  13. class Languager: NSObject {
  14. private struct Static {
  15. static var staticInstance : Languager? = nil
  16. }
  17. private var _currentLanguage:String?
  18. override init() {
  19. super.init()
  20. self.initLanguages()
  21. }
  22. //当前语言Bundle
  23. internal var currentLanguageBundle: Bundle?
  24. // 当前语言获取与切换
  25. var currentLanguage:String{
  26. get{
  27. if(self._currentLanguage==nil){
  28. self._currentLanguage = (UserDefaults.standard.array(forKey: kUserLanguage))?.first as? String
  29. //TODO 现在的语言带后缀的 不知道怎么处理 临时去掉后缀
  30. self._currentLanguage = self._currentLanguage?.replacingOccurrences(of: "-CN", with: "")
  31. self._currentLanguage = self._currentLanguage?.replacingOccurrences(of: "-US", with: "")
  32. if self._currentLanguage != "zh-HK" {
  33. self._currentLanguage = self._currentLanguage?.replacingOccurrences(of: "-HK", with: "")
  34. }
  35. self._currentLanguage = self._currentLanguage?.replacingOccurrences(of: "-HK", with: "")
  36. self._currentLanguage = self._currentLanguage?.replacingOccurrences(of: "-TW", with: "")
  37. }
  38. return self._currentLanguage!
  39. }
  40. set(newLanguage){
  41. if(self._currentLanguage == newLanguage){
  42. return
  43. }
  44. if let path = Bundle.main.path(forResource:newLanguage, ofType: "lproj"), let bundel = Bundle(path: path){
  45. self.currentLanguageBundle = bundel
  46. self._currentLanguage = newLanguage
  47. }else{
  48. //如果不支持当前语言则加载info中Localization native development region中的值的lporj
  49. let defaultLanguage = "zh-Hans"
  50. self.currentLanguageBundle = Bundle(path: Bundle.main.path(forResource: defaultLanguage, ofType: "lproj" )!)
  51. self._currentLanguage = defaultLanguage
  52. }
  53. let def = UserDefaults.standard
  54. def.setValue([self._currentLanguage!], forKey:kUserLanguage)
  55. def.synchronize()
  56. Bundle.main.onLanguage()
  57. }
  58. }
  59. // 单列
  60. class func standardLanguager() -> Languager{
  61. DispatchQueue.once(token: "standardLanguager") {
  62. Static.staticInstance = Languager()
  63. }
  64. return Static.staticInstance!
  65. }
  66. //初始化
  67. func initLanguages(){
  68. var language = (UserDefaults.standard.array(forKey: kUserLanguage))?.first as? String
  69. //TODO 现在的语言带后缀的 不知道怎么处理 临时去掉后缀
  70. language = language?.replacingOccurrences(of: "-CN", with: "")
  71. language = language?.replacingOccurrences(of: "-US", with: "")
  72. if language != "zh-HK" {
  73. language = language?.replacingOccurrences(of: "-HK", with: "")
  74. }
  75. language = language?.replacingOccurrences(of: "-TW", with: "")
  76. if let path = Bundle.main.path(forResource: language, ofType: "lproj" ), let bundel = Bundle(path: path){
  77. self.currentLanguageBundle = bundel
  78. self._currentLanguage = language
  79. }else{
  80. //如果不支持当前语言则加载info中Localization native development region中的值的lporj,设置为当前语言
  81. self.currentLanguage = "zh-Hans"
  82. print("Languager:\(String(describing: language))不支持,切换成默认语言\(self._currentLanguage!)")
  83. }
  84. }
  85. /**
  86. 获取当前语言的storyboard
  87. */
  88. func storyboard(name:String)-> UIStoryboard{
  89. return UIStoryboard(name: name, bundle: self.currentLanguageBundle)
  90. }
  91. /**
  92. 获取当前语言的nib
  93. */
  94. func nib(name:String)->UINib{
  95. return UINib(nibName: name, bundle: self.currentLanguageBundle)
  96. }
  97. /**
  98. 获取当前语言的string
  99. */
  100. func string(key:String)->String{
  101. if let str = self.currentLanguageBundle?.localizedString(forKey: key, value: nil, table: nil) {
  102. return str
  103. }
  104. return key
  105. }
  106. /**
  107. 获取当前语言的image
  108. */
  109. func image(name:String)->UIImage?{
  110. let path = self.currentLanguageBundle?.path(forResource: name+"@2x", ofType: "png")
  111. return UIImage(contentsOfFile: path!)
  112. }
  113. }
  114. func localized(key:String)->String{
  115. return Languager.standardLanguager().string(key: key)
  116. }
  117. func localizedImage(key:String)->UIImage?{
  118. return Languager.standardLanguager().image(name: key)
  119. }