Skip to content

Commit 4b9d105

Browse files
committed
declare gettext globally
1 parent 63d4fd2 commit 4b9d105

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

client/declaration.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ declare module '*.svg' {
66
const content: string;
77
export default content;
88
}
9+
declare function gettext(s: string): string;

client/django-formset/PhoneNumber.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {StyleHelpers} from './helpers';
44
import {countries} from './countries';
55
import styles from './PhoneNumber.scss';
66

7-
declare function gettext(s: string): string;
7+
const gettext = typeof window.gettext === 'function' ? window.gettext : ((s: string) => s);
8+
89

910
class PhoneNumberField {
1011
private readonly inputElement: HTMLInputElement;
@@ -60,16 +61,9 @@ class PhoneNumberField {
6061
}
6162

6263
private createCountriesMap() : [string, CountryCallingCode, CountryCode][] {
63-
if (typeof gettext === 'function') {
64-
return getCountries().map(
65-
countryCode => [gettext(countries.get(countryCode) ?? ''), getCountryCallingCode(countryCode), countryCode]
66-
);
67-
} else {
68-
// gettext is not available in the global scope, so we use the country names as-is
69-
return getCountries().map(
70-
countryCode => [countries.get(countryCode) ?? '', getCountryCallingCode(countryCode), countryCode]
71-
);
72-
}
64+
return getCountries().map(
65+
countryCode => [gettext(countries.get(countryCode) ?? ''), getCountryCallingCode(countryCode), countryCode]
66+
);
7367
}
7468

7569
private createTextBox() {

0 commit comments

Comments
 (0)