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

Fix period abbreviated when multiple days/weeks/months/years #4769

Merged
merged 4 commits into from
Feb 7, 2025
Merged
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
41 changes: 39 additions & 2 deletions RevenueCatUI/Templates/V2/Variables/VariableHandlerV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ private enum VariableLocalizationKey: String {
case numYearFew = "num_year_few"
case numYearMany = "num_year_many"
case numYearOther = "num_year_other"
case numDaysShort = "num_days_short"
case numWeeksShort = "num_weeks_short"
case numMonthsShort = "num_months_short"
case numYearsShort = "num_years_short"
}

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
Expand Down Expand Up @@ -336,6 +340,11 @@ extension VariablesV2 {
return ""
}

// Ex: "3 months" will return as "3 months"
if period.value > 1 {
return self.productPeriodWithUnit(package: package, localizations: localizations)
}

let value: String
switch period.unit {
case .day:
Expand Down Expand Up @@ -372,15 +381,43 @@ extension VariablesV2 {
return ""
}

return localizations[period.periodLocalizationKey] ?? ""
if period.value > 1 {
return self.productPeriodWithUnit(package: package, localizations: localizations)
} else {
return localizations[period.periodLocalizationKey] ?? ""
}
}

func productPeriodAbbreviated(package: Package, localizations: [String: String]) -> String {
guard let period = package.storeProduct.subscriptionPeriod else {
return ""
}

return localizations[period.periodAbbreviatedLocalizationKey] ?? ""
if period.value > 1 {
let localizedFormatKey: String
switch period.unit {
case .day:
localizedFormatKey = VariableLocalizationKey.numDaysShort.rawValue
case .week:
localizedFormatKey = VariableLocalizationKey.numWeeksShort.rawValue
case .month:
localizedFormatKey = VariableLocalizationKey.numMonthsShort.rawValue
case .year:
localizedFormatKey = VariableLocalizationKey.numYearsShort.rawValue
}

guard let localizedFormat = localizations[localizedFormatKey] else {
return ""
}
return String(format: localizedFormat, period.value)

} else {
guard let abbreviation = localizations[period.periodAbbreviatedLocalizationKey] else {
return ""
}

return abbreviation
}
}

func productPeriodInDays(package: Package) -> String {
Expand Down
56 changes: 55 additions & 1 deletion Tests/RevenueCatUITests/PaywallsV2/VariableHandlerV2Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ class VariableHandlerV2Test: TestCase {
"num_year_two": "%d years",
"num_year_few": "%d years",
"num_year_many": "%d years",
"num_year_other": "%d years"
"num_year_other": "%d years",
"num_days_short": "%dd",
"num_weeks_short": "%dwk",
"num_months_short": "%dmo",
"num_years_short": "%dyr"
]
]

Expand Down Expand Up @@ -116,6 +120,16 @@ class VariableHandlerV2Test: TestCase {
expect(result).to(equal("monthly"))
}

func testProductPeriodlyMultipleMonths() {
let result = variableHandler.processVariables(
in: "{{ product.periodly }}",
with: TestData.threeMonthPackage,
locale: locale,
localizations: localizations["en_US"]!
)
expect(result).to(equal("3 months"))
}

func testProductPrice() {
let result = variableHandler.processVariables(
in: "{{ product.price }}",
Expand All @@ -136,6 +150,16 @@ class VariableHandlerV2Test: TestCase {
expect(result).to(equal("$6.99/month"))
}

func testProductPricePerPeriodMultipleMonths() {
let result = variableHandler.processVariables(
in: "{{ product.price_per_period }}",
with: TestData.threeMonthPackage,
locale: locale,
localizations: localizations["en_US"]!
)
expect(result).to(equal("$4.99/3 months"))
}

func testProductPricePerPeriodAbbreviated() {
let result = variableHandler.processVariables(
in: "{{ product.price_per_period_abbreviated }}",
Expand All @@ -146,6 +170,16 @@ class VariableHandlerV2Test: TestCase {
expect(result).to(equal("$6.99/mo"))
}

func testProductPricePerPeriodAbbreviatedMultipleMonths() {
let result = variableHandler.processVariables(
in: "{{ product.price_per_period_abbreviated }}",
with: TestData.threeMonthPackage,
locale: locale,
localizations: localizations["en_US"]!
)
expect(result).to(equal("$4.99/3mo"))
}

func testProductPricePerDay() {
let result = variableHandler.processVariables(
in: "{{ product.price_per_day }}",
Expand Down Expand Up @@ -196,6 +230,16 @@ class VariableHandlerV2Test: TestCase {
expect(result).to(equal("month"))
}

func testProductPeriodMultipleMonths() {
let result = variableHandler.processVariables(
in: "{{ product.period }}",
with: TestData.threeMonthPackage,
locale: locale,
localizations: localizations["en_US"]!
)
expect(result).to(equal("3 months"))
}

func testProductPeriodAbbreviated() {
let result = variableHandler.processVariables(
in: "{{ product.period_abbreviated }}",
Expand All @@ -206,6 +250,16 @@ class VariableHandlerV2Test: TestCase {
expect(result).to(equal("mo"))
}

func testProductPeriodAbbreviatedMultipleMonths() {
let result = variableHandler.processVariables(
in: "{{ product.period_abbreviated }}",
with: TestData.threeMonthPackage,
locale: locale,
localizations: localizations["en_US"]!
)
expect(result).to(equal("3mo"))
}

func testProductPeriodInDays() {
let result = variableHandler.processVariables(
in: "{{ product.period_in_days }}",
Expand Down