diff --git a/Dockerfile b/Dockerfile index 097274e9da041..fccd7687a0476 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,6 +26,10 @@ FROM --platform=${BUILDPLATFORM} node:20-bullseye-slim AS superset-node ARG NPM_BUILD_CMD="build" +# Include translations in the final build. The default supports en only to +# reduce complexity and weight for those only using en +ARG BUILD_TRANSLATIONS="false" + # Used by docker-compose to skip the frontend build, # in dev we mount the repo and build the frontend inside docker ARG DEV_MODE="false" @@ -51,7 +55,7 @@ RUN --mount=type=bind,target=/frontend-mem-nag.sh,src=./docker/frontend-mem-nag. WORKDIR /app/superset-frontend # Creating empty folders to avoid errors when running COPY later on -RUN mkdir -p /app/superset/static/assets && mkdir -p /app/superset/translations +RUN mkdir -p /app/superset/static/assets RUN --mount=type=bind,target=./package.json,src=./superset-frontend/package.json \ --mount=type=bind,target=./package-lock.json,src=./superset-frontend/package-lock.json \ if [ "$DEV_MODE" = "false" ]; then \ @@ -62,20 +66,21 @@ RUN --mount=type=bind,target=./package.json,src=./superset-frontend/package.json # Runs the webpack build process COPY superset-frontend /app/superset-frontend +# This copies the .po files needed for translation +RUN mkdir -p /app/superset/translations +COPY superset/translations /app/superset/translations RUN if [ "$DEV_MODE" = "false" ]; then \ - npm run ${BUILD_CMD}; \ + BUILD_TRANSLATIONS=$BUILD_TRANSLATIONS npm run ${BUILD_CMD}; \ else \ echo "Skipping 'npm run ${BUILD_CMD}' in dev mode"; \ fi -# This copies the .po files needed for translation -RUN mkdir -p /app/superset/translations -COPY superset/translations /app/superset/translations + # Compiles .json files from the .po files, then deletes the .po files -RUN if [ "$DEV_MODE" = "false" ]; then \ +RUN if [ "$BUILD_TRANSLATIONS" = "true" ]; then \ npm run build-translation; \ else \ - echo "Skipping translations in dev mode"; \ + echo "Skipping translations as requested by build flag"; \ fi RUN rm /app/superset/translations/*/LC_MESSAGES/*.po RUN rm /app/superset/translations/messages.pot @@ -85,6 +90,10 @@ RUN rm /app/superset/translations/messages.pot ###################################################################### FROM python:${PY_VER} AS lean +# Include translations in the final build. The default supports en only to +# reduce complexity and weight for those only using en +ARG BUILD_TRANSLATIONS="false" + WORKDIR /app ENV LANG=C.UTF-8 \ LC_ALL=C.UTF-8 \ @@ -133,10 +142,14 @@ COPY --chown=superset:superset --from=superset-node /app/superset/translations s # Compile translations for the backend - this generates .mo files, then deletes the .po files COPY ./scripts/translations/generate_mo_files.sh ./scripts/translations/ -RUN ./scripts/translations/generate_mo_files.sh \ - && chown -R superset:superset superset/translations \ - && rm superset/translations/messages.pot \ - && rm superset/translations/*/LC_MESSAGES/*.po +RUN if [ "$BUILD_TRANSLATIONS" = "true" ]; then \ + ./scripts/translations/generate_mo_files.sh \ + && chown -R superset:superset superset/translations \ + && rm superset/translations/messages.pot \ + && rm superset/translations/*/LC_MESSAGES/*.po; \ + else \ + echo "Skipping translations as requested by build flag"; \ + fi COPY --chmod=755 ./docker/run-server.sh /usr/bin/ USER superset diff --git a/docs/docs/installation/docker-builds.mdx b/docs/docs/installation/docker-builds.mdx index 45a3b4c369a0f..0cdb8c9990bc6 100644 --- a/docs/docs/installation/docker-builds.mdx +++ b/docs/docs/installation/docker-builds.mdx @@ -67,6 +67,10 @@ script and the [docker.yml](https://github.com/apache/superset/blob/master/.gith GitHub action. ## Key ARGs in Dockerfile +- `BUILD_TRANSLATIONS`: whether to build the translations into the image. For the + frontend build this tells webpack to strip out all locales other than `en` from + the `moment-timezone` library. For the backendthis skips compiling the + `*.po` translation files - `DEV_MODE`: whether to skip the frontend build, this is used by our `docker-compose` dev setup where we mount the local volume and build using `webpack` in `--watch` mode, meaning as you alter the code in the local file system, webpack, from within a docker image used for this diff --git a/superset-frontend/webpack.config.js b/superset-frontend/webpack.config.js index 9c972702b240d..10bf4d49eec1d 100644 --- a/superset-frontend/webpack.config.js +++ b/superset-frontend/webpack.config.js @@ -45,10 +45,10 @@ const ROOT_DIR = path.resolve(__dirname, '..'); const TRANSLATIONS_DIR = path.resolve(__dirname, '../superset/translations'); const getAvailableTranslationCodes = () => { - const LOCALE_CODE_MAPPING = { - zh: 'zh-cn', - }; - try { + if (process.env.BUILD_TRANSLATIONS === 'true') { + const LOCALE_CODE_MAPPING = { + zh: 'zh-cn', + }; const files = fs.readdirSync(TRANSLATIONS_DIR); return files .filter(file => @@ -57,10 +57,9 @@ const getAvailableTranslationCodes = () => { .filter(dirName => !dirName.startsWith('__')) .map(dirName => dirName.replace('_', '-')) .map(dirName => LOCALE_CODE_MAPPING[dirName] || dirName); - } catch (err) { - console.error('Error reading the directory:', err); - return []; } + // Indicates to the MomentLocalesPlugin that we only want to keep 'en'. + return []; }; const {