Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.onfido.android.sdk.capture.ui.options.stepbuilder.DocumentCaptureStep
import com.onfido.sdk.flutter.helpers.CustomMediaCallback
import io.flutter.embedding.engine.plugins.FlutterPlugin
import com.onfido.android.sdk.capture.model.NFCOptions
import java.util.Locale

internal fun Any?.deserializeOnfidoBuilder(
context: Context
Expand All @@ -22,6 +23,10 @@ internal fun Any?.deserializeOnfidoBuilder(
builder.withSDKToken(it)
}

(this["locale"] as? String)?.takeIf { it.isNotBlank() }?.let { localeTag ->
builder.withLocale(Locale.forLanguageTag(localeTag))
}

val flowSteps = this["flowSteps"] as? Map<*, *>
?: throw Exception("Invalid arguments for start method (flow steps)")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.onfido.workflow.WorkflowConfig
import com.onfido.android.sdk.capture.EnterpriseFeatures
import com.onfido.sdk.flutter.helpers.BiometricTokenCallbackBridge
import com.onfido.sdk.flutter.helpers.CustomMediaCallback
import java.util.Locale

fun Any.deserializeWorkflowConfig(): WorkflowConfig {
if (this !is Map<*, *>) throw Exception("Invalid arguments for start method")
Expand All @@ -16,6 +17,10 @@ fun Any.deserializeWorkflowConfig(): WorkflowConfig {

val builder = WorkflowConfig.Builder(sdkToken, workflowRunId)

(this["locale"] as? String)?.takeIf { it.isNotBlank() }?.let { localeTag ->
builder.withLocale(Locale.forLanguageTag(localeTag))
}

val withMediaCallback = this["shouldUseMediaCallback"] as? Boolean ?: false
if (withMediaCallback) {
builder.withMediaCallback(mediaCallback = CustomMediaCallback())
Expand Down
81 changes: 81 additions & 0 deletions ios/Classes/Helpers/OnfidoLocalizationHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import Foundation
import Onfido

enum OnfidoLocalizationHelper {
private static var onfidoBundle: Bundle {
Bundle(for: WorkflowConfiguration.self)
}

static func localizedBundle(for locale: String) -> Bundle? {
for candidate in localizationCandidates(for: locale) {
guard let path = onfidoBundle.path(forResource: candidate, ofType: "lproj"),
let bundle = Bundle(path: path) else {
continue
}
return bundle
}
return nil
}

static func applyLocalization(
to builder: OnfidoConfigBuilder,
locale: String?,
customLocalizationFileName: String?
) {
if let customLocalizationFileName {
builder.withCustomLocalization(andTableName: customLocalizationFileName)
return
}

guard let locale, !locale.isEmpty, let localizedBundle = localizedBundle(for: locale) else {
return
}

builder.withCustomLocalization(andTableName: "Localizable", in: localizedBundle)
}

static func applyLocalization(
to configuration: WorkflowConfiguration,
locale: String?,
customLocalizationFileName: String?
) {
if let customLocalizationFileName {
configuration.withCustomLocalization(withTableName: customLocalizationFileName, in: .main)
return
}

guard let locale, !locale.isEmpty, let localizedBundle = localizedBundle(for: locale) else {
return
}

configuration.withCustomLocalization(withTableName: "Localizable", in: localizedBundle)
}

private static func localizationCandidates(for locale: String) -> [String] {
let normalized = locale.replacingOccurrences(of: "_", with: "-")
let components = normalized.split(separator: "-", omittingEmptySubsequences: true)

guard let language = components.first else {
return [normalized]
}

var candidates = [String]()

if components.count >= 2 {
let region = String(components[1])
candidates.append("\(language)-\(region)")
candidates.append("\(language)-\(region.uppercased())")
candidates.append("\(language)-\(region.lowercased())")
}

candidates.append(String(language))
candidates.append(normalized)

var uniqueCandidates: [String] = []
for candidate in candidates where !uniqueCandidates.contains(candidate) {
uniqueCandidates.append(candidate)
}

return uniqueCandidates
}
}
8 changes: 5 additions & 3 deletions ios/Classes/Serializer/OnfidoBuilderSerializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ extension OnfidoConfig {
onfidoBuilder.withSDKToken(token)
}

if let fileName = dictionary["iosLocalizationFileName"] as? String {
onfidoBuilder.withCustomLocalization(andTableName: fileName)
}
OnfidoLocalizationHelper.applyLocalization(
to: onfidoBuilder,
locale: dictionary["locale"] as? String,
customLocalizationFileName: dictionary["iosLocalizationFileName"] as? String
)

var appearance = Appearance()
if let iosAppearance = dictionary["iosAppearance"] as? NSDictionary {
Expand Down
8 changes: 5 additions & 3 deletions ios/Classes/Serializer/WorkflowConfigurationSerializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ extension WorkflowConfiguration {
if let enterpriseFeatures = dictionary["enterpriseFeatures"] as? NSDictionary {
withEnterpriseFeatures(EnterpriseFeatures.builder(with: enterpriseFeatures))
}
if let fileName = dictionary["iosLocalizationFileName"] as? String {
withCustomLocalization(withTableName: fileName, in: Bundle.self.main)
}
OnfidoLocalizationHelper.applyLocalization(
to: self,
locale: dictionary["locale"] as? String,
customLocalizationFileName: dictionary["iosLocalizationFileName"] as? String
)

var appearance = Appearance()
if let iosAppearance = dictionary["iosAppearance"] as? NSDictionary {
Expand Down
5 changes: 5 additions & 0 deletions lib/src/onfido.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Onfido {
Onfido(
{required String sdkToken,
String? iosLocalizationFileName,
String? locale,
IOSAppearance? iosAppearance,
EnterpriseFeatures? enterpriseFeatures,
NFCOptions? nfcOption,
Expand All @@ -22,6 +23,7 @@ class Onfido {
OnfidoTheme? onfidoTheme})
: _sdkToken = sdkToken,
_iOSLocalizationFileName = iosLocalizationFileName,
_locale = locale,
_enterpriseFeatures = enterpriseFeatures,
_iosAppearance = iosAppearance,
_nfcOption = nfcOption,
Expand All @@ -31,6 +33,7 @@ class Onfido {

final String _sdkToken;
final String? _iOSLocalizationFileName;
final String? _locale;
final EnterpriseFeatures? _enterpriseFeatures;
final IOSAppearance? _iosAppearance;
final NFCOptions? _nfcOption;
Expand All @@ -46,6 +49,7 @@ class Onfido {
flowSteps: flowSteps,
iosAppearance: _iosAppearance,
iosLocalizationFileName: _iOSLocalizationFileName,
locale: _locale,
enterpriseFeatures: _enterpriseFeatures,
nfcOption: _nfcOption,
mediaCallback: _mediaCallback,
Expand All @@ -61,6 +65,7 @@ class Onfido {
mediaCallback: _mediaCallback,
biometricTokenCallback: _biometricTokenCallback,
iosLocalizationFileName: _iOSLocalizationFileName,
locale: _locale,
enterpriseFeatures: _enterpriseFeatures,
onfidoTheme: _onfidoTheme);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/src/platform/onfido_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class MethodChannelOnfido extends OnfidoPlatform {
{required String sdkToken,
required FlowSteps flowSteps,
String? iosLocalizationFileName,
String? locale,
IOSAppearance? iosAppearance,
OnfidoMediaCallback? mediaCallback,
EnterpriseFeatures? enterpriseFeatures,
Expand All @@ -37,6 +38,7 @@ class MethodChannelOnfido extends OnfidoPlatform {
iosAppearance: iosAppearance,
shouldUseMediaCallback: mediaCallback != null,
iosLocalizationFileName: iosLocalizationFileName,
locale: locale,
enterpriseFeatures: enterpriseFeatures,
nfcOption: nfcOption,
onfidoTheme: onfidoTheme);
Expand All @@ -55,6 +57,7 @@ class MethodChannelOnfido extends OnfidoPlatform {
OnfidoMediaCallback? mediaCallback,
BiometricTokenCallback? biometricTokenCallback,
String? iosLocalizationFileName,
String? locale,
EnterpriseFeatures? enterpriseFeatures,
OnfidoTheme? onfidoTheme}) async {
final arguments = StartStudioSerializer.serialize(
Expand All @@ -64,6 +67,7 @@ class MethodChannelOnfido extends OnfidoPlatform {
shouldUseMediaCallback: mediaCallback != null,
shouldUseBiometricTokenCallback: biometricTokenCallback != null,
iosLocalizationFileName: iosLocalizationFileName,
locale: locale,
enterpriseFeatures: enterpriseFeatures,
onfidoTheme: onfidoTheme);

Expand Down
2 changes: 2 additions & 0 deletions lib/src/platform/onfido_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ abstract class OnfidoPlatform {
{required String sdkToken,
required FlowSteps flowSteps,
String? iosLocalizationFileName,
String? locale,
IOSAppearance? iosAppearance,
EnterpriseFeatures? enterpriseFeatures,
NFCOptions? nfcOption,
Expand All @@ -22,6 +23,7 @@ abstract class OnfidoPlatform {
OnfidoMediaCallback? mediaCallback,
BiometricTokenCallback? biometricTokenCallback,
String? iosLocalizationFileName,
String? locale,
EnterpriseFeatures? enterpriseFeatures,
OnfidoTheme? onfidoTheme});
}
2 changes: 2 additions & 0 deletions lib/src/serializer/start_options_serializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class StartOptionsSerializer {
bool? shouldUseMediaCallback,
EnterpriseFeatures? enterpriseFeatures,
String? iosLocalizationFileName,
String? locale,
bool? disableNFC,
NFCOptions? nfcOption,
OnfidoTheme? onfidoTheme}) {
Expand All @@ -16,6 +17,7 @@ class StartOptionsSerializer {
'flowSteps': flowSteps.toJson(),
'iosAppearance': iosAppearance?.toJson(),
'iosLocalizationFileName': iosLocalizationFileName,
'locale': locale,
'nfcOption': nfcOption?.name,
'shouldUseMediaCallback': shouldUseMediaCallback,
'enterpriseFeatures': enterpriseFeatures?.toJson(),
Expand Down
2 changes: 2 additions & 0 deletions lib/src/serializer/start_studio_serializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class StartStudioSerializer {
required String workflowRunId,
IOSAppearance? iosAppearance,
String? iosLocalizationFileName,
String? locale,
bool? shouldUseMediaCallback,
bool? shouldUseBiometricTokenCallback,
EnterpriseFeatures? enterpriseFeatures,
Expand All @@ -16,6 +17,7 @@ class StartStudioSerializer {
'workflowRunId': workflowRunId,
'iosAppearance': iosAppearance?.toJson(),
'iosLocalizationFileName': iosLocalizationFileName,
'locale': locale,
'shouldUseMediaCallback': shouldUseMediaCallback,
'shouldUseBiometricTokenCallback': shouldUseBiometricTokenCallback,
'enterpriseFeatures': enterpriseFeatures?.toJson(),
Expand Down