Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add localized attributes for subscription period and introductory period to SKProduct #315

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions SwiftyStoreKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
650307FC1E33154F001332A4 /* ProductsInfoController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 650307FB1E33154F001332A4 /* ProductsInfoController.swift */; };
650307FD1E33154F001332A4 /* ProductsInfoController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 650307FB1E33154F001332A4 /* ProductsInfoController.swift */; };
650307FE1E33154F001332A4 /* ProductsInfoController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 650307FB1E33154F001332A4 /* ProductsInfoController.swift */; };
653722811DB8282600C8F944 /* SKProduct+LocalizedPrice.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653722801DB8282600C8F944 /* SKProduct+LocalizedPrice.swift */; };
653722821DB8290A00C8F944 /* SKProduct+LocalizedPrice.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653722801DB8282600C8F944 /* SKProduct+LocalizedPrice.swift */; };
653722831DB8290B00C8F944 /* SKProduct+LocalizedPrice.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653722801DB8282600C8F944 /* SKProduct+LocalizedPrice.swift */; };
653722811DB8282600C8F944 /* SKProduct+Localized.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653722801DB8282600C8F944 /* SKProduct+Localized.swift */; };
653722821DB8290A00C8F944 /* SKProduct+Localized.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653722801DB8282600C8F944 /* SKProduct+Localized.swift */; };
653722831DB8290B00C8F944 /* SKProduct+Localized.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653722801DB8282600C8F944 /* SKProduct+Localized.swift */; };
654287F61E79F5A000F61800 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 654287F41E79F5A000F61800 /* Main.storyboard */; };
654287F81E79F5A000F61800 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 654287F71E79F5A000F61800 /* Assets.xcassets */; };
654287FD1E79F75000F61800 /* SwiftyStoreKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 54C0D52C1CF7404500F90BCE /* SwiftyStoreKit.framework */; };
Expand Down Expand Up @@ -172,7 +172,7 @@
650307F31E3177EF001332A4 /* RestorePurchasesController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RestorePurchasesController.swift; sourceTree = "<group>"; };
650307F71E317BCF001332A4 /* CompleteTransactionsController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CompleteTransactionsController.swift; sourceTree = "<group>"; };
650307FB1E33154F001332A4 /* ProductsInfoController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ProductsInfoController.swift; sourceTree = "<group>"; };
653722801DB8282600C8F944 /* SKProduct+LocalizedPrice.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "SKProduct+LocalizedPrice.swift"; sourceTree = "<group>"; };
653722801DB8282600C8F944 /* SKProduct+Localized.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "SKProduct+Localized.swift"; sourceTree = "<group>"; };
654287EE1E79F5A000F61800 /* SwiftyStoreKit_tvOSDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftyStoreKit_tvOSDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
654287F51E79F5A000F61800 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
654287F71E79F5A000F61800 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -316,7 +316,7 @@
C4A7C7621C29B8D00053ED64 /* InAppReceipt.swift */,
65E9E0781ECADF5E005CF7B4 /* InAppReceiptVerificator.swift */,
1592CD4F1E27756500D321E6 /* AppleReceiptValidator.swift */,
653722801DB8282600C8F944 /* SKProduct+LocalizedPrice.swift */,
653722801DB8282600C8F944 /* SKProduct+Localized.swift */,
C40C680F1C29414C00B60B7E /* OS.swift */,
65F7DF931DCD536100835D30 /* Platforms */,
);
Expand Down Expand Up @@ -722,7 +722,7 @@
650307F61E3177EF001332A4 /* RestorePurchasesController.swift in Sources */,
658A08391E2EC24E0074A98F /* PaymentQueueController.swift in Sources */,
65E9E07B1ECADF5E005CF7B4 /* InAppReceiptVerificator.swift in Sources */,
653722831DB8290B00C8F944 /* SKProduct+LocalizedPrice.swift in Sources */,
653722831DB8290B00C8F944 /* SKProduct+Localized.swift in Sources */,
54B069921CF742D100BAFE38 /* InAppReceipt.swift in Sources */,
650307FA1E317BCF001332A4 /* CompleteTransactionsController.swift in Sources */,
65BB6CEA1DDB018900218A0B /* SwiftyStoreKit+Types.swift in Sources */,
Expand Down Expand Up @@ -753,7 +753,7 @@
650307F41E3177EF001332A4 /* RestorePurchasesController.swift in Sources */,
658A08371E2EC24E0074A98F /* PaymentQueueController.swift in Sources */,
65E9E0791ECADF5E005CF7B4 /* InAppReceiptVerificator.swift in Sources */,
653722811DB8282600C8F944 /* SKProduct+LocalizedPrice.swift in Sources */,
653722811DB8282600C8F944 /* SKProduct+Localized.swift in Sources */,
C4A7C7631C29B8D00053ED64 /* InAppReceipt.swift in Sources */,
650307F81E317BCF001332A4 /* CompleteTransactionsController.swift in Sources */,
65BB6CE81DDB018900218A0B /* SwiftyStoreKit+Types.swift in Sources */,
Expand Down Expand Up @@ -802,7 +802,7 @@
650307F51E3177EF001332A4 /* RestorePurchasesController.swift in Sources */,
658A08381E2EC24E0074A98F /* PaymentQueueController.swift in Sources */,
65E9E07A1ECADF5E005CF7B4 /* InAppReceiptVerificator.swift in Sources */,
653722821DB8290A00C8F944 /* SKProduct+LocalizedPrice.swift in Sources */,
653722821DB8290A00C8F944 /* SKProduct+Localized.swift in Sources */,
C4083C551C2AADB500295248 /* InAppReceipt.swift in Sources */,
650307F91E317BCF001332A4 /* CompleteTransactionsController.swift in Sources */,
65BB6CE91DDB018900218A0B /* SwiftyStoreKit+Types.swift in Sources */,
Expand Down
68 changes: 68 additions & 0 deletions SwiftyStoreKit/SKProduct+Localized.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// SKProduct+LocalizedPrice.swift
// SwiftyStoreKit
//
// Created by Andrea Bizzotto on 19/10/2016.
// Copyright (c) 2015 Andrea Bizzotto (bizz84@gmail.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import StoreKit

