Skip to main content
Solved

Localization issue, duration always returned in english


Forum|alt.badge.img+4

We’re trying to display the duration of a subscription using Swift and the IOS SDK but the locale isn’t honored and the duration is always return in english, e.g. “1 Year” instead of “1 Jahr” for german with “de” locale. Does anyone have the same issue?

We’re trying to access the localized duration like so:

package.product.subscriptionPeriod?.durationTitle.localizedLowercase

Any tipps?

Best answer by cody

Hey @etrality! Thanks for the clarification, I just tested this and it seems to work:

If you modify the `format` method from that Stack Overflow post to include a specific locale for the calendar, this translation will work as expected. Here’s my new `format` method I’m using:

    static func format(unit: NSCalendar.Unit, numberOfUnits: Int) -> String? {
        var calendar = Calendar.current
        calendar.locale = Locale.current // to test German: Locale(identifier: "de")

        var dateComponents = DateComponents()
        dateComponents.calendar = calendar
        componentFormatter.allowedUnits = [unit]
        switch unit {
        case .day:
            dateComponents.setValue(numberOfUnits, for: .day)
        case .weekOfMonth:
            dateComponents.setValue(numberOfUnits, for: .weekOfMonth)
        case .month:
            dateComponents.setValue(numberOfUnits, for: .month)
        case .year:
            dateComponents.setValue(numberOfUnits, for: .year)
        default:
            return nil
        }

        return componentFormatter.string(from: dateComponents)
    }

When I print the period method like this:

print(package.product.subscriptionPeriod?.localizedPeriod())

An Optional was printed to the console:

1 Jahr

 

Let me know if that works for you, too!

View original
Did this post help you find an answer to your question?

3 replies

cody
RevenueCat Staff
Forum|alt.badge.img+8
  • RevenueCat Staff
  • 492 replies
  • July 23, 2021

Hey @etrality!

It looks like maybe you’re referencing this method from our sample app, durationTitle. This is just meant to be an example of how you might format your durations for display in your app, but doesn’t provide any localization.

A more robust way to localize this information would be to use a date formatter, as referenced in this Stack Overflow post. Essentially, that answer provides an example extension of how you might convert SKProductSubscriptionPeriod units to localized titles to display in your paywall.


Forum|alt.badge.img+4
  • Author
  • New Member
  • 1 reply
  • July 23, 2021

@cody Thanks Cody! If I understand it correctly this handles localization, meaning the formatting of time and pricing. What about the translation of human readable intervals like week, year, etc. into a different language?  


cody
RevenueCat Staff
Forum|alt.badge.img+8
  • RevenueCat Staff
  • 492 replies
  • Answer
  • July 23, 2021

Hey @etrality! Thanks for the clarification, I just tested this and it seems to work:

If you modify the `format` method from that Stack Overflow post to include a specific locale for the calendar, this translation will work as expected. Here’s my new `format` method I’m using:

    static func format(unit: NSCalendar.Unit, numberOfUnits: Int) -> String? {
        var calendar = Calendar.current
        calendar.locale = Locale.current // to test German: Locale(identifier: "de")

        var dateComponents = DateComponents()
        dateComponents.calendar = calendar
        componentFormatter.allowedUnits = [unit]
        switch unit {
        case .day:
            dateComponents.setValue(numberOfUnits, for: .day)
        case .weekOfMonth:
            dateComponents.setValue(numberOfUnits, for: .weekOfMonth)
        case .month:
            dateComponents.setValue(numberOfUnits, for: .month)
        case .year:
            dateComponents.setValue(numberOfUnits, for: .year)
        default:
            return nil
        }

        return componentFormatter.string(from: dateComponents)
    }

When I print the period method like this:

print(package.product.subscriptionPeriod?.localizedPeriod())

An Optional was printed to the console:

1 Jahr

 

Let me know if that works for you, too!


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings