diff --git a/app/components/phone_input_component.html.erb b/app/components/phone_input_component.html.erb index 3de7e14efd3..970c7ba2d7a 100644 --- a/app/components/phone_input_component.html.erb +++ b/app/components/phone_input_component.html.erb @@ -1,7 +1,10 @@ <%= content_tag( 'lg-phone-input', class: tag_options[:class], - data: { delivery_methods: delivery_methods }, + data: { + delivery_methods: delivery_methods, + translated_country_code_names: translated_country_code_names, + }, ) do %> <%= content_tag( :script, diff --git a/app/components/phone_input_component.rb b/app/components/phone_input_component.rb index 9c78b99ae9a..31e5ad76cdf 100644 --- a/app/components/phone_input_component.rb +++ b/app/components/phone_input_component.rb @@ -21,15 +21,25 @@ def initialize( end def supported_country_codes - codes = PhoneNumberCapabilities::INTERNATIONAL_CODES.keys - codes &= allowed_countries if allowed_countries - codes + @supported_country_codes ||= begin + codes = PhoneNumberCapabilities::INTERNATIONAL_CODES.keys + codes &= allowed_countries if allowed_countries + codes + end + end + + def translated_country_code_names + supported_country_codes.map do |code| + code = code.downcase + [code, I18n.t("countries.#{code}")] + end.to_h end def international_phone_codes supported_country_codes. map do |code_key| - code_data = PhoneNumberCapabilities::INTERNATIONAL_CODES[code_key] + code_data = PhoneNumberCapabilities.translated_international_codes[code_key] + [ international_phone_code_label(code_data), code_key, diff --git a/app/javascript/packages/phone-input/index.js b/app/javascript/packages/phone-input/index.js index 6a356154580..e66726c0293 100644 --- a/app/javascript/packages/phone-input/index.js +++ b/app/javascript/packages/phone-input/index.js @@ -42,6 +42,9 @@ export class PhoneInput extends HTMLElement { /** @type {string[]} */ deliveryMethods = []; + /** @type {Object.} */ + countryCodePairs = {}; + connectedCallback() { /** @type {HTMLInputElement?} */ this.textInput = this.querySelector('.phone-input__number'); @@ -49,8 +52,10 @@ export class PhoneInput extends HTMLElement { this.codeInput = this.querySelector('.phone-input__international-code'); this.codeWrapper = this.querySelector('.phone-input__international-code-wrapper'); this.exampleText = this.querySelector('.phone-input__example'); + try { this.deliveryMethods = JSON.parse(this.dataset.deliveryMethods || ''); + this.countryCodePairs = JSON.parse(this.dataset.translatedCountryCodeNames || ''); } catch {} if (!this.textInput || !this.codeInput) { @@ -118,11 +123,12 @@ export class PhoneInput extends HTMLElement { } initializeIntlTelInput() { - const { supportedCountryCodes } = this; + const { supportedCountryCodes, countryCodePairs } = this; const allowDropdown = supportedCountryCodes && supportedCountryCodes.length > 1; const iti = intlTelInput(this.textInput, { preferredCountries: ['US', 'CA'], + localizedCountries: countryCodePairs, onlyCountries: supportedCountryCodes, autoPlaceholder: 'off', allowDropdown, diff --git a/app/javascript/packages/phone-input/index.spec.js b/app/javascript/packages/phone-input/index.spec.js index 8c4995c6a2b..86891e53940 100644 --- a/app/javascript/packages/phone-input/index.spec.js +++ b/app/javascript/packages/phone-input/index.spec.js @@ -30,9 +30,14 @@ describe('PhoneInput', () => { isSingleOption = false, isNonUSSingleOption = false, deliveryMethods = ['sms', 'voice'], + translatedCountryCodeNames = {}, } = {}) { const element = document.createElement('lg-phone-input'); element.setAttribute('data-delivery-methods', JSON.stringify(deliveryMethods)); + element.setAttribute( + 'data-translated-country-code-names', + JSON.stringify(translatedCountryCodeNames), + ); element.innerHTML = `