fix: setLanguage and icon issue for the iOS SDK (#5470)

This commit is contained in:
Anshuman Pandey
2025-04-22 18:48:28 +05:30
committed by GitHub
parent d6a7a2c21f
commit 31c742f7a8
4 changed files with 83 additions and 4 deletions
@@ -62,7 +62,19 @@ final class SurveyManager {
// Display percentage
let shouldDisplay = shouldDisplayBasedOnPercentage(firstSurveyWithActionClass?.displayPercentage)
let isMultiLangSurvey = firstSurveyWithActionClass?.languages?.count ?? 0 > 1
if isMultiLangSurvey {
guard let survey = firstSurveyWithActionClass else {return}
let currentLanguage = Formbricks.language
guard let languageCode = getLanguageCode(survey: survey, language: currentLanguage) else {
Formbricks.logger?.error("Survey \(survey.name) is not available in language “\(currentLanguage)”. Skipping.")
return
}
Formbricks.language = languageCode
}
// Display and delay it if needed
if let surveyId = firstSurveyWithActionClass?.id, shouldDisplay {
isShowingSurvey = true
@@ -234,6 +246,47 @@ private extension SurveyManager {
}
}
func getLanguageCode(
survey: Survey,
language: String?
) -> String? {
// 1) Collect all codes
let availableLanguageCodes = survey.languages?
.map { $0.language.code }
// 2) If no language was passed or it's the explicit "default" token default
guard let raw = language?.lowercased(), !raw.isEmpty else {
return "default"
}
if raw == "default" {
return "default"
}
// 3) Find matching entry by code or alias
let selected = survey.languages?.first { entry in
entry.language.code.lowercased() == raw ||
entry.language.alias?.lowercased() == raw
}
// 4) If that entry is marked default default
if selected?.isDefault == true {
return "default"
}
// 5) If no entry, or not enabled, or code not in the available list nil
guard
let entry = selected,
entry.enabled,
availableLanguageCodes?.contains(entry.language.code) == true
else {
return nil
}
// 6) Otherwise return its code
return entry.language.code
}
/// Filters the surveys based on the user's segments.
func filterSurveysBasedOnSegments(_ surveys: [Survey], segments: [String]) -> [Survey] {
return surveys.filter { survey in
@@ -5,6 +5,25 @@ enum DisplayOptionType: String, Codable {
case displaySome = "displaySome"
}
struct SurveyLanguage: Codable {
let enabled: Bool
let isDefault: Bool // must differ from "default" in JSON
let language: LanguageDetail
private enum CodingKeys: String, CodingKey {
case enabled
case isDefault = "default"
case language
}
}
struct LanguageDetail: Codable {
let id: String
let code: String
let alias: String?
let projectId: String
}
struct Survey: Codable {
let id: String
let name: String
@@ -16,5 +35,5 @@ struct Survey: Codable {
let displayOption: DisplayOptionType?
let segment: Segment?
let styling: Styling?
let languages: [SurveyLanguage]?
}
@@ -86,12 +86,19 @@ private class WebViewData {
init(environmentResponse: EnvironmentResponse, surveyId: String) {
data["survey"] = environmentResponse.getSurveyJson(forSurveyId: surveyId)
data["isBrandingEnabled"] = true
data["languageCode"] = Formbricks.language
data["appUrl"] = Formbricks.appUrl
data["environmentId"] = Formbricks.environmentId
data["contactId"] = Formbricks.userManager?.contactId
data["isWebEnvironment"] = false
let isMultiLangSurvey = environmentResponse.data.data.surveys?.first(where: { $0.id == surveyId })?.languages?.count ?? 0 > 1
if isMultiLangSurvey {
data["languageCode"] = Formbricks.language
} else {
data["languageCode"] = "default"
}
let hasCustomStyling = environmentResponse.data.data.surveys?.first(where: { $0.id == surveyId })?.styling != nil
let enabled = environmentResponse.data.data.project.styling?.allowStyleOverwrite ?? false
@@ -10,7 +10,7 @@ export function SurveyCloseButton({ onClose }: SurveyCloseButtonProps) {
onClick={onClose}
className="fb-text-heading fb-relative fb-h-5 fb-w-5 fb-rounded-md hover:fb-bg-black/5 focus:fb-outline-none focus:fb-ring-2 focus:fb-ring-offset-2">
<svg
className="fb-h-5 fb-w-5 fb-p-0.5"
className="fb-h-6 fb-w-6 fb-p-0.5"
fill="none"
viewBox="0 0 24 24"
strokeWidth="1"