Universal i18n server
I'm trying to use Universal with i18n.
I'm already building the app and setting the server config so that once the app passes to browser-side mode the user is redirected to the correct translation of the app, that is fine.
The issue I'm having is in the server-side rendering.
With the way that the express server is set, I don't see how to provide the universal's server-side correct translation, and only the default language is being displayed instead of the local one.
Like with browser-side, I tried to have a different builds, one for each language, for the main.bundle file used by the server-side mode. Problem is, I can't set more than one of those files per app.
Dist folder structure:
dist/
server.js
browser/
en/
...
it/
...
server/
en/
...
main.bundle // eng translation
it/
...
main.bundle // ita translation
server.ts file
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
// In this line I can only provide one file for the server-side translation,
// and I can't dynamically change it to the correct translation.
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } =
require("./dist/server/en/main.bundle");
app.engine("html", ngExpressEngine({
bootstrap: AppServerModuleNgFactory,
providers: [
provideModuleMap(LAZY_MODULE_MAP),
],
}));
The server-side app is rendered from the main.bundle in the fourth line. However I don't get the possibility to provide one for each translation, how can I fix that?
My point is having Angular Universal provide the correct server-side translation of the app.
I assume your urls are build like "/en/sample" for all english sites ("/en/" as base) and "/sample" for your italian sites (default, base "/"). If this assumption is incorrect adjust this code to your needs. Basically you want to build two seperate engines and use the correct one depending on the called url.
// import your bundles
const itBundle = require('./dist/server/it/main.bundle');
const enBundle = require('./dist/server/en/main.bundle');
// define your engines for it and en
// id is needed to find the path
// base is for routing see below
const languageEngines = [{
id: 'en',
base: '/en/',
engine: ngExpressEngine({
bootstrap: enBundle.AppServerModuleNgFactory,
providers: [provideModuleMap(enBundle.LAZY_MODULE_MAP)]
})
},
{
id: 'it',
base: '',
engine: ngExpressEngine({
bootstrap: itBundle.AppServerModuleNgFactory,
providers: [provideModuleMap(itBundle.LAZY_MODULE_MAP)]
})
}];
// Load your engine
app.engine('html', (filePath, options, callback) => {
options.engine(
filePath,
{ req: options.req, res: options.res},
callback
)
});
app.set('view engine', 'html');
// handle en file routes
app.get('/en/*.*', express.static('./dist/browser'));
// file routes for it
app.get('*.*', express.static('./dist/browser/it'));
// handle routes for each language
languageEngines.forEach(languageEngine => {
app.get(`${languageEngine.base}*`, (req, res) => {
res.render(`./dist/browser/${languageEngine.id}/index.html`, {
req,
res,
engine: languageEngine.engine
})
})
});
If you have questions feel free to ask.
链接地址: http://www.djcxy.com/p/31402.html上一篇: Angular 4通用延迟加载错误
下一篇: 通用i18n服务器