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

Fixed issues with finding some locales (ex: zh-Hans and zh-Hant) #4771

Merged
merged 3 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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: 16 additions & 0 deletions RevenueCat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/* Begin PBXBuildFile section */
030890812D2B764D0069677B /* VariableHandlerV2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 030890802D2B76450069677B /* VariableHandlerV2.swift */; };
030890842D2B77E70069677B /* VariableHandlerV2Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 030890832D2B77E20069677B /* VariableHandlerV2Tests.swift */; };
030F918A2D55C1D20085103F /* LocaleFinder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 030F91892D55C1AB0085103F /* LocaleFinder.swift */; };
030F918C2D55C9DC0085103F /* LocaleFinderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 030F918B2D55C9D80085103F /* LocaleFinderTests.swift */; };
0313FD41268A506400168386 /* DateProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0313FD40268A506400168386 /* DateProvider.swift */; };
0354AA462D4029C300F9E330 /* TabControlButtonComponentViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0354AA3E2D4029C300F9E330 /* TabControlButtonComponentViewModel.swift */; };
0354AA472D4029C300F9E330 /* TabControlComponentViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0354AA402D4029C300F9E330 /* TabControlComponentViewModel.swift */; };
Expand Down Expand Up @@ -1283,6 +1285,8 @@
/* Begin PBXFileReference section */
030890802D2B76450069677B /* VariableHandlerV2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VariableHandlerV2.swift; sourceTree = "<group>"; };
030890832D2B77E20069677B /* VariableHandlerV2Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VariableHandlerV2Tests.swift; sourceTree = "<group>"; };
030F91892D55C1AB0085103F /* LocaleFinder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocaleFinder.swift; sourceTree = "<group>"; };
030F918B2D55C9D80085103F /* LocaleFinderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocaleFinderTests.swift; sourceTree = "<group>"; };
0313FD40268A506400168386 /* DateProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateProvider.swift; sourceTree = "<group>"; };
0354AA3D2D4029C300F9E330 /* TabControlButtonComponentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabControlButtonComponentView.swift; sourceTree = "<group>"; };
0354AA3E2D4029C300F9E330 /* TabControlButtonComponentViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabControlButtonComponentViewModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2541,12 +2545,21 @@
030890822D2B77DD0069677B /* PaywallsV2 */ = {
isa = PBXGroup;
children = (
030F918B2D55C9D80085103F /* LocaleFinderTests.swift */,
03C06FC42D4553BB00600693 /* PresentedPartialsTests.swift */,
030890832D2B77E20069677B /* VariableHandlerV2Tests.swift */,
);
path = PaywallsV2;
sourceTree = "<group>";
};
030F91882D55C0FF0085103F /* Localizations */ = {
isa = PBXGroup;
children = (
030F91892D55C1AB0085103F /* LocaleFinder.swift */,
);
path = Localizations;
sourceTree = "<group>";
};
0354AA452D4029C300F9E330 /* Tabs */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -4956,6 +4969,7 @@
children = (
88B1BAE32C813A3C001B7EE5 /* PaywallsV2View.swift */,
2C7457492CEA6B06004ACE52 /* EnvironmentObjects */,
030F91882D55C0FF0085103F /* Localizations */,
0308907F2D2B76340069677B /* Variables */,
2C7457442CEA652B004ACE52 /* ViewHelpers */,
2C7457452CEA653A004ACE52 /* ViewModelHelpers */,
Expand Down Expand Up @@ -6810,6 +6824,7 @@
887A60CA2C1D037000E1A461 /* RemoteImage.swift in Sources */,
887A607B2C1D037000E1A461 /* Bundle+Extensions.swift in Sources */,
03C72F6D2D32CDFB00297FEC /* FamilySharingTogglePreview.swift in Sources */,
030F918A2D55C1D20085103F /* LocaleFinder.swift in Sources */,
353756662C382C2800A1B8D6 /* CustomerCenterError.swift in Sources */,
887A60CF2C1D037000E1A461 /* View+PresentPaywallFooter.swift in Sources */,
3537566B2C382C2800A1B8D6 /* CustomerCenterView.swift in Sources */,
Expand Down Expand Up @@ -6940,6 +6955,7 @@
88AD4C482C24E8EA00943C3E /* ExternalPurchaseAndRestoreTests.swift in Sources */,
887A633D2C1D177800E1A461 /* Template2ViewTests.swift in Sources */,
887A633E2C1D177800E1A461 /* Template3ViewTests.swift in Sources */,
030F918C2D55C9DC0085103F /* LocaleFinderTests.swift in Sources */,
887A633F2C1D177800E1A461 /* Template4ViewTests.swift in Sources */,
887A63402C1D177800E1A461 /* Template5ViewTests.swift in Sources */,
887A63412C1D177800E1A461 /* BaseSnapshotTest.swift in Sources */,
Expand Down
34 changes: 34 additions & 0 deletions RevenueCatUI/Templates/V2/Localizations/LocaleFinder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// LocaleFinder.swift
//
// Created by Josh Holtz on 2/6/25.

import Foundation

extension Dictionary where Key == String {

func findLocale(_ locale: Locale) -> Value? {
let localeIdentifier = locale.identifier

if let exactMatch = self[localeIdentifier] {
return exactMatch
}

// For zh-Hans and zh-Hant
let underscoreUocaleIdentifier = localeIdentifier.replacingOccurrences(of: "-", with: "_")
if let exactMatch = self[underscoreUocaleIdentifier] {
return exactMatch
}

return nil
}

}
4 changes: 2 additions & 2 deletions RevenueCatUI/Templates/V2/PaywallsV2View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,9 @@ fileprivate extension PaywallsV2View {
let chosenLocale = Self.preferredLocale(from: paywallLocales) ?? defaultLocale

// STEP 3: Get localization for one of preferred locales in order
if let localizedStrings = componentsLocalizations[chosenLocale.identifier] {
if let localizedStrings = componentsLocalizations.findLocale(chosenLocale) {
return .init(locale: chosenLocale, localizedStrings: localizedStrings)
} else if let localizedStrings = componentsLocalizations[defaultLocale.identifier] {
} else if let localizedStrings = componentsLocalizations.findLocale(defaultLocale) {
Logger.error(Strings.paywall_could_not_find_localization("\(chosenLocale)"))
return .init(locale: defaultLocale, localizedStrings: localizedStrings)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct UIConfigProvider {
}

func getLocalizations(for locale: Locale) -> [String: String] {
guard let localizations = self.uiConfig.localizations[locale.identifier] else {
guard let localizations = self.uiConfig.localizations.findLocale(locale) else {
Logger.error("Could not find localizations for '\(locale.identifier)'")
return [:]
}
Expand Down
49 changes: 49 additions & 0 deletions Tests/RevenueCatUITests/PaywallsV2/LocaleFinderTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// LocaleFinderTests.swift
//
// Created by Josh Holtz on 2/6/25.

import Nimble
import RevenueCat
@testable import RevenueCatUI
import XCTest

@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
class LocaleFinderTest: TestCase {

static let expectedTranslations = ["expected": "translations"]
static let wrongTranslations = ["wrong": "translations"]

func test_en_US() {
let localizations = [
"en_US": Self.expectedTranslations,
"es_ES": Self.wrongTranslations
]

let locale = Locale(identifier: "en_US")

let foundLocalizations = localizations.findLocale(locale)
expect(foundLocalizations).to(equal(Self.expectedTranslations))
}

func test_zh_Hans() {
let localizations = [
"zh_Hans": Self.expectedTranslations,
"es_ES": Self.wrongTranslations
]

let locale = Locale(identifier: "zh-Hans")

let foundLocalizations = localizations.findLocale(locale)
expect(foundLocalizations).to(equal(Self.expectedTranslations))
}

}