48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
/**
|
|
* Utility function to safely access translations with fallbacks
|
|
*/
|
|
export function safeTranslation(t: any, key: string, fallback: string = '') {
|
|
try {
|
|
if (!t || typeof t !== 'function') {
|
|
console.warn(`Translation function not available for key "${key}"`);
|
|
return fallback;
|
|
}
|
|
|
|
const translation = t(key);
|
|
// If the translation returns the key itself or contains "MISSING", use fallback
|
|
if (!translation || translation === key || translation.includes('MISSING')) {
|
|
return fallback;
|
|
}
|
|
return translation;
|
|
} catch (error) {
|
|
console.warn(`Translation error for key "${key}":`, error);
|
|
return fallback;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Utility function to check if the server is responding
|
|
* Can be used to detect server issues early
|
|
*/
|
|
export async function checkServerConnection() {
|
|
try {
|
|
const response = await fetch('/api/health', {
|
|
method: 'GET',
|
|
headers: {
|
|
'Cache-Control': 'no-cache',
|
|
},
|
|
});
|
|
|
|
return {
|
|
isConnected: response.ok,
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
};
|
|
} catch (error) {
|
|
console.error('Server connection check failed:', error);
|
|
return {
|
|
isConnected: false,
|
|
error: error instanceof Error ? error.message : String(error),
|
|
};
|
|
}
|
|
} |