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.9 KiB

  1. //
  2. // String+LocalizeTableName.swift
  3. // Localize_Swift
  4. //
  5. // Created by Vitalii Budnik on 3/10/16.
  6. // Copyright © 2016 Vitalii Budnik. All rights reserved.
  7. //
  8. import Foundation
  9. /// 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. - returns: The localized string.
  16. */
  17. func localized(using tableName: String?) -> String {
  18. return localized(using: tableName, in: .main)
  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 tableName: The receivers string table to search. If tableName is `nil`
  24. or is an empty string, the method attempts to use `Localizable.strings`.
  25. - returns: The formatted localized string with arguments.
  26. */
  27. func localizedFormat(arguments: CVarArg..., using tableName: String?) -> String {
  28. return String(format: localized(using: tableName), arguments: arguments)
  29. }
  30. /**
  31. Swift 2 friendly plural localization syntax with a format argument.
  32. - parameter argument: Argument to determine pluralisation.
  33. - parameter tableName: The receivers string table to search. If tableName is `nil`
  34. or is an empty string, the method attempts to use `Localizable.strings`.
  35. - returns: Pluralized localized string.
  36. */
  37. func localizedPlural(argument: CVarArg, using tableName: String?) -> String {
  38. return NSString.localizedStringWithFormat(localized(using: tableName) as NSString, argument) as String
  39. }
  40. }