Blame view

HDFwear/AppSettings.swift 8.34 KB
75d24c15   yangbin   123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  //
  //  AppSettings.swift
  //  zc
  //
  //  Created by wyp on 2021/1/18.
  //  Copyright © 2021 wyp. All rights reserved.
  //
  import Foundation
  import MBProgressHUD
  
  class AppSettings: NSObject {
  
      fileprivate static let kSharedSettingsKey = "DefaultUserSettings"
  
      static let shared: AppSettings = AppSettings()
      
  //    {
  //        let appSettings: AppSettings
  //        if let savedData = UserDefaults.standard.object(forKey: AppSettings.kSharedSettingsKey) as? Data,
  //            let defaultSettings = NSKeyedUnarchiver.unarchiveObject(with: savedData) as? AppSettings {
  //            self.langua
  //        } else {
  //            appSettings = AppSettings()
  //        }
  //
  //        return appSettings
  //    }()
  
      func saveSharedInstance() {
          UserDefaults.standard.set(language.rawValue, forKey: AppSettings.kSharedSettingsKey)
      }
  
      enum Language: String {
          case Chinese = "zh-Hans"
          case English = "en"
          case Russisch = "ru"
          case Spanish = "es"
          case German = "de"
          case Italy = "it"
          case French = "fr"
          case Vietnamese = "vi"
          case Poland = "pl"
          case Dutch = "nl"
          case Greek = "el" //10
          case Turkey = "en??1"
          case Ronmanian = "en??2"
          case Bulgarian = "en?3"
          case TChinese = "en??4"
          case Hebrew = "he"
          case Danish = "da"
          case Serbia = "en??5"
          case Sweden = "en??6"
          case Czech = "cs" //19
          case Skovak = "sk"
          case Hungarian = "hu"
          case WHERE = "en????"
          case Japanese = "ja"
          case Thai = "th"
          case Ukraine = "en??7"
          case Suomi = "en??8"
          case Norsk = "en??9"
          case Korean = "ko"
          case Indonesian = "id"
          case Latvia = "lv" //30
          case Lithuania = "lt"
          case Estonia = "et"
          case Portuguese = "pt"
          case Arab = "ar"
f2cf74c7   yangbin   1.0.20(4)
69
          case Malay = "ms"
75d24c15   yangbin   123
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
          
          case system = "system"
          var code: String {
              return rawValue
          }
      }
  //        var cmd: UInt8 {
  //            switch rawValue: {
  //            case  "zh-Hans":
  //                return 0x01
  //            case  "en":
  //                return 0x00
  //            default:
  //                return 0x00
  //            }
  //        }
          
  
          
      
      static func localeIsChinese() -> Bool {
          if let lang = Locale.preferredLanguages.first {
              if lang.hasPrefix("zh") {
                  return true
              } else {
                  return false
              }
          } else {
              return false
          }
      }
      
      static func localeLanguage() -> Language {
          if let lang = Locale.preferredLanguages.first {
              if lang.hasPrefix("zh") {
                  return .Chinese
              }  else if lang.hasPrefix("en") {
                  return .English
              } else if lang.hasPrefix("ru") {
                  return .Russisch
              } else if lang.hasPrefix("es") {
                  return .Spanish
              } else if lang.hasPrefix("de") {
                  return .German
              } else if lang.hasPrefix("it") {
                  return .Italy
              } else if lang.hasPrefix("fr") {
                  return .French
              } else if lang.hasPrefix("pl") {
                  return .Poland
              } else if lang.hasPrefix("cs") {
                  return .Czech
              } else if lang.hasPrefix("sk") {
                  return .Skovak
              } else if lang.hasPrefix("hu") {
                  return .Hungarian
              } else if lang.hasPrefix("ja") {
                  return .Japanese
              } else if lang.hasPrefix("lv") {
                  return .Latvia
              } else if lang.hasPrefix("lt") {
                  return .Lithuania
              } else if lang.hasPrefix("et") {
                  return .Estonia
              } else if lang.hasPrefix("pt") {
                  return .Portuguese
              } else if lang.hasPrefix("ar") {
                  return .Arab
f2cf74c7   yangbin   1.0.20(4)
138
139
140
141
              } else if lang.hasPrefix("th") {
                  return .Thai
              }  else if lang.hasPrefix("ms") {
                  return .Malay
75d24c15   yangbin   123
142
143
144
145
146
147
148
149
150
151
152
              } else  {
                  return .English
              }
          }
          return .English
      }
  
  
      var language: Language = {
  //        return .English
  //        return .Chinese
66e7e76d   yangbin   NFC
153
          return AppSettings.localeLanguage()
75d24c15   yangbin   123
154
155
156
  //        if let savedData = UserDefaults.standard.object(forKey: AppSettings.kSharedSettingsKey) as? String {
  //            return Language(rawValue: savedData) ?? .Chinese
  //        } else {
66e7e76d   yangbin   NFC
157
  //            return  AppSettings.localeIsChinese() ? .Chinese : .English
75d24c15   yangbin   123
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  //        }
      }()
      
      
      var _bundle: UInt8 = 0;
      func setLanguage(language: Language) {
          if self.language != language {
              self.language = language
              saveSharedInstance()
              let hud = MBProgressHUD.showMessage("")
              DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                  hud?.hide(animated: true)
                  KeyWindow.rootViewController = ZCTabBarController()
              }
  //            if let appdelegate = UIApplication.shared.delegate {
  //                let storyBoard = UIStoryboard.init(name: "Main", bundle: nil)
  //                if let mainController = storyBoard.instantiateViewController(withIdentifier: "rootViewController") as? UINavigationController {
                  
  //                }
  //            }
          } else {
              
          }
          self.language = language
          saveSharedInstance()
      }
      
      
      override init() {
          // 第一次初始语言, 看手机是什么语言
  //        language = AppSettings.localeIsChinese() ? .Chinese : .English
  //        language = .system
          super.init()
      }
  
  }
  private var bundleByLanguageCode: [String: Foundation.Bundle] = [:]
  
  extension AppSettings.Language {
      var bundle: Foundation.Bundle? {
          if let bundle = bundleByLanguageCode[code] {
              return bundle
          } else {
              let mainBundle = Foundation.Bundle.main
              if let path = mainBundle.path(forResource: code, ofType: "lproj"),
                  let bundle = Foundation.Bundle(path: path) {
                  bundleByLanguageCode[code] = bundle
                  return bundle
              } else {
                  return nil
              }
          }
      }
      
      var cmd: UInt8 {
          switch self {
          case .Chinese:
              return 0x00
          case .English:
              return 0x01
          case .Russisch:
              return 0x02
          case .Spanish:
              return 0x03
          case .German:
              return 0x04
          case .Italy:
              return 0x05
          case .French:
              return 0x06
          case .Vietnamese:
              return 0x07
          case .Poland:
              return 0x08
          case .Dutch:
              return 0x09
          case .Greek: //
              return 10
          case .Turkey:
              return 11
          case .Ronmanian:
              return 12
          case .Bulgarian:
              return 13
          case .TChinese:
              return 14
          case .Hebrew:
              return 15
          case .Danish:
              return 16
          case .Serbia:
              return 17
          case .Sweden:
              return 18
          case .Czech:
              return 19
          case .Skovak:
              return 20
          case .Hungarian:
              return 21
          case .WHERE:
              return 22
          case .Japanese:
              return 23
          case .Thai:
              return 24
          case .Ukraine:
              return 25
          case .Suomi:
              return 26
          case .Norsk:
              return 27
          case .Korean:
              return 28
          case .Indonesian:
              return 29
          case .Latvia:
              return 30
          case .Lithuania:
              return 31
          case .Estonia:
              return 32
          case .Portuguese:
              return 33
          case .Arab:
              return 34
          case .system:
              return 0x00
f2cf74c7   yangbin   1.0.20(4)
286
287
          case .Malay:
              return 0x00
75d24c15   yangbin   123
288
289
290
291
292
293
294
295
296
297
298
299
300
          }
      }
  }
  
  class HDFBundle: Foundation.Bundle {
      override func localizedString(forKey key: String, value: String?, table tableName: String?) -> String {
          if let bundle = AppSettings.shared.language.bundle {
              return bundle.localizedString(forKey: key, value: value, table: tableName)
          } else {
              return super.localizedString(forKey: key, value: value, table: tableName)
          }
      }
  }