LocaleHelper.swift 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*****************************************************************************
  2. * LocaleHelper.swift
  3. * VLC for iOSUITests
  4. *****************************************************************************
  5. * Copyright (c) 2018 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Mike JS. Choi <mkchoi212 # icloud.com>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. import Foundation
  13. import XCTest
  14. struct LocaleHelper {
  15. let localizationBundle: Bundle
  16. let inherantBundle = Bundle(for: UIApplication.self)
  17. init(lang: String, target: AnyClass) {
  18. localizationBundle = LocaleHelper.loadLocalizables(lang: lang, target: target)
  19. }
  20. func localized(key: String) -> String {
  21. let res = NSLocalizedString(key, bundle: localizationBundle, comment: "")
  22. return res
  23. }
  24. }
  25. extension LocaleHelper {
  26. static func loadLocalizables(lang: String, target: AnyClass) -> Bundle {
  27. let mainBundle = Bundle(for: target.self)
  28. guard let path = mainBundle.path(forResource: lang, ofType: ".lproj") else {
  29. XCTFail("Could not resolve localization file for \(lang)")
  30. return Bundle()
  31. }
  32. guard let bundle = Bundle(path: path) else {
  33. XCTFail("Could not load bundle at \(path)")
  34. return Bundle()
  35. }
  36. return bundle
  37. }
  38. }