You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
Repository navigation
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
How to build SSR with multi-lang (i18n) with Angular 9 #1497
With the new angular version, documentation is not clear to me how to build angular 9 SSR with i18n.
Previously in version 8, I was doing the following commands:
ng build --configuration=production-ar && ng build --configuration=production-en && ng run website:server:production-en && ng run website:server:production-ar
Currently, I am lost how to build multi-lang without webpack.server.config.js .
Is there any template project for such requirements? I didn't find it in the documentation.
here is my current angular.json
here is my server.ts (I modified line 14 to read from /website/ar/browser)
How to make server.ts file work for both languages?
Should I have 2 instances of the server (line 44 in the image above)? one for 'en' and one for 'ar'?
What changes should I do to server.ts to do these 2 calls? Should I create 2 server.ts? ng run website:server:production-en && ng run website:server:production-ar
I was able to let it work by creating 2 server.ts files (server.ar.ts) and (server.en.ts)
With this implementation, I need to run 2 express engines (port 4000 & port 4001) on the server.
add these two files (server.ar.ts & server.en.ts) to tsconfig.server.json "files": [ "src/main.server.ts", "server.ar.ts", "server.en.ts" ],
server.en.ts looks like the following:
import 'zone.js/dist/zone-node';
import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import { join } from 'path';
import { AppServerModule } from './src/main.server';
import { APP_BASE_HREF } from '@angular/common';
import { existsSync } from 'fs';
// The Express app is exported so that it can be used by serverless Functions.
export function app() {
const server = express();
const distFolder = join(process.cwd(), 'dist/website/');
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
server.engine('html', ngExpressEngine({
bootstrap: AppServerModule,
}));
server.set('view engine', 'html');
server.set('views', join(distFolder, 'browser'));
// Example Express Rest API endpoints
// app.get('/api/**', (req, res) => { });
// Serve static files from /browser
server.get('/en/*.*', express.static(join(distFolder, 'browser'), { maxAge: '1y' }));
server.get('*.*', express.static(join(distFolder, 'browser', 'en'), { maxAge: '1y' }));
const indexHtml = join(distFolder, 'browser', 'en', 'index.html');
// All regular routes use the Universal engine
server.get('/en/*', (req, res) => {
res.render('en/index', { req, res });
});
server.get('*', (req, res) => {
res.render('en/index', { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
});
return server;
}
function run() {
const port = process.env.PORT || 4000;
// Start up the Node server
const server = app();
server.listen(port, () => {
console.log(`Node Express server listening on http://localhost:${port}`);
});
}
// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.
declare const __non_webpack_require__: NodeRequire;
const mainModule = __non_webpack_require__.main;
const moduleFilename = mainModule && mainModule.filename || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
run();
}
export * from './src/main.server';
add this to package.json "build:website:ssr": "ng build --configuration=production-ar && ng build --configuration=production-en ng run website:server:production-ar && ng run website:server:production-en"
update angular angular.json build and server
with this implementation my dist folder will be like the following:
-website
-browser
-en
-ar
-server
-en
-ar
If you are deploying to apache 2.4 you can configure the virtual host as follows
Activity
keserwan commentedon Feb 10, 2020
I am not able to configure SSR with i18n.
Previously everything was working fine with Webpack.server.config.js.
alan-agius4 commentedon Feb 10, 2020
Hi, let continue tracking this here #1497
alan-agius4 commentedon Feb 10, 2020
Wrong link angular/angular-cli#25726
keserwan commentedon Feb 11, 2020
I was able to let it work by creating 2 server.ts files (server.ar.ts) and (server.en.ts)
With this implementation, I need to run 2 express engines (port 4000 & port 4001) on the server.
add these two files (server.ar.ts & server.en.ts) to tsconfig.server.json
"files": [ "src/main.server.ts", "server.ar.ts", "server.en.ts" ],server.en.ts looks like the following:
add this to package.json
"build:website:ssr": "ng build --configuration=production-ar && ng build --configuration=production-en ng run website:server:production-ar && ng run website:server:production-en"update angular angular.json build and server


with this implementation my dist folder will be like the following:
If you are deploying to apache 2.4 you can configure the virtual host as follows
angular-automatic-lock-bot commentedon Mar 13, 2020
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.