-
-
Notifications
You must be signed in to change notification settings - Fork 253
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic Link, useRouter and usePathname locales #586
Comments
Here's how I construct intl middleware: export const handleI18nRouting = (client?: Client) =>
createIntlMiddleware({
locales:
client?.settings && isFeatureEnabled(client.settings, Feature.Translation)
? getFeatureOption(client.settings, Feature.Translation, 'locales', locales)
: locales,
defaultLocale:
client?.settings && isFeatureEnabled(client.settings, Feature.Translation)
? getFeatureOption(client.settings, Feature.Translation, 'defaultLocale', defaultLocale)
: defaultLocale,
localeDetection: client?.settings
? isFeatureEnabled(client.settings, Feature.Translation)
: false,
}); |
Here's what I've come up so far with type LocalizedNavigation = ReturnType<typeof createLocalizedPathnamesNavigation<any, any>>;
interface PageRouterContextValue {
navigation: LocalizedNavigation;
}
const PageRouterContext = createContext<PageRouterContextValue>({
get navigation(): LocalizedNavigation {
throw new Error('Context used outside provider');
},
});
interface PageRouterProviderProps {
children: React.ReactNode;
}
export function PageRouterProvider({ children }: PageRouterProviderProps) {
const { settings, isFeatureEnabled, getFeatureOption } = useSettings();
const navigation = useMemo(
() =>
createLocalizedPathnamesNavigation({
locales: isFeatureEnabled(Feature.Translation)
? getFeatureOption(Feature.Translation, 'locales', locales)
: locales,
pathnames: {},
}),
[settings]
);
return (
<PageRouterContext.Provider value={{ navigation: navigation as unknown as LocalizedNavigation }}>
{children}
</PageRouterContext.Provider>
);
} |
You can import import configs from '~/config.json';
import { getRequestConfig } from 'next-intl/server';
import { createSharedPathnamesNavigation } from 'next-intl/navigation';
export default getRequestConfig(async ({ locale }) => {
const messagesFormApps = await Promise.all(
configs.applications.map(async app => {
return {
...(await import(`~/langs/${locale}/${app}.json`)).default
};
})
);
return {
messages: {
...messagesFormApps.reduce((acc, messages) => ({ ...acc, ...messages }), {})
},
timeZone: 'UTC'
};
});
export const { Link, redirect, usePathname, useRouter } = createSharedPathnamesNavigation({
locales: configs.languages.locales.filter(locale => locale.enabled).map(locale => locale.key)
}); |
How many locales does your app handle? Any chance you can create the navigation APIs in the outer module scope with all supported locales and restrict them within the app to the sub-set that the user supports? I'll move this to a discussion since this seems to be more of a usage question. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Is your feature request related to a problem? Please describe.
I have recently upgraded to
3.0.0-rc.6
. There is one issue I am having in regards to this upgrade.My app features locales dynamic per user. User can configure the set of languages the app supports.
createLocalizedPathnamesNavigation
requireslocales
to be known at build time, the same for the whole app.Describe the solution you'd like
Ideally, I would use internal
next/navigation
library features instead of decorated onesDescribe alternatives you've considered
I am still trying to figure out how I can create those components/hooks dynamically and it's a pain. Probably I would have to share them using React Context, which is quite painful.
The text was updated successfully, but these errors were encountered: