feat: add translations for countrycode selector (phone)

This commit is contained in:
Simon Emanuelsson
2025-06-24 15:39:20 +02:00
committed by Simon.Emanuelsson
parent 15adca9eb1
commit 64b3bd71dc

View File

@@ -1,12 +1,15 @@
"use client" "use client"
import "react-international-phone/style.css" import "react-international-phone/style.css"
import { useEffect } from "react" import { useEffect, useMemo } from "react"
import { TextField } from "react-aria-components" import { TextField } from "react-aria-components"
import { useFormContext, useWatch } from "react-hook-form" import { useFormContext, useWatch } from "react-hook-form"
import { import {
buildCountryData,
CountrySelector, CountrySelector,
defaultCountries,
DialCodePreview, DialCodePreview,
parseCountry,
type ParsedCountry, type ParsedCountry,
usePhoneInput, usePhoneInput,
} from "react-international-phone" } from "react-international-phone"
@@ -41,6 +44,21 @@ export default function Phone({
}: PhoneProps) { }: PhoneProps) {
const intl = useIntl() const intl = useIntl()
const lang = useLang() const lang = useLang()
const countries = useMemo(() => {
return defaultCountries.map((dc) => {
const country = parseCountry(dc)
const translatedCountryName = intl.formatDisplayName(
country.iso2.toUpperCase(),
{ type: "region" }
)
if (translatedCountryName) {
country.name = translatedCountryName
}
return buildCountryData(country)
})
}, [intl])
const { formState, getFieldState, register, setValue } = useFormContext() const { formState, getFieldState, register, setValue } = useFormContext()
const fieldState = getFieldState(name) const fieldState = getFieldState(name)
const ccFieldState = getFieldState(countrySelectorName) const ccFieldState = getFieldState(countrySelectorName)
@@ -49,6 +67,7 @@ export default function Phone({
}) })
const { country, setCountry } = usePhoneInput({ const { country, setCountry } = usePhoneInput({
countries,
defaultCountry: phoneNumberCC defaultCountry: phoneNumberCC
? phoneNumberCC ? phoneNumberCC
: getDefaultCountryFromLang(lang), : getDefaultCountryFromLang(lang),
@@ -73,6 +92,7 @@ export default function Phone({
return ( return (
<div className={`${styles.phone} ${className}`}> <div className={`${styles.phone} ${className}`}>
<CountrySelector <CountrySelector
countries={countries}
disabled={readOnly} disabled={readOnly}
dropdownArrowClassName={styles.arrow} dropdownArrowClassName={styles.arrow}
flagClassName={styles.flag} flagClassName={styles.flag}