diff --git a/backend/model/Localizations.ts b/backend/model/Localizations.ts new file mode 100644 index 00000000..7b5af32e --- /dev/null +++ b/backend/model/Localizations.ts @@ -0,0 +1,18 @@ +import {ProjectPath} from "../ProjectPath"; +import * as fs from "fs"; +import * as path from "path"; +import {Config} from "../../common/config/private/Config"; + +export class Localizations { + + constructor() { + } + + public static init() { + const notLanguage = ['assets']; + const dirCont = fs.readdirSync(ProjectPath.FrontendFolder).filter(f => fs.statSync(path.resolve(ProjectPath.FrontendFolder, f)).isDirectory()); + Config.Client.languages = dirCont.filter(d => notLanguage.indexOf(d) == -1); + Config.Client.languages.push("en"); + } + +} diff --git a/backend/routes/PublicRouter.ts b/backend/routes/PublicRouter.ts index 501b67be..988e0568 100644 --- a/backend/routes/PublicRouter.ts +++ b/backend/routes/PublicRouter.ts @@ -1,16 +1,46 @@ -import * as _express from "express"; import {NextFunction, Request, Response} from "express"; -import * as _path from "path"; +import * as path from "path"; +import * as fs from "fs"; import {Utils} from "../../common/Utils"; import {Config} from "../../common/config/private/Config"; import {ProjectPath} from "../ProjectPath"; import {AuthenticationMWs} from "../middlewares/user/AuthenticationMWs"; +import {CookieNames} from "../../common/CookieNames"; + export class PublicRouter { public static route(app) { + const setLocale = (req: Request, res: Response, next: Function) => { + let localePath = ""; + let selectedLocale = req['locale']; + if (req.cookies && req.cookies[CookieNames.lang]) { + if (Config.Client.languages.indexOf(req.cookies[CookieNames.lang]) !== -1) { + console.log(selectedLocale); + selectedLocale = req.cookies[CookieNames.lang]; + } + } + if (selectedLocale !== 'en') { + localePath = req['locale']; + } + res.cookie(CookieNames.lang, selectedLocale); + req['localePath'] = localePath; + next(); + }; + const renderIndex = (req: Request, res: Response) => { - res.sendFile(_path.resolve(ProjectPath.FrontendFolder, 'index.html'), {maxAge: 31536000}); + res.sendFile(path.resolve(ProjectPath.FrontendFolder, req['localePath'], 'index.html'), {maxAge: 31536000}); + }; + + + const redirectToBase = (locale: string) => { + return (req: Request, res: Response) => { + console.log(locale); + if (Config.Client.languages.indexOf(locale) !== -1) { + res.cookie(CookieNames.lang, locale); + } + res.redirect("/"); + }; }; app.use( @@ -29,16 +59,42 @@ export class PublicRouter { }); app.get('/config_inject.js', (req: Request, res: Response) => { - res.render(_path.resolve(ProjectPath.FrontendFolder, 'config_inject.ejs'), res.tpl); + res.render(path.resolve(ProjectPath.FrontendFolder, 'config_inject.ejs'), res.tpl); }); + app.get(['/', '/login', "/gallery*", "/share*", "/admin", "/search*"], AuthenticationMWs.tryAuthenticate, + setLocale, renderIndex ); + Config.Client.languages.forEach(l => { + app.get(['/' + l + '/', '/' + l + '/login', '/' + l + "/gallery*", '/' + l + "/share*", '/' + l + "/admin", '/' + l + "/search*"], + redirectToBase(l) + ); + }); - app.use(_express.static(ProjectPath.FrontendFolder, {maxAge: 31536000})); - // app.use('/node_modules', _express.static(_path.resolve(__dirname, './../../node_modules'))); - // app.use('/common', _express.static(_path.resolve(__dirname, './../../common'))); + app.get('/assets/:file', + setLocale, + (req: Request, res: Response) => { + const file = path.resolve(ProjectPath.FrontendFolder, req['localePath'], 'assets', req.params.file); + fs.exists(file, (exists: boolean) => { + if (!exists) { + return res.sendStatus(404); + } + res.sendFile(file); + }); + }); + app.get('/:file', + setLocale, + (req: Request, res: Response) => { + const file = path.resolve(ProjectPath.FrontendFolder, req['localePath'], req.params.file); + fs.exists(file, (exists: boolean) => { + if (!exists) { + return res.sendStatus(404); + } + res.sendFile(file); + }); + }); } } diff --git a/backend/server.ts b/backend/server.ts index 985aa66a..7512c120 100644 --- a/backend/server.ts +++ b/backend/server.ts @@ -1,6 +1,8 @@ import * as _express from "express"; import * as _bodyParser from "body-parser"; +import * as cookieParser from "cookie-parser"; import * as _http from "http"; +import * as locale from "locale"; import {PublicRouter} from "./routes/PublicRouter"; import {UserRouter} from "./routes/UserRouter"; import {GalleryRouter} from "./routes/GalleryRouter"; @@ -16,6 +18,8 @@ import {ThumbnailGeneratorMWs} from "./middlewares/thumbnail/ThumbnailGeneratorM import {DiskManager} from "./model/DiskManger"; import {NotificationRouter} from "./routes/NotificationRouter"; import {ConfigDiagnostics} from "./model/ConfigDiagnostics"; +import {Localizations} from "./model/Localizations"; +import {CookieNames} from "../common/CookieNames"; const _session = require('cookie-session'); @@ -56,7 +60,7 @@ export class Server { } this.app.use(_session({ - name: "pigallery2-session", + name: CookieNames.session, keys: ["key1" + s4() + s4() + s4() + s4(), "key2" + s4() + s4() + s4() + s4(), "key3" + s4() + s4() + s4() + s4()] })); @@ -71,9 +75,13 @@ export class Server { */ // for parsing application/json this.app.use(_bodyParser.json()); + this.app.use(cookieParser()); DiskManager.init(); ThumbnailGeneratorMWs.init(); + Localizations.init(); + + this.app.use(locale(Config.Client.languages, "en")); if (Config.Server.database.type != DatabaseType.memory) { await ObjectManagerRepository.InitSQLManagers(); } else { diff --git a/common/CookieNames.ts b/common/CookieNames.ts new file mode 100644 index 00000000..8a2258e2 --- /dev/null +++ b/common/CookieNames.ts @@ -0,0 +1,4 @@ +export class CookieNames { + public static lang = "pigallery2-lang"; + public static session = "pigallery2-session"; +} diff --git a/common/config/public/ConfigClass.ts b/common/config/public/ConfigClass.ts index 24af6c3a..d1aab99a 100644 --- a/common/config/public/ConfigClass.ts +++ b/common/config/public/ConfigClass.ts @@ -36,6 +36,7 @@ export module ClientConfig { enableOnScrollThumbnailPrioritising: boolean; authenticationRequired: boolean; publicUrl: string; + languages: string[]; } } @@ -73,7 +74,8 @@ export class PublicConfigClass { enableOnScrollRendering: true, enableOnScrollThumbnailPrioritising: true, authenticationRequired: true, - publicUrl: "" + publicUrl: "", + languages: [] }; } diff --git a/frontend/app/admin/admin.component.html b/frontend/app/admin/admin.component.html index ed9e50b1..a8325e1f 100644 --- a/frontend/app/admin/admin.component.html +++ b/frontend/app/admin/admin.component.html @@ -3,7 +3,7 @@
-

Server notifications

+

Server notifications

@@ -16,14 +16,14 @@
-
- -
+ +