You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.8 KiB

  1. //
  2. // String+LocalizedBundleTableName.swift
  3. // Localize_Swift
  4. //
  5. // Created by Vitalii Budnik on 10/7/16.
  6. // Copyright © 2016 Roy Marmelstein. All rights reserved.
  7. //
  8. import Foundation
  9. /// bundle & tableName friendly extension
  10. public extension String {
  11. /**
  12. Swift 2 friendly localization syntax, replaces NSLocalizedString.
  13. - parameter tableName: The receivers string table to search. If tableName is `nil`
  14. or is an empty string, the method attempts to use `Localizable.strings`.
  15. - parameter bundle: The receivers bundle to search. If bundle is `nil`,
  16. the method attempts to use main bundle.
  17. - returns: The localized string.
  18. */
  19. func localized(using tableName: String?, in bundle: Bundle?) -> String {
  20. let bundle: Bundle = bundle ?? .main
  21. if let path = bundle.path(forResource: Localize.currentLanguage(), ofType: "lproj"),
  22. let bundle = Bundle(path: path) {
  23. return bundle.localizedString(forKey: self, value: nil, table: tableName)
  24. }
  25. else if let path = bundle.path(forResource: LCLBaseBundle, ofType: "lproj"),
  26. let bundle = Bundle(path: path) {
  27. return bundle.localizedString(forKey: self, value: nil, table: tableName)
  28. }
  29. return self
  30. }
  31. /**
  32. Swift 2 friendly localization syntax with format arguments, replaces String(format:NSLocalizedString).
  33. - parameter arguments: arguments values for temlpate (substituted according to the users default locale).
  34. - parameter tableName: The receivers string table to search. If tableName is `nil`
  35. or is an empty string, the method attempts to use `Localizable.strings`.
  36. - parameter bundle: The receivers bundle to search. If bundle is `nil`,
  37. the method attempts to use main bundle.
  38. - returns: The formatted localized string with arguments.
  39. */
  40. func localizedFormat(arguments: CVarArg..., using tableName: String?, in bundle: Bundle?) -> String {
  41. return String(format: localized(using: tableName, in: bundle), arguments: arguments)
  42. }
  43. /**
  44. Swift 2 friendly plural localization syntax with a format argument.
  45. - parameter argument: Argument to determine pluralisation.
  46. - parameter tableName: The receivers string table to search. If tableName is `nil`
  47. or is an empty string, the method attempts to use `Localizable.strings`.
  48. - parameter bundle: The receivers bundle to search. If bundle is `nil`,
  49. the method attempts to use main bundle.
  50. - returns: Pluralized localized string.
  51. */
  52. func localizedPlural(argument: CVarArg, using tableName: String?, in bundle: Bundle?) -> String {
  53. return NSString.localizedStringWithFormat(localized(using: tableName, in: bundle) as NSString, argument) as String
  54. }
  55. }