@@ -10,7 +10,7 @@ export interface ModernI18nContextValue {
1010 // Plugin configuration for useModernI18n hook
1111 entryName ?: string ;
1212 languages ?: string [ ] ;
13- enableLocaleDetection ?: boolean ;
13+ localePathRedirect ?: boolean ;
1414 // Callback to update language in context
1515 updateLanguage ?: ( newLang : string ) => void ;
1616}
@@ -33,12 +33,6 @@ export const ModernI18nProvider: FC<ModernI18nProviderProps> = ({
3333 ) ;
3434} ;
3535
36- export interface UseModernI18nOptions {
37- entryName ?: string ;
38- languages ?: string [ ] ;
39- enableLocaleDetection ?: boolean ;
40- }
41-
4236export interface UseModernI18nReturn {
4337 language : string ;
4438 changeLanguage : ( newLang : string ) => Promise < void > ;
@@ -86,9 +80,7 @@ const useRouterHooks = () => {
8680 * @param options - Optional configuration to override context settings
8781 * @returns Object containing i18n functionality and utilities
8882 */
89- export const useModernI18n = (
90- options : UseModernI18nOptions = { } ,
91- ) : UseModernI18nReturn => {
83+ export const useModernI18n = ( ) : UseModernI18nReturn => {
9284 const context = useContext ( ModernI18nContext ) ;
9385 if ( ! context ) {
9486 throw new Error ( 'useModernI18n must be used within a ModernI18nProvider' ) ;
@@ -97,19 +89,12 @@ export const useModernI18n = (
9789 const {
9890 language : contextLanguage ,
9991 i18nInstance,
100- entryName : contextEntryName ,
101- languages : contextLanguages ,
102- enableLocaleDetection : contextEnableLocaleDetection ,
92+ entryName,
93+ languages,
94+ localePathRedirect ,
10395 updateLanguage,
10496 } = context ;
10597
106- // Merge context options with passed options (passed options take precedence)
107- const {
108- entryName = contextEntryName ,
109- languages = contextLanguages || [ ] ,
110- enableLocaleDetection = contextEnableLocaleDetection || false ,
111- } = options ;
112-
11398 // Get router hooks safely
11499 const { navigate, location, hasRouter } = useRouterHooks ( ) ;
115100
@@ -140,7 +125,7 @@ export const useModernI18n = (
140125
141126 // Update URL if locale detection is enabled, we're in browser, and router is available
142127 if (
143- enableLocaleDetection &&
128+ localePathRedirect &&
144129 isBrowser ( ) &&
145130 hasRouter &&
146131 navigate &&
@@ -151,19 +136,27 @@ export const useModernI18n = (
151136 const relativePath = currentPath . replace ( entryPath , '' ) ;
152137
153138 // Build new path with updated language
154- const newPath = buildLocalizedUrl ( relativePath , newLang , languages ) ;
139+ const newPath = buildLocalizedUrl (
140+ relativePath ,
141+ newLang ,
142+ languages || [ ] ,
143+ ) ;
155144 const newUrl = entryPath + newPath + location . search + location . hash ;
156145
157146 // Navigate to new URL
158147 navigate ( newUrl , { replace : true } ) ;
159- } else if ( enableLocaleDetection && isBrowser ( ) && ! hasRouter ) {
148+ } else if ( localePathRedirect && isBrowser ( ) && ! hasRouter ) {
160149 // Fallback: use window.history API when router is not available
161150 const currentPath = window . location . pathname ;
162151 const entryPath = getEntryPath ( entryName ) ;
163152 const relativePath = currentPath . replace ( entryPath , '' ) ;
164153
165154 // Build new path with updated language
166- const newPath = buildLocalizedUrl ( relativePath , newLang , languages ) ;
155+ const newPath = buildLocalizedUrl (
156+ relativePath ,
157+ newLang ,
158+ languages || [ ] ,
159+ ) ;
167160 const newUrl =
168161 entryPath + newPath + window . location . search + window . location . hash ;
169162
@@ -183,7 +176,7 @@ export const useModernI18n = (
183176 [
184177 i18nInstance ,
185178 updateLanguage ,
186- enableLocaleDetection ,
179+ localePathRedirect ,
187180 entryName ,
188181 languages ,
189182 hasRouter ,
@@ -195,7 +188,7 @@ export const useModernI18n = (
195188 // Helper function to check if a language is supported
196189 const isLanguageSupported = useCallback (
197190 ( lang : string ) => {
198- return languages . includes ( lang ) ;
191+ return languages ? .includes ( lang ) || false ;
199192 } ,
200193 [ languages ] ,
201194 ) ;
@@ -204,7 +197,7 @@ export const useModernI18n = (
204197 language : currentLanguage ,
205198 changeLanguage,
206199 i18nInstance,
207- supportedLanguages : languages ,
200+ supportedLanguages : languages || [ ] ,
208201 isLanguageSupported,
209202 } ;
210203} ;
0 commit comments