diff --git a/Localize_Swift.xcodeproj/project.pbxproj b/Localize_Swift.xcodeproj/project.pbxproj index d1ca9b8..095764a 100644 --- a/Localize_Swift.xcodeproj/project.pbxproj +++ b/Localize_Swift.xcodeproj/project.pbxproj @@ -21,6 +21,14 @@ 343A6DEE1D152E5A0081AA37 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 344169461C67539300B93D28 /* Foundation.framework */; }; 343A6DF01D152E5A0081AA37 /* Localize_Swift.h in Headers */ = {isa = PBXBuildFile; fileRef = 3433F2501C518B38003AE34D /* Localize_Swift.h */; settings = {ATTRIBUTES = (Public, ); }; }; 344169471C67539300B93D28 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 344169461C67539300B93D28 /* Foundation.framework */; }; + 68973D661DA7AA200076F08A /* String+LocalizeBundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68973D651DA7AA200076F08A /* String+LocalizeBundle.swift */; }; + 68973D671DA7AA200076F08A /* String+LocalizeBundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68973D651DA7AA200076F08A /* String+LocalizeBundle.swift */; }; + 68973D681DA7AA200076F08A /* String+LocalizeBundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68973D651DA7AA200076F08A /* String+LocalizeBundle.swift */; }; + 68973D691DA7AA200076F08A /* String+LocalizeBundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68973D651DA7AA200076F08A /* String+LocalizeBundle.swift */; }; + 68973D6B1DA7AB140076F08A /* String+LocalizedBundleTableName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68973D6A1DA7AB140076F08A /* String+LocalizedBundleTableName.swift */; }; + 68973D6C1DA7AB140076F08A /* String+LocalizedBundleTableName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68973D6A1DA7AB140076F08A /* String+LocalizedBundleTableName.swift */; }; + 68973D6D1DA7AB140076F08A /* String+LocalizedBundleTableName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68973D6A1DA7AB140076F08A /* String+LocalizedBundleTableName.swift */; }; + 68973D6E1DA7AB140076F08A /* String+LocalizedBundleTableName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68973D6A1DA7AB140076F08A /* String+LocalizedBundleTableName.swift */; }; 68A520041DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68A520031DA6D5FD00F43D9E /* String+LocalizeTableName.swift */; }; 68A520051DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68A520031DA6D5FD00F43D9E /* String+LocalizeTableName.swift */; }; 68A520061DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68A520031DA6D5FD00F43D9E /* String+LocalizeTableName.swift */; }; @@ -50,6 +58,8 @@ 343A6DE91D152B1B0081AA37 /* Localize_Swift-tvOS.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = "Localize_Swift-tvOS.plist"; path = "/Users/marmelroy/Documents/Projects/OpenSource/Localize/Localize_Swift-tvOS.plist"; sourceTree = ""; }; 343A6DF51D152E5A0081AA37 /* Localize_Swift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Localize_Swift.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 344169461C67539300B93D28 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; + 68973D651DA7AA200076F08A /* String+LocalizeBundle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "String+LocalizeBundle.swift"; path = "Sources/String+LocalizeBundle.swift"; sourceTree = SOURCE_ROOT; }; + 68973D6A1DA7AB140076F08A /* String+LocalizedBundleTableName.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "String+LocalizedBundleTableName.swift"; path = "Sources/String+LocalizedBundleTableName.swift"; sourceTree = SOURCE_ROOT; }; 68A520031DA6D5FD00F43D9E /* String+LocalizeTableName.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.swift; name = "String+LocalizeTableName.swift"; path = "Sources/String+LocalizeTableName.swift"; sourceTree = SOURCE_ROOT; tabWidth = 4; usesTabs = 0; }; /* End PBXFileReference section */ @@ -124,6 +134,8 @@ 3433F2501C518B38003AE34D /* Localize_Swift.h */, 3433F2511C518B38003AE34D /* Localize.swift */, 68A520031DA6D5FD00F43D9E /* String+LocalizeTableName.swift */, + 68973D651DA7AA200076F08A /* String+LocalizeBundle.swift */, + 68973D6A1DA7AB140076F08A /* String+LocalizedBundleTableName.swift */, 3433F23B1C518AF7003AE34D /* Info.plist */, 343A6DE91D152B1B0081AA37 /* Localize_Swift-tvOS.plist */, 344169461C67539300B93D28 /* Foundation.framework */, @@ -355,6 +367,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 68973D6B1DA7AB140076F08A /* String+LocalizedBundleTableName.swift in Sources */, + 68973D661DA7AA200076F08A /* String+LocalizeBundle.swift in Sources */, 3433F2531C518B38003AE34D /* Localize.swift in Sources */, 68A520041DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */, ); @@ -372,6 +386,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 68973D6C1DA7AB140076F08A /* String+LocalizedBundleTableName.swift in Sources */, + 68973D671DA7AA200076F08A /* String+LocalizeBundle.swift in Sources */, 343A6DD11D1529560081AA37 /* Localize.swift in Sources */, 68A520051DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */, ); @@ -381,6 +397,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 68973D6D1DA7AB140076F08A /* String+LocalizedBundleTableName.swift in Sources */, + 68973D681DA7AA200076F08A /* String+LocalizeBundle.swift in Sources */, 343A6DDF1D152B1B0081AA37 /* Localize.swift in Sources */, 68A520061DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */, ); @@ -390,6 +408,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 68973D6E1DA7AB140076F08A /* String+LocalizedBundleTableName.swift in Sources */, + 68973D691DA7AA200076F08A /* String+LocalizeBundle.swift in Sources */, 343A6DEC1D152E5A0081AA37 /* Localize.swift in Sources */, 68A520071DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */, ); diff --git a/Sources/Localize.swift b/Sources/Localize.swift index d056435..4c97eef 100644 --- a/Sources/Localize.swift +++ b/Sources/Localize.swift @@ -59,7 +59,7 @@ public extension String { - Returns: The localized string. */ func localized() -> String { - return localized(using: nil) + return localized(using: nil, in: .main) } /** diff --git a/Sources/String+LocalizeBundle.swift b/Sources/String+LocalizeBundle.swift new file mode 100644 index 0000000..c2c3dae --- /dev/null +++ b/Sources/String+LocalizeBundle.swift @@ -0,0 +1,54 @@ +// +// String+LocalizeBundle.swift +// Localize_Swift +// +// Created by Vitalii Budnik on 10/7/16. +// Copyright © 2016 Roy Marmelstein. All rights reserved. +// + +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 + } + +} diff --git a/Sources/String+LocalizeTableName.swift b/Sources/String+LocalizeTableName.swift index 88e14ab..b848938 100644 --- a/Sources/String+LocalizeTableName.swift +++ b/Sources/String+LocalizeTableName.swift @@ -20,15 +20,7 @@ public extension String { - Returns: The localized string. */ func localized(using tableName: String?) -> String { - if let path = Bundle.main.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.main.path(forResource: LCLBaseBundle, ofType: "lproj"), - let bundle = Bundle(path: path) { - return bundle.localizedString(forKey: self, value: nil, table: tableName) - } - return self + return localized(using: tableName, in: .main) } /** diff --git a/Sources/String+LocalizedBundleTableName.swift b/Sources/String+LocalizedBundleTableName.swift new file mode 100644 index 0000000..016c6b1 --- /dev/null +++ b/Sources/String+LocalizedBundleTableName.swift @@ -0,0 +1,72 @@ +// +// String+LocalizedBundleTableName.swift +// Localize_Swift +// +// Created by Vitalii Budnik on 10/7/16. +// Copyright © 2016 Roy Marmelstein. All rights reserved. +// + +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 + } + +}