|
8 | 8 | // In production, this would be imported from the npm package |
9 | 9 | // import { localizer, _t } from '@profullstack/localizer'; |
10 | 10 | // For development, we'll use our deps.js file |
11 | | -import { localizer, _t } from './deps.js'; |
12 | | -import defaultStateManager from './state-manager.js'; |
| 11 | +import { localizer, _t, createStore } from './deps.js'; |
| 12 | + |
| 13 | +// Create a store for i18n state |
| 14 | +const i18nStore = createStore('i18n', { |
| 15 | + currentLanguage: 'en', |
| 16 | + availableLanguages: ['en', 'fr', 'de', 'uk', 'ru', 'pl', 'zh', 'ja', 'ar'], |
| 17 | + isRTL: false |
| 18 | +}); |
13 | 19 |
|
14 | 20 | // Store available languages |
15 | 21 | const AVAILABLE_LANGUAGES = ['en', 'fr', 'de', 'uk', 'ru', 'pl', 'zh', 'ja', 'ar']; |
@@ -94,25 +100,18 @@ async function initI18n() { |
94 | 100 | * Initialize state management for i18n |
95 | 101 | */ |
96 | 102 | function initStateManagement() { |
97 | | - // Set up initial i18n state in the state manager |
98 | | - defaultStateManager.setState({ |
99 | | - i18n: { |
100 | | - currentLanguage: localizer.getLanguage(), |
101 | | - availableLanguages: AVAILABLE_LANGUAGES, |
102 | | - isRTL: ['ar', 'he', 'fa', 'ur'].includes(localizer.getLanguage()) |
103 | | - } |
104 | | - }); |
| 103 | + // Update the store with current language |
| 104 | + i18nStore.state.currentLanguage = localizer.getLanguage(); |
| 105 | + i18nStore.state.isRTL = ['ar', 'he', 'fa', 'ur'].includes(localizer.getLanguage()); |
105 | 106 |
|
106 | | - // Subscribe to language changes in the state manager |
107 | | - defaultStateManager.subscribe((state, changedKeys) => { |
108 | | - if (changedKeys.includes('i18n.currentLanguage')) { |
109 | | - const newLang = state.i18n.currentLanguage; |
110 | | - if (newLang !== localizer.getLanguage()) { |
111 | | - console.log(`Language change detected in state manager: ${newLang}`); |
112 | | - changeLanguage(newLang); |
113 | | - } |
| 107 | + // Subscribe to language changes in the store |
| 108 | + i18nStore.subscribe((state) => { |
| 109 | + const newLang = state.currentLanguage; |
| 110 | + if (newLang !== localizer.getLanguage()) { |
| 111 | + console.log(`Language change detected in store: ${newLang}`); |
| 112 | + changeLanguage(newLang); |
114 | 113 | } |
115 | | - }, 'i18n.currentLanguage'); |
| 114 | + }); |
116 | 115 | } |
117 | 116 |
|
118 | 117 | /** |
@@ -180,14 +179,12 @@ function changeLanguage(language) { |
180 | 179 | // Apply RTL direction if needed |
181 | 180 | applyDirectionToDocument(); |
182 | 181 |
|
183 | | - // Update the state manager (without triggering the subscription callback) |
| 182 | + // Update the store (without triggering circular updates) |
184 | 183 | const isRTL = ['ar', 'he', 'fa', 'ur'].includes(language); |
185 | | - defaultStateManager.setState({ |
186 | | - i18n: { |
187 | | - currentLanguage: language, |
188 | | - isRTL |
189 | | - } |
190 | | - }, true); // Silent update to prevent circular updates |
| 184 | + |
| 185 | + // Use direct property assignment to avoid triggering the subscription |
| 186 | + i18nStore.state.currentLanguage = language; |
| 187 | + i18nStore.state.isRTL = isRTL; |
191 | 188 |
|
192 | 189 | // Dispatch an event to notify that language has changed |
193 | 190 | window.dispatchEvent(new CustomEvent('language-changed', { |
|
0 commit comments