Skip to content

Commit b34e5b0

Browse files
committed
Add locale selection for flutter web
1 parent 574acfb commit b34e5b0

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

packages/stripe/lib/src/stripe.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ class Stripe {
9797
instance.markNeedsSettings();
9898
}
9999

100+
/// Retrieves the locale.
101+
/// For now works on web only.
102+
static String? get locale => instance._locale;
103+
104+
/// Sets the locale.
105+
/// For now works on web only.
106+
static set locale(String? value) {
107+
if (value == instance._locale) {
108+
return;
109+
}
110+
instance._locale = value;
111+
instance.markNeedsSettings();
112+
}
113+
100114
/// Reconfigures the Stripe platform by applying the current values for
101115
/// [publishableKey], [merchantIdentifier], [stripeAccountId],
102116
/// [threeDSecureParams], [urlScheme], [setReturnUrlSchemeOnAndroid]
@@ -107,6 +121,7 @@ class Stripe {
107121
threeDSecureParams: threeDSecureParams,
108122
urlScheme: urlScheme,
109123
setReturnUrlSchemeOnAndroid: setReturnUrlSchemeOnAndroid,
124+
locale: locale,
110125
);
111126

112127
/// Exposes a [ValueListenable] whether or not GooglePay (on Android) or Apple Pay (on iOS)
@@ -706,6 +721,7 @@ class Stripe {
706721
String? _merchantIdentifier;
707722
String? _urlScheme;
708723
bool? _setReturnUrlSchemeOnAndroid;
724+
String? _locale;
709725

710726
static StripePlatform? __platform;
711727

@@ -732,6 +748,7 @@ class Stripe {
732748
String? merchantIdentifier,
733749
String? urlScheme,
734750
bool? setReturnUrlSchemeOnAndroid,
751+
String? locale,
735752
}) async {
736753
_needsSettings = false;
737754
await _platform.initialise(
@@ -741,6 +758,7 @@ class Stripe {
741758
merchantIdentifier: merchantIdentifier,
742759
urlScheme: urlScheme,
743760
setReturnUrlSchemeOnAndroid: setReturnUrlSchemeOnAndroid,
761+
locale: locale,
744762
);
745763
}
746764

packages/stripe_platform_interface/lib/src/method_channel_stripe.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class MethodChannelStripe extends StripePlatform {
5353
String? merchantIdentifier,
5454
String? urlScheme,
5555
bool? setReturnUrlSchemeOnAndroid,
56+
String? locale,
5657
}) async {
5758
await _methodChannel.invokeMethod('initialise', {
5859
'publishableKey': publishableKey,
@@ -62,6 +63,7 @@ class MethodChannelStripe extends StripePlatform {
6263
'threeDSecureParams': threeDSecureParams,
6364
'urlScheme': urlScheme,
6465
'setReturnUrlSchemeOnAndroid': setReturnUrlSchemeOnAndroid,
66+
'locale': locale,
6567
});
6668

6769
_methodChannel.setMethodCallHandler((call) async {

packages/stripe_platform_interface/lib/src/stripe_platform_interface.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ abstract class StripePlatform extends PlatformInterface {
3232
String? merchantIdentifier,
3333
String? urlScheme,
3434
bool? setReturnUrlSchemeOnAndroid,
35+
String? locale,
3536
});
3637

3738
Future<PaymentMethod> createPaymentMethod(

packages/stripe_web/lib/src/web_stripe.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,23 @@ class WebStripe extends StripePlatform {
5252
String? merchantIdentifier,
5353
String? urlScheme,
5454
bool? setReturnUrlSchemeOnAndroid,
55+
String? locale,
5556
}) async {
5657
_urlScheme = urlScheme;
5758

5859
if (__stripe != null) {
5960
// Check if the new stripeAccountId is different
60-
if (__stripe!.stripeAccount != stripeAccountId) {
61+
if (__stripe!.stripeAccount != stripeAccountId || __stripe!.locale != locale) {
6162
// Re-initialize with new stripeAccountId
6263
await stripe_js.loadStripe();
6364
var stripeOption = stripe_js.StripeOptions();
64-
stripeOption.stripeAccount = stripeAccountId;
65+
if (__stripe!.stripeAccount != stripeAccountId) {
66+
stripeOption.stripeAccount = stripeAccountId;
67+
}
68+
if (locale != null && __stripe!.locale != locale) {
69+
stripeOption.locale = locale;
70+
}
71+
6572
__stripe = stripe_js.Stripe(publishableKey, stripeOption);
6673
}
6774
return;
@@ -72,6 +79,9 @@ class WebStripe extends StripePlatform {
7279
if (stripeAccountId != null) {
7380
stripeOption.stripeAccount = stripeAccountId;
7481
}
82+
if (locale != null) {
83+
stripeOption.locale = locale;
84+
}
7585
__stripe = stripe_js.Stripe(publishableKey, stripeOption);
7686
}
7787

0 commit comments

Comments
 (0)