File size: 782 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import { setGeoLocation } from '@automattic/number-formatters';
import cookie from 'cookie';
async function fetchCountryCode(): Promise< string > {
try {
const response = await fetch( 'https://public-api.wordpress.com/geo/' );
const data = await response.json();
return data.country_short ?? '';
} catch {
return '';
}
}
/**
* Initializes country code for localization and formatting libraries from the current user. If the
* user is not logged in, it will fetch the country code from the geolocation API.
*/
export async function setupCountryCode() {
const cookies = cookie.parse( document.cookie );
const countryCode = cookies.country_code ?? ( await fetchCountryCode() );
if ( countryCode && countryCode !== 'unknown' ) {
setGeoLocation( countryCode );
}
}
|