Skip to content
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

[Docs]: App Router setup with i18n routing #1670

Closed
mhafez1978 opened this issue Jan 21, 2025 · 5 comments · May be fixed by #1412
Closed

[Docs]: App Router setup with i18n routing #1670

mhafez1978 opened this issue Jan 21, 2025 · 5 comments · May be fixed by #1412

Comments

@mhafez1978
Copy link

locale defined as any here is problematic and goes against typescript safety - we should not be using type any here with locale. it should be as const.

import {getRequestConfig} from 'next-intl/server';
import {routing} from './routing';
 
export default getRequestConfig(async ({requestLocale}) => {
  // This typically corresponds to the `[locale]` segment
  let locale = await requestLocale;
 
  // Ensure that a valid locale is used
  if (!locale || !routing.locales.includes(locale as any)) {
    locale = routing.defaultLocale;
  }
 
  return {
    locale,
    messages: (await import(`../../messages/${locale}.json`)).default
  };
});
@mhafez1978
Copy link
Author

any feedback on how this should be set would be appreciated !

@amannn
Copy link
Owner

amannn commented Jan 21, 2025

With hasLocale that is currently proposed in next-intl@4 beta, you can avoid the any.

@amannn amannn mentioned this issue Jan 21, 2025
8 tasks
@mhafez1978
Copy link
Author

mhafez1978 commented Jan 21, 2025 via email

@daveycodez
Copy link

Here is how I define Locale type, you can just use it wherever

routing.ts

import { defineRouting } from "next-intl/routing"
import { createNavigation } from "next-intl/navigation"
import { isExport } from "@/lib/utils"

export const routing = defineRouting({
    // A list of all locales that are supported
    locales: ["en", "de"],

    // Used when no locale matches
    defaultLocale: "en",

    localePrefix: isExport ? "always" : "as-needed",

    localeCookie: false
})

// Lightweight wrappers around Next.js' navigation APIs
// that will consider the routing configuration
export const { Link, redirect, usePathname, useRouter, getPathname } =
    createNavigation(routing)

export type Locale = (typeof routing.locales)[number]

@mhafez1978
Copy link
Author

Thank you so so much both for your feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants