Skip to content

Commit 17781fd

Browse files
authored
feat(core): add script env variables: NODE_ENV + BABEL_ENV + DOCUSAURUS_CURRENT_LOCALE (temporary i18n workaround) (#8677)
1 parent fd021bd commit 17781fd

File tree

6 files changed

+56
-13
lines changed

6 files changed

+56
-13
lines changed

.cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"__snapshots__",
2929
"website/src/data/users.tsx",
3030
"website/src/data/tweets.tsx",
31+
"website/docusaurus.config.localized.json",
3132
"*.xyz",
3233
"*.docx",
3334
"versioned_docs",

packages/docusaurus/bin/docusaurus.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ import {
2424
} from '../lib/index.js';
2525
import beforeCli from './beforeCli.mjs';
2626

27+
// Env variables are initialized to dev, but can be overridden by each command
28+
// For example, "docusaurus build" overrides them to "production"
29+
// See also https://github.com/facebook/docusaurus/issues/8599
30+
process.env.BABEL_ENV ??= 'development';
31+
process.env.NODE_ENV ??= 'development';
32+
2733
await beforeCli();
2834

2935
cli.version(DOCUSAURUS_VERSION).usage('<command> [options]');

packages/docusaurus/src/commands/build.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export async function build(
4646
// See https://github.com/facebook/docusaurus/pull/2496
4747
forceTerminate: boolean = true,
4848
): Promise<string> {
49+
process.env.BABEL_ENV = 'production';
50+
process.env.NODE_ENV = 'production';
51+
process.env.DOCUSAURUS_CURRENT_LOCALE = cliOptions.locale;
52+
4953
const siteDir = await fs.realpath(siteDirParam);
5054

5155
['SIGINT', 'SIGTERM'].forEach((sig) => {
@@ -117,8 +121,11 @@ async function buildLocale({
117121
forceTerminate: boolean;
118122
isLastLocale: boolean;
119123
}): Promise<string> {
120-
process.env.BABEL_ENV = 'production';
121-
process.env.NODE_ENV = 'production';
124+
// Temporary workaround to unlock the ability to translate the site config
125+
// We'll remove it if a better official API can be designed
126+
// See https://github.com/facebook/docusaurus/issues/4542
127+
process.env.DOCUSAURUS_CURRENT_LOCALE = locale;
128+
122129
logger.info`name=${`[${locale}]`} Creating an optimized production build...`;
123130

124131
const props: Props = await load({

packages/docusaurus/src/commands/start.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ export async function start(
3939
siteDirParam: string = '.',
4040
cliOptions: Partial<StartCLIOptions> = {},
4141
): Promise<void> {
42+
// Temporary workaround to unlock the ability to translate the site config
43+
// We'll remove it if a better official API can be designed
44+
// See https://github.com/facebook/docusaurus/issues/4542
45+
process.env.DOCUSAURUS_CURRENT_LOCALE = cliOptions.locale;
46+
4247
const siteDir = await fs.realpath(siteDirParam);
4348

44-
process.env.NODE_ENV = 'development';
45-
process.env.BABEL_ENV = 'development';
4649
logger.info('Starting the development server...');
4750

4851
function loadSite() {

website/docusaurus.config.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const {
1616
dogfoodingThemeInstances,
1717
} = require('./_dogfooding/dogfooding.config');
1818

19+
/** @type {Record<string,Record<string,string>>} */
20+
const ConfigLocalized = require('./docusaurus.config.localized.json');
21+
1922
const ArchivedVersionsDropdownItems = Object.entries(VersionsArchived).splice(
2023
0,
2124
5,
@@ -63,10 +66,27 @@ const isVersioningDisabled = !!process.env.DISABLE_VERSIONING || isI18nStaging;
6366
const TwitterSvg =
6467
'<svg style="fill: #1DA1F2; vertical-align: middle; margin-left: 3px;" width="16" height="16" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"></path></svg>';
6568

69+
const defaultLocale = 'en';
70+
71+
function getLocalizedConfigValue(/** @type {string} */ key) {
72+
const currentLocale = process.env.DOCUSAURUS_CURRENT_LOCALE ?? defaultLocale;
73+
const values = ConfigLocalized[key];
74+
if (!values) {
75+
throw new Error(`Localized config key=${key} not found`);
76+
}
77+
const value = values[currentLocale] ?? values[defaultLocale];
78+
if (!value) {
79+
throw new Error(
80+
`Localized value for config key=${key} not found for both currentLocale=${currentLocale} or defaultLocale=${defaultLocale}`,
81+
);
82+
}
83+
return value;
84+
}
85+
6686
/** @type {import('@docusaurus/types').Config} */
6787
const config = {
6888
title: 'Docusaurus',
69-
tagline: 'Build optimized websites quickly, focus on your content',
89+
tagline: getLocalizedConfigValue('tagline'),
7090
organizationName: 'facebook',
7191
projectName: 'docusaurus',
7292
baseUrl,
@@ -83,17 +103,17 @@ const config = {
83103
},
84104
],
85105
i18n: {
86-
defaultLocale: 'en',
106+
defaultLocale,
87107

88108
locales:
89109
isDeployPreview || isBranchDeploy
90110
? // Deploy preview and branch deploys: keep them fast!
91-
['en']
111+
[defaultLocale]
92112
: isI18nStaging
93113
? // Staging locales: https://docusaurus-i18n-staging.netlify.app/
94-
['en', 'ja']
114+
[defaultLocale, 'ja']
95115
: // Production locales
96-
['en', 'fr', 'pt-BR', 'ko', 'zh-CN'],
116+
[defaultLocale, 'fr', 'pt-BR', 'ko', 'zh-CN'],
97117
},
98118
webpack: {
99119
jsLoader: (isServer) => ({
@@ -151,7 +171,7 @@ const config = {
151171
description:
152172
'Keep yourself up-to-date about new features in every release',
153173
copyright: `Copyright © ${new Date().getFullYear()} Facebook, Inc.`,
154-
language: 'en',
174+
language: defaultLocale,
155175
},
156176
},
157177
],
@@ -163,7 +183,7 @@ const config = {
163183
path: 'community',
164184
routeBasePath: 'community',
165185
editUrl: ({locale, versionDocsDirPath, docPath}) => {
166-
if (locale !== 'en') {
186+
if (locale !== defaultLocale) {
167187
return `https://crowdin.com/project/docusaurus-v2/${locale}`;
168188
}
169189
return `https://github.com/facebook/docusaurus/edit/main/website/${versionDocsDirPath}/${docPath}`;
@@ -292,7 +312,7 @@ const config = {
292312
// sidebarCollapsible: false,
293313
// sidebarCollapsed: true,
294314
editUrl: ({locale, docPath}) => {
295-
if (locale !== 'en') {
315+
if (locale !== defaultLocale) {
296316
return `https://crowdin.com/project/docusaurus-v2/${locale}`;
297317
}
298318
// We want users to submit doc updates to the upstream/next version!
@@ -332,7 +352,7 @@ const config = {
332352
// routeBasePath: '/',
333353
path: 'blog',
334354
editUrl: ({locale, blogDirPath, blogPath}) => {
335-
if (locale !== 'en') {
355+
if (locale !== defaultLocale) {
336356
return `https://crowdin.com/project/docusaurus-v2/${locale}`;
337357
}
338358
return `https://github.com/facebook/docusaurus/edit/main/website/${blogDirPath}/${blogPath}`;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"tagline": {
3+
"en": "Build optimized websites quickly, focus on your content",
4+
"fr": "Construisez rapidement des sites web optimisés, concentrez-vous sur votre contenu"
5+
}
6+
}

0 commit comments

Comments
 (0)