Improve web signing flow and locale handling
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
const COUNTRY_BY_TIMEZONE = {
|
||||
'America/Guayaquil': 'EC',
|
||||
'America/Bogota': 'CO',
|
||||
'America/Lima': 'PE',
|
||||
'America/Caracas': 'VE',
|
||||
'America/Santiago': 'CL',
|
||||
'America/Buenos_Aires': 'AR',
|
||||
'America/Sao_Paulo': 'BR',
|
||||
'America/Mexico_City': 'MX',
|
||||
'America/Panama': 'PA',
|
||||
'America/Costa_Rica': 'CR',
|
||||
'America/El_Salvador': 'SV',
|
||||
'America/Managua': 'NI',
|
||||
'America/Tegucigalpa': 'HN',
|
||||
'America/Guatemala': 'GT',
|
||||
'America/La_Paz': 'BO',
|
||||
'America/Asuncion': 'PY',
|
||||
'America/Montevideo': 'UY',
|
||||
'America/Puerto_Rico': 'PR',
|
||||
'America/New_York': 'US',
|
||||
'America/Chicago': 'US',
|
||||
'America/Denver': 'US',
|
||||
'America/Los_Angeles': 'US',
|
||||
'Europe/Madrid': 'ES',
|
||||
};
|
||||
|
||||
const getClientLocale = () => {
|
||||
if (typeof navigator === 'undefined') return 'es-EC';
|
||||
return navigator.languages?.[0] || navigator.language || 'es-EC';
|
||||
};
|
||||
|
||||
const getRegionFromLocale = (locale) => {
|
||||
try {
|
||||
const normalized = String(locale || '').replace('_', '-');
|
||||
if (typeof Intl.Locale === 'function') {
|
||||
return new Intl.Locale(normalized).region || null;
|
||||
}
|
||||
const parts = normalized.split('-');
|
||||
return parts[1] || null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const getCountryName = (countryCode, locale) => {
|
||||
if (!countryCode) return '';
|
||||
try {
|
||||
const display = new Intl.DisplayNames([locale || 'es-EC'], { type: 'region' });
|
||||
return display.of(countryCode) || countryCode;
|
||||
} catch {
|
||||
return countryCode;
|
||||
}
|
||||
};
|
||||
|
||||
export const detectClientSignatureContext = () => {
|
||||
const locale = getClientLocale();
|
||||
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC';
|
||||
const countryCode = getRegionFromLocale(locale) || COUNTRY_BY_TIMEZONE[timeZone] || null;
|
||||
const countryName = getCountryName(countryCode, locale);
|
||||
|
||||
return {
|
||||
locale,
|
||||
timeZone,
|
||||
countryCode,
|
||||
countryName,
|
||||
defaultLocation: countryName || '',
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user