From b94a86dae3e07c0abc5e7bb4973af2e1f0113194 Mon Sep 17 00:00:00 2001 From: Vitalii Budnik Date: Thu, 6 Oct 2016 22:49:15 +0300 Subject: [PATCH] tableName support --- Localize_Swift.xcodeproj/project.pbxproj | 10 +++ Sources/Localize.swift | 10 +-- Sources/String+LocalizeTableName.swift | 62 +++++++++++++++++++ .../Sample/fr.lproj/ButtonTitles.strings | 11 ++++ 4 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 Sources/String+LocalizeTableName.swift create mode 100644 examples/LanguageSwitch/Sample/fr.lproj/ButtonTitles.strings diff --git a/Localize_Swift.xcodeproj/project.pbxproj b/Localize_Swift.xcodeproj/project.pbxproj index c877fc3..d1ca9b8 100644 --- a/Localize_Swift.xcodeproj/project.pbxproj +++ b/Localize_Swift.xcodeproj/project.pbxproj @@ -21,6 +21,10 @@ 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 */; }; + 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 */; }; + 68A520071DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68A520031DA6D5FD00F43D9E /* String+LocalizeTableName.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -46,6 +50,7 @@ 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; }; + 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 */ /* Begin PBXFrameworksBuildPhase section */ @@ -118,6 +123,7 @@ children = ( 3433F2501C518B38003AE34D /* Localize_Swift.h */, 3433F2511C518B38003AE34D /* Localize.swift */, + 68A520031DA6D5FD00F43D9E /* String+LocalizeTableName.swift */, 3433F23B1C518AF7003AE34D /* Info.plist */, 343A6DE91D152B1B0081AA37 /* Localize_Swift-tvOS.plist */, 344169461C67539300B93D28 /* Foundation.framework */, @@ -350,6 +356,7 @@ buildActionMask = 2147483647; files = ( 3433F2531C518B38003AE34D /* Localize.swift in Sources */, + 68A520041DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -366,6 +373,7 @@ buildActionMask = 2147483647; files = ( 343A6DD11D1529560081AA37 /* Localize.swift in Sources */, + 68A520051DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -374,6 +382,7 @@ buildActionMask = 2147483647; files = ( 343A6DDF1D152B1B0081AA37 /* Localize.swift in Sources */, + 68A520061DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -382,6 +391,7 @@ buildActionMask = 2147483647; files = ( 343A6DEC1D152E5A0081AA37 /* Localize.swift in Sources */, + 68A520071DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Sources/Localize.swift b/Sources/Localize.swift index b35c2c6..d056435 100644 --- a/Sources/Localize.swift +++ b/Sources/Localize.swift @@ -59,15 +59,7 @@ public extension String { - Returns: The localized string. */ func localized() -> 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: nil) - } - else if let path = Bundle.main.path(forResource: LCLBaseBundle, ofType: "lproj"), - let bundle = Bundle(path: path) { - return bundle.localizedString(forKey: self, value: nil, table: nil) - } - return self + return localized(using: nil) } /** diff --git a/Sources/String+LocalizeTableName.swift b/Sources/String+LocalizeTableName.swift new file mode 100644 index 0000000..88e14ab --- /dev/null +++ b/Sources/String+LocalizeTableName.swift @@ -0,0 +1,62 @@ +// +// String+LocalizeTableName.swift +// Localize_Swift +// +// Created by Vitalii Budnik on 3/10/16. +// Copyright © 2016 Vitalii Budnik. All rights reserved. +// + +import Foundation + +/// 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`. + + - 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 + } + + /** + 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 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?, arguments: CVarArg...) -> String { + return String(format: localized(using: tableName), 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 argument: Argument to determine pluralisation + + - returns: Pluralized localized string. + */ + func localizedPlural(using tableName: String?, argument: CVarArg) -> String { + return NSString.localizedStringWithFormat(localized(using: tableName) as NSString, argument) as String + } + +} diff --git a/examples/LanguageSwitch/Sample/fr.lproj/ButtonTitles.strings b/examples/LanguageSwitch/Sample/fr.lproj/ButtonTitles.strings new file mode 100644 index 0000000..fb94c75 --- /dev/null +++ b/examples/LanguageSwitch/Sample/fr.lproj/ButtonTitles.strings @@ -0,0 +1,11 @@ +/* + ButtonTitles.strings + Sample + + Created by Vitalii Budnik on 10/6/16. + Copyright © 2016 Roy Marmelstein. All rights reserved. +*/ + +"Change" = "Modifier"; + +"Reset" = "Réinitialiser";