refactor: decouple core-storage services from static keys and update i18n setup to use configurable storage adapters
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import i18n from 'i18next';
|
||||
import { secureStorage, StorageKey } from '@repo/core-storage';
|
||||
import { globalStorageAdapter } from './setup';
|
||||
|
||||
/**
|
||||
* Changes the active language, saves the preference locally, and optionally syncs with the backend.
|
||||
@@ -16,7 +16,9 @@ export async function changeLanguage(
|
||||
if (prevLng === newLng) return;
|
||||
|
||||
// 1. Update local storage & i18next optimistically
|
||||
await secureStorage.setItem(StorageKey.LOCALE, newLng);
|
||||
if (globalStorageAdapter) {
|
||||
await globalStorageAdapter.setLanguage(newLng);
|
||||
}
|
||||
await i18n.changeLanguage(newLng);
|
||||
|
||||
// 2. Trigger optional backend sync
|
||||
@@ -26,7 +28,9 @@ export async function changeLanguage(
|
||||
} catch (error) {
|
||||
console.error('[i18n] Backend sync failed, rolling back language', error);
|
||||
// Rollback on failure
|
||||
await secureStorage.setItem(StorageKey.LOCALE, prevLng);
|
||||
if (globalStorageAdapter) {
|
||||
await globalStorageAdapter.setLanguage(prevLng);
|
||||
}
|
||||
await i18n.changeLanguage(prevLng);
|
||||
throw error; // Rethrow so the caller can show an error toast
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user