From 2acd8ff94ad81013ba35365c63705668b71dc083 Mon Sep 17 00:00:00 2001 From: Jagankumar <53823168+jagankumar-egov@users.noreply.github.com> Date: Tue, 2 Jul 2024 23:20:19 +0530 Subject: [PATCH] added base localisation --- micro-frontends/sample/docker/Dockerfile | 21 ++++++++++++------ micro-frontends/sample/fetch-locales.js | 27 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 micro-frontends/sample/fetch-locales.js diff --git a/micro-frontends/sample/docker/Dockerfile b/micro-frontends/sample/docker/Dockerfile index 333834d917a..84df7782e4d 100644 --- a/micro-frontends/sample/docker/Dockerfile +++ b/micro-frontends/sample/docker/Dockerfile @@ -1,4 +1,4 @@ -# Use Node.js base image with version 16 +# Stage 1: Fetch locales and build the app FROM node:20 AS build # Set working directory @@ -17,11 +17,11 @@ ENV COMMIT_ID=$COMMIT_ID # Copy package.json and yarn.lock (if exists) COPY package.json ./ +# Clean up node_modules and yarn.lock RUN rm -rf node_modules/ - RUN rm -rf yarn.lock -# clear cache +# Clear yarn cache RUN yarn cache clean # Install dependencies @@ -33,15 +33,24 @@ LABEL commit_id=$COMMIT_ID # Copy the rest of the application COPY . . -# Build the React app with Webpack +# Fetch locales before building the app +COPY fetch-locales.js . +RUN node fetch-locales.js + +# Build the React app with Vite RUN yarn build +# Stage 2: Serve the built app using nginx FROM nginx:mainline-alpine +# Define the work directory for nginx ENV WORK_DIR=/var/web/sample-ui +# Create the work directory RUN mkdir -p ${WORK_DIR} - +# Copy the build files from the previous stage COPY --from=build /app/dist ${WORK_DIR}/ -COPY --from=build /app/docker/nginx.conf /etc/nginx/conf.d/default.conf \ No newline at end of file + +# Copy the nginx configuration file +COPY --from=build /app/docker/nginx.conf /etc/nginx/conf.d/default.conf diff --git a/micro-frontends/sample/fetch-locales.js b/micro-frontends/sample/fetch-locales.js new file mode 100644 index 00000000000..e8745ff6a4d --- /dev/null +++ b/micro-frontends/sample/fetch-locales.js @@ -0,0 +1,27 @@ +const axios = require('axios'); +const fs = require('fs'); +const path = require('path'); + +const locales = [ + { lang: 'en', url: 'https://api.example.com/locales/en' }, + { lang: 'fr', url: 'https://api.example.com/locales/fr' }, + { lang: 'de', url: 'https://api.example.com/locales/de' } +]; + +const fetchLocales = async () => { + for (const locale of locales) { + try { + const response = await axios.get(locale.url); + const dir = path.join(__dirname, 'public', 'locales', locale.lang); + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir, { recursive: true }); + } + fs.writeFileSync(path.join(dir, 'translation.json'), JSON.stringify(response.data, null, 2)); + console.log(`Locale ${locale.lang} saved.`); + } catch (error) { + console.error(`Error fetching locale ${locale.lang}:`, error); + } + } +}; + +fetchLocales();