Skip to content

Commit

Permalink
Merge pull request #24 from Decybel07/SwiftPM
Browse files Browse the repository at this point in the history
[#21] Temporary solution for plurals when using Swift PM
  • Loading branch information
Decybel07 authored Mar 2, 2020
2 parents d1ddd96 + ac7ed62 commit ff6b824
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# Change Log
---

## [5.6.1](https://github.com/Decybel07/L10n-swift/tree/5.6.1) (2020-03-02)
* Temporary solution for plurals when using Swift PM [#21](https://github.com/Decybel07/L10n-swift/issues/21)

## [5.6.0](https://github.com/Decybel07/L10n-swift/tree/5.6.0) (2019-11-24)
* Storing selected language for `L10n.shared`

Expand Down
2 changes: 1 addition & 1 deletion L10n-swift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Pod::Spec.new do |spec|
spec.name = 'L10n-swift'
spec.module_name = 'L10n_swift'
spec.version = '5.6.0'
spec.version = '5.6.1'
spec.summary = 'Localization of an application with ability to change language "on the fly" and support for plural forms in any language.'

spec.description = <<-DESC
Expand Down
10 changes: 8 additions & 2 deletions L10n_swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@
2DE4B16D1F2C957A0067EE98 /* L10n_swiftTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = L10n_swiftTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
2DE4B1811F2C95A70067EE98 /* L10n_swift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = L10n_swift.framework; sourceTree = BUILT_PRODUCTS_DIR; };
2DE4B1891F2C95A70067EE98 /* L10n_swiftTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = L10n_swiftTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
2DE4B1991F2C96E80067EE98 /* .swift-version */ = {isa = PBXFileReference; lastKnownFileType = text; path = ".swift-version"; sourceTree = "<group>"; };
2DE4B19A1F2C96E80067EE98 /* .travis.yml */ = {isa = PBXFileReference; lastKnownFileType = text; path = .travis.yml; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.sh; };
2DE4B19B1F2C96E80067EE98 /* CHANGELOG.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CHANGELOG.md; sourceTree = "<group>"; };
2DE4B19C1F2C96E80067EE98 /* L10n-swift.podspec */ = {isa = PBXFileReference; lastKnownFileType = text; path = "L10n-swift.podspec"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.ruby; };
Expand Down Expand Up @@ -568,7 +567,6 @@
2DE4B19A1F2C96E80067EE98 /* .travis.yml */,
2D99E7C1209F4777003ECD51 /* .jazzy.yml */,
2DE4B19E1F2C96E80067EE98 /* Package.swift */,
2DE4B1991F2C96E80067EE98 /* .swift-version */,
);
name = "Supporting Files";
sourceTree = "<group>";
Expand Down Expand Up @@ -1293,6 +1291,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 5.6.1;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_BUNDLE_IDENTIFIER = "CodingLifestyle.L10n-iOS";
Expand Down Expand Up @@ -1355,6 +1354,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 5.6.1;
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = "CodingLifestyle.L10n-iOS";
PRODUCT_NAME = "$(PROJECT_NAME)";
Expand Down Expand Up @@ -1534,6 +1534,7 @@
INFOPLIST_FILE = "$(SRCROOT)/Source/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 5.6.1;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_BUNDLE_IDENTIFIER = "CodingLifestyle.L10n-watchOS";
Expand Down Expand Up @@ -1596,6 +1597,7 @@
INFOPLIST_FILE = "$(SRCROOT)/Source/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 5.6.1;
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = "CodingLifestyle.L10n-watchOS";
PRODUCT_NAME = "$(PROJECT_NAME)";
Expand Down Expand Up @@ -1662,6 +1664,7 @@
INFOPLIST_FILE = "$(SRCROOT)/Source/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 5.6.1;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_BUNDLE_IDENTIFIER = "CodingLifestyle.L10n-tvOS";
Expand Down Expand Up @@ -1723,6 +1726,7 @@
INFOPLIST_FILE = "$(SRCROOT)/Source/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 5.6.1;
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = "CodingLifestyle.L10n-tvOS";
PRODUCT_NAME = "$(PROJECT_NAME)";
Expand Down Expand Up @@ -1897,6 +1901,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.10;
MARKETING_VERSION = 5.6.1;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_BUNDLE_IDENTIFIER = "CodingLifestyle.L10n-macOS";
Expand Down Expand Up @@ -1959,6 +1964,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.10;
MARKETING_VERSION = 5.6.1;
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = "CodingLifestyle.L10n-macOS";
PRODUCT_NAME = "$(PROJECT_NAME)";
Expand Down
6 changes: 0 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ let package = Package(
return paths
}()
),
// .testTarget(
// name: "L10n-swiftTest",
// dependencies: ["L10n-swift"],
// path: "Tests",
// cSettings: [.define("LANG", to: "en_US.UTF-8")]
// ),
],
swiftLanguageVersions: [.v4, .v4_2, .v5]
)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ github "Decybel07/L10n-swift", ~> 5.6

