Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/02-pages/02-guides/internationalization.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,40 @@ When [pre-rendering](/docs/pages/building-your-application/rendering/static-site

When leveraging `getStaticPaths`, the configured locales are provided in the context parameter of the function under `locales` and the configured defaultLocale under `defaultLocale`.

### Client-side via `useRouter()`

You can access locale data in the browser using the [`useRouter()`](https://nextjs.org/docs/pages/api-reference/functions/use-router) hook:

```jsx
import { useRouter } from 'next/router';

export default function LocaleExample() {
const { locale, locales, defaultLocale } = useRouter();

return (
Current locale: {locale}
All locales: {locales?.join(', ')}
Default locale: {defaultLocale}
Comment on lines +227 to +229
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Current locale: {locale}
All locales: {locales?.join(', ')}
Default locale: {defaultLocale}
<>
<div>Current locale: {locale}</div>
<div>All locales: {locales?.join(', ')}</div>
<div>Default locale: {defaultLocale}</div>
</>

The JSX code example has invalid syntax - the return statement contains text content without wrapping it in a JSX element.

View Details

Analysis

Invalid JSX syntax in internationalization documentation example

What fails: The LocaleExample component in docs/02-pages/02-guides/internationalization.mdx (lines 226-230) has invalid JSX syntax - returns multiple text nodes without a wrapper element

How to reproduce:

cd /tmp && npm install @babel/core @babel/preset-react
node << 'EOF'
const babel = require('@babel/core');
const code = `
export default function LocaleExample() {
  const { locale, locales, defaultLocale } = useRouter();
  return (
      Current locale: {locale}
      All locales: {locales?.join(', ')}
      Default locale: {defaultLocale}
  );
}`;
babel.transformSync(code, { presets: ['@babel/preset-react'] });
EOF

Result: Babel compilation fails with error: Unexpected token, expected "," (7:14) at the "Current locale:" text line

Expected: JSX requires multiple elements/text nodes to be wrapped in a parent element or Fragment per React documentation on Fragments. All other code examples in this file properly wrap content in JSX elements.

);
}
```

### Server-side via `getStaticProps` or `getServerSideProps`

When using [`getStaticProps`](https://nextjs.org/docs/pages/building-your-application/data-fetching/get-static-props) or [`getServerSideProps`](https://nextjs.org/docs/pages/building-your-application/data-fetching/get-server-side-props), locale values are available through the `context` parameter:

```jsx
export async function getStaticProps({ locale, locales, defaultLocale }) {
return {
props: {
locale,
locales,
defaultLocale,
},
};
}
```

## Transition between locales

You can use `next/link` or `next/router` to transition between locales.
Expand Down
Loading