Skip to content

Commit

Permalink
declare gettext globally
Browse files Browse the repository at this point in the history
  • Loading branch information
jrief committed Nov 14, 2023
1 parent 63d4fd2 commit 4b9d105
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
1 change: 1 addition & 0 deletions client/declaration.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ declare module '*.svg' {
const content: string;
export default content;
}
declare function gettext(s: string): string;
16 changes: 5 additions & 11 deletions client/django-formset/PhoneNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 4b9d105

Please sign in to comment.