refactor: unify category data fetching across components to support language context and improve state management

This commit is contained in:
sebseb7
2025-07-31 08:26:10 +02:00
parent 9b38ed6f2a
commit 42fa46f2f9
5 changed files with 103 additions and 47 deletions

View File

@@ -145,7 +145,6 @@ i18n
.use(initReactI18next)
.init({
resources,
lng: 'de', // Force German as default
fallbackLng: 'de',
debug: process.env.NODE_ENV === 'development',

View File

@@ -19,8 +19,30 @@ export class LanguageProvider extends Component {
constructor(props) {
super(props);
// Always start with German
const currentLanguage = 'de';
// Try to get the current language from i18n or storage, fallback to German
let currentLanguage = 'de';
console.log("LanguageProvider constructor - Debug info:");
console.log(" props.i18n:", props.i18n);
console.log(" props.i18n.language:", props.i18n?.language);
if (props.i18n && props.i18n.language) {
currentLanguage = props.i18n.language;
console.log(" Using language from i18n:", currentLanguage);
} else if (typeof window !== 'undefined') {
// Try to get from sessionStorage first, then localStorage
try {
const sessionLang = sessionStorage.getItem('i18nextLng');
const localLang = localStorage.getItem('i18nextLng');
console.log(" sessionStorage i18nextLng:", sessionLang);
console.log(" localStorage i18nextLng:", localLang);
currentLanguage = sessionLang || localLang || 'de';
console.log(" Using language from storage:", currentLanguage);
} catch (error) {
console.warn('Could not read language from storage:', error);
}
}
console.log(" Final currentLanguage:", currentLanguage);
this.state = {
currentLanguage,
@@ -37,9 +59,22 @@ export class LanguageProvider extends Component {
componentDidMount() {
if (this.props.i18n) {
this.props.i18n.on('languageChanged', this.handleLanguageChanged);
// Set initial language state and HTML attribute
this.setState({ currentLanguage: this.props.i18n.language });
document.documentElement.lang = this.props.i18n.language;
console.log("LanguageProvider componentDidMount:");
console.log(" current state.currentLanguage:", this.state.currentLanguage);
console.log(" props.i18n.language:", this.props.i18n.language);
// Only update state if i18n language is different and not the default 'de'
// This prevents overriding the language we restored from storage
if (this.props.i18n.language !== this.state.currentLanguage &&
this.props.i18n.language !== 'de') {
console.log(" Updating language from i18n:", this.props.i18n.language);
this.setState({ currentLanguage: this.props.i18n.language });
document.documentElement.lang = this.props.i18n.language;
} else {
console.log(" Keeping language from constructor:", this.state.currentLanguage);
document.documentElement.lang = this.state.currentLanguage;
}
// Start demo mode
if (this.state.demoMode) {