【发布时间】:2021-10-15 20:25:01
【问题描述】:
我将 i18n 与 Angular Universal SSR 一起使用。问题是客户端收到源语言环境中的文本,几秒钟后被正确的语言环境替换。
例如,客户端加载 http://localhost:4000/en-US/ 在第一次显示中显示为 es 语言环境,几秒钟后文本为替换为 en-US 语言环境文本。
构建文件夹已正确创建,并且代理适用于每个区域设置。我希望服务器返回带有正确翻译的 html,以便 SEO 爬虫可以在每个语言环境中正确找到内容。
似乎问题出在构建中,不是使用正确的语言环境生成的。
angular.json 文件中的项目配置:
...
"i18n": {
"sourceLocale": "es",
"locales": {
"en-US": {
"translation":"src/i18n/messages.en.xlf",
"baseHref": "en-US/"
}
}
},
...
"architect": {
"build": {
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"outputPath": "dist/web/browser",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"aot": true,
"localize": true,
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"scripts": [],
"customWebpackConfig":{
"path": "./webpack.config.js"
}
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"namedChunks": false,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
}
]
}
},
...
}
构建项目的命令:
ng build --prod --configuration=production && ng run web:server:production
构建目录导致路径dist/web/browser:
en-US/
es/
server.ts文件:
export function app(lang: string): express.Express {
const server = express();
server.use(compression());
const distFolder = join(process.cwd(), `dist/web/browser/${lang}`);
const indexHtml = 'index.html';
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
server.engine('html', ngExpressEngine({
bootstrap: AppServerModule,
extraProviders: [{ provide: LOCALE_ID, useValue: lang }],
} as any));
server.set('view engine', 'html');
server.set('views', distFolder);
server.get('*.*', express.static(distFolder, {
maxAge: '1y'
}));
server.get('*', (req, res) => {
console.log(`LOADING DIST FOLDER: ${distFolder}`)
console.log(`LOADING INDEX: ${indexHtml}`)
res.render(`${indexHtml}`, {
req,
res,
providers: [
{ provide: APP_BASE_HREF, useValue: req.baseUrl },
{ provide: NgxRequest, useValue: req },
{ provide: NgxResponse, useValue: res }
]
});
});
return server;
}
function run(): void {
const port = process.env.PORT || 4000;
// Start up the Node server
const server = express();
const appEs = app('es');
const appEn = app('en-US');
server.use('/en-US', appEn);
server.use('/es', appEs);
server.use('', appEs);
server.use(compression());
server.use(cookieparser());
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';
【问题讨论】:
标签: angular server-side-rendering angular-i18n