fix(language): auto-sync from user_metadata.locale at sign-in
Bug: User mit iOS-Sprache=Arabisch sah App auf Englisch wenn Localization.getLocales() auf seinem Setup nicht zuverlässig 'ar' zurückgab (iOS-Region≠Sprache, App-Override etc). Fix: bei sign-in (init() initial-getSession + onAuthStateChange für SIGNED_IN events) wird session.user.user_metadata.locale gelesen. Wenn AsyncStorage @rebreak/language NOCH NICHT gesetzt ist (User hat keine explicit Choice gemacht) → silent apply der server-locale (inkl. RTL-flip, KEIN Restart-Alert). Respektiert User-Choice: wenn AsyncStorage gefüllt ist (z.B. User hat manuell in Settings gewechselt), bleibt das gewinnen.
This commit is contained in:
parent
63a1a3b550
commit
73f70b5e28
@ -7,6 +7,7 @@ import * as AppleAuthentication from 'expo-apple-authentication';
|
|||||||
import { supabase } from '../lib/supabase';
|
import { supabase } from '../lib/supabase';
|
||||||
import { apiFetch } from '../lib/api';
|
import { apiFetch } from '../lib/api';
|
||||||
import i18n from '../lib/i18n';
|
import i18n from '../lib/i18n';
|
||||||
|
import { syncLanguageFromUserMetadata } from './language';
|
||||||
|
|
||||||
const SUPPORTED_LOCALES = ['de', 'en', 'fr', 'ar'] as const;
|
const SUPPORTED_LOCALES = ['de', 'en', 'fr', 'ar'] as const;
|
||||||
type SupportedLocale = (typeof SUPPORTED_LOCALES)[number];
|
type SupportedLocale = (typeof SUPPORTED_LOCALES)[number];
|
||||||
@ -69,9 +70,15 @@ export const useAuthStore = create<AuthState>((set) => ({
|
|||||||
user: data.session?.user ?? null,
|
user: data.session?.user ?? null,
|
||||||
loading: false,
|
loading: false,
|
||||||
});
|
});
|
||||||
|
if (data.session?.user) {
|
||||||
|
void syncLanguageFromUserMetadata(data.session.user);
|
||||||
|
}
|
||||||
|
|
||||||
supabase.auth.onAuthStateChange((_event, session) => {
|
supabase.auth.onAuthStateChange((_event, session) => {
|
||||||
set({ session, user: session?.user ?? null });
|
set({ session, user: session?.user ?? null });
|
||||||
|
if (session?.user) {
|
||||||
|
void syncLanguageFromUserMetadata(session.user);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@ -61,3 +61,32 @@ export const useLanguageStore = create<LanguageState>((set) => ({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
// Auto-sync language from session.user.user_metadata.locale beim Sign-In.
|
||||||
|
// Wenn AsyncStorage @rebreak/language LEER ist (kein explicit user-choice)
|
||||||
|
// und Server-side metadata.locale gesetzt — silent apply (kein Restart-Alert).
|
||||||
|
// Respektiert User-Choice: AsyncStorage gewinnt immer wenn gesetzt.
|
||||||
|
export async function syncLanguageFromUserMetadata(
|
||||||
|
user: { user_metadata?: { locale?: string | null } | null } | null,
|
||||||
|
): Promise<void> {
|
||||||
|
if (!user) return;
|
||||||
|
const raw = user.user_metadata?.locale;
|
||||||
|
if (!raw) return;
|
||||||
|
const short = String(raw).split('-')[0].toLowerCase();
|
||||||
|
const valid: AppLanguage[] = ['de', 'en', 'fr', 'ar'];
|
||||||
|
if (!(valid as readonly string[]).includes(short)) return;
|
||||||
|
const lang = short as AppLanguage;
|
||||||
|
|
||||||
|
const stored = await AsyncStorage.getItem(STORAGE_KEY);
|
||||||
|
if (stored) return; // explicit user-choice wins
|
||||||
|
|
||||||
|
if (i18n.language === lang) {
|
||||||
|
await AsyncStorage.setItem(STORAGE_KEY, lang);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await AsyncStorage.setItem(STORAGE_KEY, lang);
|
||||||
|
await i18n.changeLanguage(lang);
|
||||||
|
applyRTL(lang);
|
||||||
|
useLanguageStore.setState({ language: lang });
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user