From daaef14ca3482b973f82dcedd5cb06b3653c333b Mon Sep 17 00:00:00 2001 From: Vitalii Budnik Date: Sun, 9 Oct 2016 11:19:09 +0300 Subject: [PATCH] `argument[s]` parameter in `.localizePlural` & `.localizeFormat` methods is on the first place now. --- Sources/String+LocalizeBundle.swift | 82 ++++++------ Sources/String+LocalizeTableName.swift | 28 ++--- Sources/String+LocalizedBundleTableName.swift | 118 +++++++++--------- 3 files changed, 114 insertions(+), 114 deletions(-) diff --git a/Sources/String+LocalizeBundle.swift b/Sources/String+LocalizeBundle.swift index c2c3dae..55c3a4f 100644 --- a/Sources/String+LocalizeBundle.swift +++ b/Sources/String+LocalizeBundle.swift @@ -10,45 +10,45 @@ import Foundation /// bundle friendly extension public extension String { - - /** - Swift 2 friendly localization syntax, replaces NSLocalizedString - - - Parameter bundle: The receiver’s bundle to search. If bundle is `nil`, - the method attempts to use main bundle. - - - Returns: The localized string. - */ - func localized(in bundle: Bundle?) -> String { - return localized(using: nil, in: bundle) - } - - /** - Swift 2 friendly localization syntax with format arguments, replaces String(format:NSLocalizedString) - - - Parameter bundle: The receiver’s bundle to search. If bundle is `nil`, - the method attempts to use main bundle. - - - Parameter arguments: arguments values for temlpate (substituted according to the user’s default locale). - - - Returns: The formatted localized string with arguments. - */ - func localizedFormat(in bundle: Bundle?, arguments: CVarArg...) -> String { - return String(format: localized(in: bundle), arguments: arguments) - } - - /** - Swift 2 friendly plural localization syntax with a format argument - - - Parameter bundle: The receiver’s bundle to search. If bundle is `nil`, - the method attempts to use main bundle. - - - parameter argument: Argument to determine pluralisation - - - returns: Pluralized localized string. - */ - func localizedPlural(in bundle: Bundle?, argument: CVarArg) -> String { - return NSString.localizedStringWithFormat(localized(in: bundle) as NSString, argument) as String - } - + + /** + Swift 2 friendly localization syntax, replaces NSLocalizedString. + + - parameter bundle: The receiver’s bundle to search. If bundle is `nil`, + the method attempts to use main bundle. + + - returns: The localized string. + */ + func localized(in bundle: Bundle?) -> String { + return localized(using: nil, in: bundle) + } + + /** + Swift 2 friendly localization syntax with format arguments, replaces String(format:NSLocalizedString). + + - parameter arguments: arguments values for temlpate (substituted according to the user’s default locale). + + - parameter bundle: The receiver’s bundle to search. If bundle is `nil`, + the method attempts to use main bundle. + + - returns: The formatted localized string with arguments. + */ + func localizedFormat(arguments: CVarArg..., in bundle: Bundle?) -> String { + return String(format: localized(in: bundle), arguments: arguments) + } + + /** + Swift 2 friendly plural localization syntax with a format argument. + + - parameter argument: Argument to determine pluralisation. + + - parameter bundle: The receiver’s bundle to search. If bundle is `nil`, + the method attempts to use main bundle. + + - returns: Pluralized localized string. + */ + func localizedPlural(argument: CVarArg, in bundle: Bundle?) -> String { + return NSString.localizedStringWithFormat(localized(in: bundle) as NSString, argument) as String + } + } diff --git a/Sources/String+LocalizeTableName.swift b/Sources/String+LocalizeTableName.swift index b848938..ee7558e 100644 --- a/Sources/String+LocalizeTableName.swift +++ b/Sources/String+LocalizeTableName.swift @@ -12,42 +12,42 @@ import Foundation public extension String { /** - Swift 2 friendly localization syntax, replaces NSLocalizedString + Swift 2 friendly localization syntax, replaces NSLocalizedString. - - Parameter tableName: The receiver’s string table to search. If tableName is `nil` + - parameter tableName: The receiver’s string table to search. If tableName is `nil` or is an empty string, the method attempts to use `Localizable.strings`. - - Returns: The localized string. + - returns: The localized string. */ func localized(using tableName: String?) -> String { return localized(using: tableName, in: .main) } /** - Swift 2 friendly localization syntax with format arguments, replaces String(format:NSLocalizedString) + Swift 2 friendly localization syntax with format arguments, replaces String(format:NSLocalizedString). - - Parameter tableName: The receiver’s string table to search. If tableName is `nil` + - parameter arguments: arguments values for temlpate (substituted according to the user’s default locale). + + - parameter tableName: The receiver’s string table to search. If tableName is `nil` or is an empty string, the method attempts to use `Localizable.strings`. - - Parameter arguments: arguments values for temlpate (substituted according to the user’s default locale). - - - Returns: The formatted localized string with arguments. + - returns: The formatted localized string with arguments. */ - func localizedFormat(using tableName: String?, arguments: CVarArg...) -> String { + func localizedFormat(arguments: CVarArg..., using tableName: String?) -> String { return String(format: localized(using: tableName), arguments: arguments) } /** - Swift 2 friendly plural localization syntax with a format argument + Swift 2 friendly plural localization syntax with a format argument. - - Parameter tableName: The receiver’s string table to search. If tableName is `nil` + - parameter argument: Argument to determine pluralisation. + + - parameter tableName: The receiver’s string table to search. If tableName is `nil` or is an empty string, the method attempts to use `Localizable.strings`. - - parameter argument: Argument to determine pluralisation - - returns: Pluralized localized string. */ - func localizedPlural(using tableName: String?, argument: CVarArg) -> String { + func localizedPlural(argument: CVarArg, using tableName: String?) -> String { return NSString.localizedStringWithFormat(localized(using: tableName) as NSString, argument) as String } diff --git a/Sources/String+LocalizedBundleTableName.swift b/Sources/String+LocalizedBundleTableName.swift index 016c6b1..9cdd2f1 100644 --- a/Sources/String+LocalizedBundleTableName.swift +++ b/Sources/String+LocalizedBundleTableName.swift @@ -10,63 +10,63 @@ import Foundation /// bundle & tableName friendly extension public extension String { - - /** - Swift 2 friendly localization syntax, replaces NSLocalizedString - - - Parameter tableName: The receiver’s string table to search. If tableName is `nil` - or is an empty string, the method attempts to use `Localizable.strings`. - - - Parameter bundle: The receiver’s bundle to search. If bundle is `nil`, - the method attempts to use main bundle. - - - Returns: The localized string. - */ - func localized(using tableName: String?, in bundle: Bundle?) -> String { - let bundle: Bundle = bundle ?? .main - if let path = bundle.path(forResource: Localize.currentLanguage(), ofType: "lproj"), - let bundle = Bundle(path: path) { - return bundle.localizedString(forKey: self, value: nil, table: tableName) - } - else if let path = bundle.path(forResource: LCLBaseBundle, ofType: "lproj"), - let bundle = Bundle(path: path) { - return bundle.localizedString(forKey: self, value: nil, table: tableName) - } - return self - } - - /** - Swift 2 friendly localization syntax with format arguments, replaces String(format:NSLocalizedString) - - - Parameter tableName: The receiver’s string table to search. If tableName is `nil` - or is an empty string, the method attempts to use `Localizable.strings`. - - - Parameter bundle: The receiver’s bundle to search. If bundle is `nil`, - the method attempts to use main bundle. - - - Parameter arguments: arguments values for temlpate (substituted according to the user’s default locale). - - - Returns: The formatted localized string with arguments. - */ - func localizedFormat(using tableName: String?, in bundle: Bundle?, arguments: CVarArg...) -> String { - return String(format: localized(using: tableName, in: bundle), arguments: arguments) - } - - /** - Swift 2 friendly plural localization syntax with a format argument - - - Parameter tableName: The receiver’s string table to search. If tableName is `nil` - or is an empty string, the method attempts to use `Localizable.strings`. - - - Parameter bundle: The receiver’s bundle to search. If bundle is `nil`, - the method attempts to use main bundle. - - - parameter argument: Argument to determine pluralisation - - - returns: Pluralized localized string. - */ - func localizedPlural(using tableName: String?, in bundle: Bundle?, argument: CVarArg) -> String { - return NSString.localizedStringWithFormat(localized(using: tableName, in: bundle) as NSString, argument) as String - } - + + /** + Swift 2 friendly localization syntax, replaces NSLocalizedString. + + - parameter tableName: The receiver’s string table to search. If tableName is `nil` + or is an empty string, the method attempts to use `Localizable.strings`. + + - parameter bundle: The receiver’s bundle to search. If bundle is `nil`, + the method attempts to use main bundle. + + - returns: The localized string. + */ + func localized(using tableName: String?, in bundle: Bundle?) -> String { + let bundle: Bundle = bundle ?? .main + if let path = bundle.path(forResource: Localize.currentLanguage(), ofType: "lproj"), + let bundle = Bundle(path: path) { + return bundle.localizedString(forKey: self, value: nil, table: tableName) + } + else if let path = bundle.path(forResource: LCLBaseBundle, ofType: "lproj"), + let bundle = Bundle(path: path) { + return bundle.localizedString(forKey: self, value: nil, table: tableName) + } + return self + } + + /** + Swift 2 friendly localization syntax with format arguments, replaces String(format:NSLocalizedString). + + - parameter arguments: arguments values for temlpate (substituted according to the user’s default locale). + + - parameter tableName: The receiver’s string table to search. If tableName is `nil` + or is an empty string, the method attempts to use `Localizable.strings`. + + - parameter bundle: The receiver’s bundle to search. If bundle is `nil`, + the method attempts to use main bundle. + + - returns: The formatted localized string with arguments. + */ + func localizedFormat(arguments: CVarArg..., using tableName: String?, in bundle: Bundle?) -> String { + return String(format: localized(using: tableName, in: bundle), arguments: arguments) + } + + /** + Swift 2 friendly plural localization syntax with a format argument. + + - parameter argument: Argument to determine pluralisation. + + - parameter tableName: The receiver’s string table to search. If tableName is `nil` + or is an empty string, the method attempts to use `Localizable.strings`. + + - parameter bundle: The receiver’s bundle to search. If bundle is `nil`, + the method attempts to use main bundle. + + - returns: Pluralized localized string. + */ + func localizedPlural(argument: CVarArg, using tableName: String?, in bundle: Bundle?) -> String { + return NSString.localizedStringWithFormat(localized(using: tableName, in: bundle) as NSString, argument) as String + } + }