public extension SKProduct {

public var localizedPrice: String? {
let numberFormatter = NumberFormatter()
numberFormatter.locale = self.priceLocale
numberFormatter.numberStyle = .currency
return numberFormatter.string(from: self.price)
}

@available(iOS 11.2, OSX 10.13.2, *)
public var localizedIntroductoryPrice: String? {
guard let price = introductoryPrice?.price, let locale = introductoryPrice?.priceLocale else { return nil }
let numberFormatter = NumberFormatter()
numberFormatter.locale = locale
numberFormatter.numberStyle = .currency
return numberFormatter.string(from: price)
}

@available(iOS 11.2, OSX 10.13.2, *)
public var localizedSubscriptionPeriod: String? {
guard let subscriptionPeriod = subscriptionPeriod else { return nil }
return localizedPeriod(period: subscriptionPeriod)
}

@available(iOS 11.2, OSX 10.13.2, *)
public var localizedIntroductoryPeriod: String? {
guard let introductoryPeriod = introductoryPrice?.subscriptionPeriod else { return nil }
return localizedPeriod(period: introductoryPeriod)
}

@available(iOS 11.2, OSX 10.13.2, *)
private func localizedPeriod(period: SKProductSubscriptionPeriod) -> String {
switch period.unit {
case .day: return String(format: NSLocalizedString("localized subscription period day", comment: "localized subscription period day"), arguments: [period.numberOfUnits])
case .week: return String(format: NSLocalizedString("localized subscription period week", comment: "localized subscription period week"), arguments: [period.numberOfUnits])
case .month: return String(format: NSLocalizedString("localized subscription period month", comment: "localized subscription period month"), arguments: [period.numberOfUnits])
case .year: return String(format: NSLocalizedString("localized subscription period year", comment: "localized subscription period year"), arguments: [period.numberOfUnits])
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method introduces arbitrary localized strings which are not defined in a strings table.

I think it should be up to users to decide how to format these (in other words, this is application business logic).

In short, I believe it could be convenient to introduce localizedIntroductoryPrice as you have done, but I would leave localizedSubscriptionPeriod and localizedIntroductoryPeriod out of this PR.

I might borrow some of your code to calculate localizedIntroductoryPrice and make a separate PR to show my proposed approach.


}
36 changes: 0 additions & 36 deletions SwiftyStoreKit/SKProduct+LocalizedPrice.swift

This file was deleted.