Skip to content

Commit

Permalink
Add support for i18n config in Turbopack (#56182)
Browse files Browse the repository at this point in the history
Add support for `i18n` in Turbopack.
<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

---------

Co-authored-by: Will Binns-Smith <wbinnssmith@gmail.com>
  • Loading branch information
timneutkens and wbinnssmith committed Sep 28, 2023
1 parent 7f6a494 commit 59ce492
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
22 changes: 22 additions & 0 deletions packages/next-swc/crates/next-core/src/env.rs
Expand Up @@ -139,6 +139,28 @@ pub async fn env_for_js(
},
);

map.insert(
"__NEXT_I18N_SUPPORT".to_string(),
// How do I check if i18n exists in the config?
if next_config.i18n.is_none() {
"false".to_string()
} else {
"true".to_string()
},
);

map.insert(
"__NEXT_I18N_DOMAINS".to_string(),
// Don't stringify undefined
match next_config.i18n.as_ref() {
Some(i18n) => match i18n.domains.as_ref() {
Some(domains) => serde_json::to_string(domains)?,
None => "undefined".to_string(),
},
None => "undefined".to_string(),
},
);

if !test_mode.is_empty() {
map.insert("__NEXT_TEST_MODE".to_string(), "true".to_string());
}
Expand Down
Expand Up @@ -107,7 +107,6 @@ fn defines(
process.env.__NEXT_CLIENT_ROUTER_D_FILTER = serde_json::to_value(&filter.dynamic_filter)?,
process.env.__NEXT_DIST_DIR = dist_root_path.to_string(),
process.env.__NEXT_HAS_REWRITES = true,
process.env.__NEXT_I18N_SUPPORT = false,
))
}

Expand Down
22 changes: 11 additions & 11 deletions packages/next-swc/crates/next-core/src/next_config.rs
Expand Up @@ -90,6 +90,7 @@ pub struct NextConfig {
pub base_path: Option<String>,
pub skip_middleware_url_normalize: Option<bool>,
pub skip_trailing_slash_redirect: Option<bool>,
pub i18n: Option<I18NConfig>,

///
#[serde(rename = "_originalRedirects")]
Expand All @@ -115,7 +116,6 @@ pub struct NextConfig {
generate_build_id: Option<serde_json::Value>,
generate_etags: bool,
http_agent_options: HttpAgentConfig,
i18n: Option<I18NConfig>,
on_demand_entries: OnDemandEntriesConfig,
optimize_fonts: bool,
output_file_tracing: bool,
Expand Down Expand Up @@ -176,20 +176,20 @@ struct HttpAgentConfig {

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
#[serde(rename_all = "camelCase")]
struct DomainLocale {
default_locale: String,
domain: String,
http: Option<bool>,
locales: Option<Vec<String>>,
pub struct DomainLocale {
pub default_locale: String,
pub domain: String,
pub http: Option<bool>,
pub locales: Option<Vec<String>>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
#[serde(rename_all = "camelCase")]
struct I18NConfig {
default_locale: String,
domains: Option<Vec<DomainLocale>>,
locale_detection: Option<bool>,
locales: Vec<String>,
pub struct I18NConfig {
pub default_locale: String,
pub domains: Option<Vec<DomainLocale>>,
pub locale_detection: Option<bool>,
pub locales: Vec<String>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
Expand Down
4 changes: 3 additions & 1 deletion packages/next/src/lib/turbopack-warning.ts
Expand Up @@ -21,8 +21,10 @@ const supportedTurbopackNextConfigOptions = [
'swcMinify',
'transpilePackages',
'trailingSlash',
'i18n.locales',
'i18n.defaultLocale',
'i18n.domains',
'i18n.localeDetection',
'i18n.locales',
'sassOptions',
'configOrigin',
'httpAgentOptions',
Expand Down

0 comments on commit 59ce492

Please sign in to comment.