Skip to content

Commit

Permalink
tableName support
Browse files Browse the repository at this point in the history
  • Loading branch information
nekrich committed Oct 6, 2016
1 parent 6d7427a commit b94a86d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 9 deletions.
10 changes: 10 additions & 0 deletions Localize_Swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 = "<absolute>"; };
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 */
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -350,6 +356,7 @@
buildActionMask = 2147483647;
files = (
3433F2531C518B38003AE34D /* Localize.swift in Sources */,
68A520041DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -366,6 +373,7 @@
buildActionMask = 2147483647;
files = (
343A6DD11D1529560081AA37 /* Localize.swift in Sources */,
68A520051DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -374,6 +382,7 @@
buildActionMask = 2147483647;
files = (
343A6DDF1D152B1B0081AA37 /* Localize.swift in Sources */,
68A520061DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -382,6 +391,7 @@
buildActionMask = 2147483647;
files = (
343A6DEC1D152E5A0081AA37 /* Localize.swift in Sources */,
68A520071DA6D5FD00F43D9E /* String+LocalizeTableName.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
10 changes: 1 addition & 9 deletions Sources/Localize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down
62 changes: 62 additions & 0 deletions Sources/String+LocalizeTableName.swift
Original file line number Diff line number Diff line change
@@ -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
}

}
11 changes: 11 additions & 0 deletions examples/LanguageSwitch/Sample/fr.lproj/ButtonTitles.strings
Original file line number Diff line number Diff line change
@@ -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";

0 comments on commit b94a86d

Please sign in to comment.