diff --git a/client/declaration.d.ts b/client/declaration.d.ts index 9629ad61..b95b16f8 100644 --- a/client/declaration.d.ts +++ b/client/declaration.d.ts @@ -6,3 +6,4 @@ declare module '*.svg' { const content: string; export default content; } +declare function gettext(s: string): string; diff --git a/client/django-formset/PhoneNumber.ts b/client/django-formset/PhoneNumber.ts index eadf95d4..8dbefe13 100644 --- a/client/django-formset/PhoneNumber.ts +++ b/client/django-formset/PhoneNumber.ts @@ -4,7 +4,8 @@ import {StyleHelpers} from './helpers'; import {countries} from './countries'; import styles from './PhoneNumber.scss'; -declare function gettext(s: string): string; +const gettext = typeof window.gettext === 'function' ? window.gettext : ((s: string) => s); + class PhoneNumberField { private readonly inputElement: HTMLInputElement; @@ -60,16 +61,9 @@ class PhoneNumberField { } private createCountriesMap() : [string, CountryCallingCode, CountryCode][] { - if (typeof gettext === 'function') { - return getCountries().map( - countryCode => [gettext(countries.get(countryCode) ?? ''), getCountryCallingCode(countryCode), countryCode] - ); - } else { - // gettext is not available in the global scope, so we use the country names as-is - return getCountries().map( - countryCode => [countries.get(countryCode) ?? '', getCountryCallingCode(countryCode), countryCode] - ); - } + return getCountries().map( + countryCode => [gettext(countries.get(countryCode) ?? ''), getCountryCallingCode(countryCode), countryCode] + ); } private createTextBox() {