Last active
December 2, 2022 09:28
-
-
Save bleuscyther/029c82cad7de4453ced93a4d11cf9a99 to your computer and use it in GitHub Desktop.
Angular 15 Universal + Internationalization (localize)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"i18n": { | |
"sourceLocale": { | |
"code": "en", | |
"baseHref": "/en/" | |
}, | |
"locales": { | |
"es": { | |
"translation": "src/locales/messages.es.xlf", | |
"baseHref": "/es/" | |
}, | |
"fr": { | |
"translation": "src/locales/messages.fr.xlf", | |
"baseHref": "/fr/" | |
}, | |
"ht": { | |
"translation": "src/locales/messages.ht.xlf", | |
"baseHref": "/ht/" | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "zone.js/node"; | |
import {ngExpressEngine} from "@nguniversal/express-engine"; | |
import * as express from "express"; | |
import {existsSync} from "fs"; | |
import {join} from "path"; | |
import {AppServerModule} from "./src/main.server"; | |
import {environment} from "./src/environments/environment"; | |
import {LOCALE_ID} from "@angular/core"; | |
import {APP_BASE_HREF} from "@angular/common"; | |
// The Express app is exported so that it can be used by serverless Functions. | |
export function app(): express.Express { | |
const server = express(); | |
const browser = environment.production ? "browser" : "dist/my-site/browser"; | |
const distFolder = join(process.cwd(), browser); | |
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/main/modules/express-engine) | |
server.engine("html", ngExpressEngine({ | |
bootstrap: AppServerModule, | |
})); | |
server.set("view engine", "html"); | |
server.set("views", distFolder); | |
server.get("/", function(req, res) { | |
res.redirect("/en"); | |
}); | |
// Example Express Rest API endpoints | |
// server.get('/api/**', (req, res) => { }); | |
// Serve static files from /browser | |
server.use(express.static(distFolder)); | |
// All regular routes use the Universal engine | |
server.get( | |
[ | |
"/:language(en|fr|es|ht)", | |
"/:language(en|fr|es|ht)/*", | |
], | |
(req, res) => { | |
const lang = req.params["language"]; | |
const indexHtml = existsSync( | |
join( | |
`${lang}/${distFolder}`, "index.original.html", | |
), | |
) ? "index.original.html" : "index"; | |
res.render(`${lang}/${indexHtml}`, | |
{ | |
req, | |
providers: [ | |
{provide: LOCALE_ID, useValue: lang}, | |
{provide: APP_BASE_HREF, useValue: `/${lang}/`}, | |
], | |
}, | |
); | |
}, | |
); | |
return server; | |
} | |
function run(): void { | |
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"; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment