Skip to content

Commit

Permalink
Added Bundle support
Browse files Browse the repository at this point in the history
  • Loading branch information
nekrich committed Oct 7, 2016
1 parent bd7ca5f commit c8948d3
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 10 deletions.
20 changes: 20 additions & 0 deletions Localize_Swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 = "<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; };
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 */

Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
);
Expand All @@ -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 */,
);
Expand All @@ -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 */,
);
Expand All @@ -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 */,
);
Expand Down
2 changes: 1 addition & 1 deletion Sources/Localize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public extension String {
- Returns: The localized string.
*/
func localized() -> String {
return localized(using: nil)
return localized(using: nil, in: .main)
}

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

}
10 changes: 1 addition & 9 deletions Sources/String+LocalizeTableName.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

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

}

0 comments on commit c8948d3

Please sign in to comment.