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

Support for 'default' locale #1679

Closed
nhac-lly opened this issue Feb 9, 2022 · 10 comments
Closed

Support for 'default' locale #1679

nhac-lly opened this issue Feb 9, 2022 · 10 comments

Comments

@nhac-lly
Copy link

nhac-lly commented Feb 9, 2022

https://nextjs.org/docs/advanced-features/i18n-routing#prefixing-the-default-locale
as NextJS team suggest, it is better to use 'default' that fallback to actual main language for some case.
One of those case is to have locale routing always on URL.

For now to use it I need to create a local file for 'default'.
Can we somehow map it to 'en'?

@isaachinman
Copy link
Contributor

Sorry, I don't think I understand your question. Can you explain further?

@nhac-lly
Copy link
Author

module.exports = { i18n: { locales: ['default', 'en', 'de', 'fr'], defaultLocale: 'default', localeDetection: false, }, trailingSlash: true, }
I meant when using this config, you need to also create a public/locale/default/common.json for the 'default' locale, but 'default' is just to fallback to the actual language like 'en'.
In other word is to force 'default' to not needing a locale json for it as it is not used.

@isaachinman
Copy link
Contributor

Ah OK, got it. I'm not convinced that using a non-standard "locale" like default just to manipulate routing is a good idea in the first place. It speaks to the lack of configurability on the NextJs routing side of things.

That being said, I would be fine with merging support for this, as it is indeed in the official NextJs docs, for better or worse. We would need to update createConfig to ignore non-standard locales. PRs welcome.

@fcandi
Copy link

fcandi commented Mar 5, 2022

Thanks, I am in the same boat, using the "default" language as recommended in the official docs, forwarding "/" to "/some_locale" depending on the browsers language.

I now get an ENOENT error loading the non existing default language. Is there a simple preliminary fix for now?

Andreas

@santyas
Copy link

santyas commented Mar 7, 2022

Hey @fcandi @nhac-lly

I've tested it, first you have to create default folder inside /public/locales/ with a common.js file inside (I think that it works if its empy, if it crash just duplicate this common.js file as your common.js prefered locale default).

Then update your getStaticsProps func:

export const getStaticProps: GetStaticProps = async ({locale}) => {
  let theLocale = locale as string
  console.log(`Locale: ${theLocale}`)

  return {
    props: {
      ...(await serverSideTranslations(theLocale == 'default' ? 'ar' : theLocale, ['common'])),
    },
  };
}

I'm using TS so I had to edit it a little bit.

Keep in mind you must have the _middleware file configured as specified in the nextjs i18n docs.

My next.config file:

const {i18n} = require('./next-i18next.config');

const nextConfig = {
	reactStrictMode: true,
	i18n,
	trailingSlash: true,
}

module.exports = nextConfig

And my next-i18next.config file:

module.exports = {
	i18n: {
		locales: ['default', 'ar', 'uy', 'bo', 'py'],
		defaultLocale: 'default',
		localeDetection: false,
	},
};

CC @isaachinman

@adrai
Copy link
Member

adrai commented Jul 7, 2022

Support for Prefixing the Default Locale will be included in the next release.

@adrai adrai closed this as completed Jul 7, 2022
@adrai
Copy link
Member

adrai commented Jul 14, 2022

included in v11.1.1

@deltasierra96
Copy link

included in v11.1.1

Any documentation on this feature or is it as before?

@adrai
Copy link
Member

adrai commented Jul 17, 2022

there is no extra config needed. it just ignores the language "default" when configured in the i18n config: f9a6539#diff-28136a2e354d423cb0c0191cef90624f802ee7921bf0aacbb9bfd3e5f2b1f501R226

@deltasierra96
Copy link

there is no extra config needed. it just ignores the language "default" when configured in the i18n config: f9a6539#diff-28136a2e354d423cb0c0191cef90624f802ee7921bf0aacbb9bfd3e5f2b1f501R226

Awesome. Thanks for the update, Adraino!

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

No branches or pull requests

6 participants