Skip to content

Commit

Permalink
fix(next-international): nested objects locales with fallbackLocale (#73
Browse files Browse the repository at this point in the history
)
  • Loading branch information
QuiiBz authored Jul 20, 2023
1 parent 3c494b1 commit b71ad6c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Context, ReactElement, ReactNode, useCallback, useEffect, useState } from 'react';
import React, { Context, ReactElement, ReactNode, useCallback, useEffect, useMemo, useState } from 'react';
import type { LocaleContext } from '../../types';
import type { BaseLocale, ImportedLocales } from 'international-types';
import { flattenLocale } from '../../common/flatten-locale';
Expand Down Expand Up @@ -32,19 +32,18 @@ export function createI18nProviderClient<Locale extends BaseLocale>(
loadLocale(baseLocale);
}, [baseLocale, loadLocale]);

const value = useMemo(
() => ({
localeContent: (clientLocale || baseLocale) as Locale,
fallbackLocale: fallbackLocale ? flattenLocale(fallbackLocale) : undefined,
}),
[clientLocale, baseLocale, fallbackLocale],
);

if (!clientLocale && fallback) {
return fallback;
}

return (
<I18nClientContext.Provider
value={{
localeContent: (clientLocale || baseLocale) as Locale,
fallbackLocale,
}}
>
{children}
</I18nClientContext.Provider>
);
return <I18nClientContext.Provider value={value}>{children}</I18nClientContext.Provider>;
};
}
21 changes: 10 additions & 11 deletions packages/next-international/src/pages/create-i18n-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Context, ReactElement, ReactNode, useCallback, useEffect, useRef, useState } from 'react';
import React, { Context, ReactElement, ReactNode, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import type { LocaleContext } from '../types';
import type { BaseLocale, ImportedLocales } from 'international-types';
import { useRouter } from 'next/router';
Expand Down Expand Up @@ -65,6 +65,14 @@ export function createI18nProvider<Locale extends BaseLocale>(
initialLoadRef.current = false;
}, [baseLocale, loadLocale, locale]);

const value = useMemo(
() => ({
localeContent: (clientLocale || baseLocale) as Locale,
fallbackLocale: fallbackLocale ? flattenLocale(fallbackLocale) : undefined,
}),
[clientLocale, baseLocale, fallbackLocale],
);

if (!locale || !defaultLocale) {
return error(`'i18n.defaultLocale' not defined in 'next.config.js'`);
}
Expand All @@ -77,15 +85,6 @@ export function createI18nProvider<Locale extends BaseLocale>(
return fallback;
}

return (
<I18nContext.Provider
value={{
localeContent: (clientLocale || baseLocale) as Locale,
fallbackLocale,
}}
>
{children}
</I18nContext.Provider>
);
return <I18nContext.Provider value={value}>{children}</I18nContext.Provider>;
};
}

0 comments on commit b71ad6c

Please sign in to comment.