feat: replace demo storage instances with secure storage implementations across i18n and storage features
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import i18n from 'i18next';
|
||||
import { demoSecureStorage, StorageKey } from '@repo/core-storage';
|
||||
import { secureStorage, StorageKey } from '@repo/core-storage';
|
||||
|
||||
/**
|
||||
* Changes the active language, saves the preference locally, and optionally syncs with the backend.
|
||||
@@ -9,14 +9,14 @@ import { demoSecureStorage, StorageKey } from '@repo/core-storage';
|
||||
*/
|
||||
export async function changeLanguage(
|
||||
newLng: string,
|
||||
syncCallback?: (newLng: string, prevLng: string) => Promise<void>
|
||||
syncCallback?: (newLng: string, prevLng: string) => Promise<void>,
|
||||
): Promise<void> {
|
||||
const prevLng = i18n.language;
|
||||
|
||||
|
||||
if (prevLng === newLng) return;
|
||||
|
||||
// 1. Update local storage & i18next optimistically
|
||||
await demoSecureStorage.setItem(StorageKey.LOCALE, newLng);
|
||||
await secureStorage.setItem(StorageKey.LOCALE, newLng);
|
||||
await i18n.changeLanguage(newLng);
|
||||
|
||||
// 2. Trigger optional backend sync
|
||||
@@ -26,7 +26,7 @@ export async function changeLanguage(
|
||||
} catch (error) {
|
||||
console.error('[i18n] Backend sync failed, rolling back language', error);
|
||||
// Rollback on failure
|
||||
await demoSecureStorage.setItem(StorageKey.LOCALE, prevLng);
|
||||
await secureStorage.setItem(StorageKey.LOCALE, prevLng);
|
||||
await i18n.changeLanguage(prevLng);
|
||||
throw error; // Rethrow so the caller can show an error toast
|
||||
}
|
||||
@@ -43,13 +43,9 @@ export async function changeLanguage(
|
||||
* @param overrides A deeply nested object containing the overridden string keys and values.
|
||||
* @param lng Specific language to override. Defaults to currently active language.
|
||||
*/
|
||||
export function applyTenantOverrides(
|
||||
namespace: string,
|
||||
overrides: Record<string, unknown>,
|
||||
lng?: string
|
||||
): void {
|
||||
export function applyTenantOverrides(namespace: string, overrides: Record<string, unknown>, lng?: string): void {
|
||||
const targetLng = lng || i18n.language;
|
||||
|
||||
|
||||
// deep: true -> merges with existing keys rather than replacing the whole namespace
|
||||
// overwrite: true -> allows replacing existing specific keys
|
||||
i18n.addResourceBundle(targetLng, namespace, overrides, true, true);
|
||||
|
||||
Reference in New Issue
Block a user