简体   繁体   English

基于 i18n 语言的 React Stripe Elements 占位符

[英]React Stripe Elements placeholder based on i18n language

I want to change the placeholder of the CardElement based on i18n language.我想根据 i18n 语言更改 CardElement 的占位符。 Im passing the locale as option when loading stripe我在加载条带时将语言环境作为选项传递

stripePromise = loadStripe(key, {   locale: i18nInstance.language })

It did work but when changing the language with the switcher it doesn't reflect on the placeholders.How can i update the options accordingly knowing that Loading Stripe in useEffect has side effects它确实有效,但是当使用切换器更改语言时,它不会反映在占位符上。知道在 useEffect 中加载 Stripe 有副作用,我如何相应地更新选项

Stripe also allows you to use the Elements Props to insert the locale like this: Stripe 还允许您使用Elements Props插入区域设置,如下所示:

<Elements stripe={stripePromise} options={ locale: i18nInstance.language }>

Adding the locale to options will give you more flexibility to handle this value inside the component instead of outside as they suggest for loadStripe .将语言环境添加到选项将使您更灵活地在组件内部处理此值,而不是像他们建议的那样在外部处理loadStripe

I am not using the same library for i18n but here is an example with NextJs and TS我没有为i18n使用相同的库,但这里是 NextJs 和 TS 的示例

import React from 'react'
import {
  loadStripe,
  StripeElementLocale,
} from '@stripe/stripe-js'
import { Elements } from '@stripe/react-stripe-js'
import { useRouter } from 'next/router'
import Checkout from 'library/organisms/Checkout'

// Make sure to call `loadStripe` outside of a component’s render to avoid
// recreating the `Stripe` object on every render.
const stripePromise = loadStripe('ADD YOUR KEY')

const CheckoutPage = () => {
  const { locale } = useRouter()

  // The following line has been simplified to only check if the locale is 'en'
  // so TS don't complain but you could develope it for more languages.
  const stripeLocale: StripeElementLocale = locale === 'en' ? locale : 'auto'

  return (
    <Elements stripe={stripePromise} options={ locale: stripeLocale }>
      <Checkout />
    </Elements>
  )
}

export default CheckoutPage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM