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.

54 lines
1.7 KiB

  1. //
  2. // String+LocalizeBundle.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 friendly extension
  10. public extension String {
  11. /**
  12. Swift 2 friendly localization syntax, replaces NSLocalizedString.
  13. - parameter bundle: The receivers bundle to search. If bundle is `nil`,
  14. the method attempts to use main bundle.
  15. - returns: The localized string.
  16. */
  17. func localized(in bundle: Bundle?) -> String {
  18. return localized(using: nil, in: bundle)
  19. }
  20. /**
  21. Swift 2 friendly localization syntax with format arguments, replaces String(format:NSLocalizedString).
  22. - parameter arguments: arguments values for temlpate (substituted according to the users default locale).
  23. - parameter bundle: The receivers bundle to search. If bundle is `nil`,
  24. the method attempts to use main bundle.
  25. - returns: The formatted localized string with arguments.
  26. */
  27. func localizedFormat(arguments: CVarArg..., in bundle: Bundle?) -> String {
  28. return String(format: localized(in: bundle), arguments: arguments)
  29. }
  30. /**
  31. Swift 2 friendly plural localization syntax with a format argument.
  32. - parameter argument: Argument to determine pluralisation.
  33. - parameter bundle: The receivers bundle to search. If bundle is `nil`,
  34. the method attempts to use main bundle.
  35. - returns: Pluralized localized string.
  36. */
  37. func localizedPlural(argument: CVarArg, in bundle: Bundle?) -> String {
  38. return NSString.localizedStringWithFormat(localized(in: bundle) as NSString, argument) as String
  39. }
  40. }