使用Angular Universal进行国际化



我正在尝试用不同的语言发布我的网站,并遵循官方Angular本地化指南(https://angular.io/guide/i18n)。

然而,当我构建项目并提供它时,我得到错误Failed to lookup view "index" in views directory "C:PATH_TO_PROJECTdistbrowser"。这是因为dist/browser目录现在被分成了不同的语言。如何配置服务器以服务每种语言?Angular确实给出了如何配置Nginx和Apache服务器的说明,但我找不到任何关于Angular Universal的说明。

server.ts

/***************************************************************************************************
* Load `$localize` onto the global scope - used if i18n tags appear in Angular templates.
*/
import '@angular/localize/init';
// Polyfills required for Firebase
import 'globalthis/auto';
(global as any).WebSocket = require('ws');
(global as any).XMLHttpRequest = require('xhr2');
import 'zone.js/dist/zone-node';
import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import * as compression from 'compression';
import { join } from 'path';
import { AppServerModule } from './src/main.server';
import { APP_BASE_HREF } from '@angular/common';
import { existsSync } from 'fs';
import { LOCALE_ID } from '@angular/core';
const domino = require('domino');
// The Express app is exported so that it can be used by serverless Functions.
export function app(lang: string): express.Express {
const server = express();
const distFolder = join(process.cwd(), `dist/browser${lang}`);
const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';
const win = domino.createWindow(indexHtml);
global['window'] = win;
global['document'] = win.document;
server.use(express.json());
// 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', distFolder);
// Serve static files from /browser
server.get('*.*', express.static(distFolder, {
maxAge: '1y'
}));
// All regular routes use the Universal engine
server.get('*', (req, res) => {
res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }, { provide: LOCALE_ID, useValue: lang }] });
});
return server;
}
function run(): void {
const port = process.env.PORT || 4200;
// Start up the Node server
const appEn = app('en');
const appFr = app('fr');
const appDe = app('de');
const server = express();
server.use('', appEn);
server.use('/en', appEn);
server.use('/fr', appFr);
server.use('/de', appDe);
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';

我也有麻烦的预渲染应用程序。我得到错误An unhandled exception occurred: Could not find the main bundle: C:PATH_TO_PROJECTdistserveren-USmain.js

提前感谢!

我最终使用了一个代理,我称之为server.run.js,它会将用户路由到正确的url。

最新更新