String+TUILocalized.swift 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //
  2. // String+TUILocalized.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/18.
  6. //
  7. import Foundation
  8. class TIMLocalizedText {
  9. static let shared = TIMLocalizedText()
  10. private var commonBundle: Bundle?
  11. private var faceBundle: Bundle?
  12. private init() {
  13. rebuildBundle()
  14. LNEventDeliver.addObserver(self)
  15. }
  16. func commonText(_ key: String) -> String {
  17. commonBundle?.localizedString(forKey: key, value: key, table: nil) ?? key
  18. }
  19. func faceText(_ key: String) -> String {
  20. faceBundle?.localizedString(forKey: key, value: key, table: nil) ?? key
  21. }
  22. }
  23. extension TIMLocalizedText: LNAppMainEvent {
  24. func onAppLanguageChanged(newLanguage: LNAppLanguage) {
  25. rebuildBundle()
  26. }
  27. }
  28. extension TIMLocalizedText {
  29. private func rebuildBundle() {
  30. let curLan = LNAppConfig.shared.curLang.bundleName
  31. var language = "Localizable/" + curLan
  32. let commonPath = Bundle(path: Bundle.main.path(forResource: "TIMCommonLocalizable", ofType: "bundle")!)!
  33. if commonPath.path(forResource: language, ofType: "lproj") == nil {
  34. language = "Localizable/" + LNAppLanguage.english.bundleName
  35. }
  36. commonBundle = Bundle(path: commonPath.path(forResource: language, ofType: "lproj")!)
  37. let facePath = Bundle(path: Bundle.main.path(forResource: "TUIChatFace", ofType: "bundle")!)!
  38. faceBundle = Bundle(path: facePath.path(forResource: language, ofType: "lproj")!)
  39. }
  40. }