Add `.l10nPlural(CVarArg...)` following any `String` object you want translated with plurals:
```swift
"numberOfApples".l10nPlural(2)
"numberOfApples".l10nPlural(args: [2])
```
More about plurals, you can read on [**wiki**](https://github.com/Decybel07/L10n-swift/wiki/Plural)

Expand Down
63 changes: 61 additions & 2 deletions Source/Core/Plural/Plural.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ internal enum Plural: String {

extension Plural {

private static let format = Bundle(for: L10n.self).localizedString(forKey: "integer", value: "other", table: "Plural")

private static let format = Plural.createFormat()
static func variants(for number: NSNumber, with locale: Locale?) -> [Plural] {
var variants: [Plural] = []

Expand All @@ -40,4 +40,63 @@ extension Plural {
let variant = base ?? alternative
return variant == alternative ? [alternative] : [variant, alternative]
}

private static func createFormat() -> String {
let table = "Plural"
let `extension` = "stringsdict"
var bundle = Bundle(for: L10n.self)

if bundle.url(forResource: table, withExtension: `extension`) == nil,
case let subdirectory = "spmResources",
bundle.url(forResource: table, withExtension: `extension`, subdirectory: subdirectory) == nil
{
// FIXME: This is temporary solution for Swift Package Manager.
// See also: https://github.com/Decybel07/L10n-swift/issues/21

let baseUrl = bundle.bundleURL.appendingPathComponent(subdirectory)
let url = baseUrl.appendingPathComponent(table).appendingPathExtension(`extension`)
let fileContent = """
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>integer</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@value@</string>
<key>value</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>ld</string>
<key>zero</key>
<string>zero</string>
<key>one</key>
<string>one</string>
<key>two</key>
<string>two</string>
<key>few</key>
<string>few</string>
<key>many</key>
<string>many</string>
<key>other</key>
<string>other</string>
</dict>
</dict>
</dict>
</plist>
"""
do {
try FileManager.default.createDirectory(at: baseUrl, withIntermediateDirectories: true)
try fileContent.write(to: url, atomically: true, encoding: .utf8)
bundle = Bundle(url: baseUrl) ?? bundle
} catch {
L10n.shared.logger?.log("Can't create \(url): \(error.localizedDescription)")
}
}

return bundle.localizedString(forKey: "integer", value: "other", table: table)
}
}
4 changes: 2 additions & 2 deletions Source/Extensions/Number+Localizable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ extension NumericLocalizable where Self: NSNumberRepresentable {

// MARK: - IntegerLocalizable

protocol IntegerLocalizable: NumericLocalizable {}
public protocol IntegerLocalizable: NumericLocalizable {}

extension IntegerLocalizable {

Expand All @@ -82,7 +82,7 @@ extension IntegerLocalizable {

// MARK: - FloatingPointLocalizable

protocol FloatingPointLocalizable: NumericLocalizable {}
public protocol FloatingPointLocalizable: NumericLocalizable {}

extension FloatingPointLocalizable {
/**
Expand Down
2 changes: 1 addition & 1 deletion Source/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>5.5.0</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down

0 comments on commit ff6b824

Please sign in to comment.