diff options
author | Chocobozzz <me@florianbigard.com> | 2018-06-28 13:59:48 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-06-28 15:53:12 +0200 |
commit | 8afc19a6121569da054462c7cb351a3f13030a32 (patch) | |
tree | 6dba42963681062536e635dd3ca401e1c7b0ca9f /server/controllers | |
parent | 3ea9a1c311c3e3c55fb95560d4dd99a77c52df4a (diff) | |
download | PeerTube-8afc19a6121569da054462c7cb351a3f13030a32.tar.gz PeerTube-8afc19a6121569da054462c7cb351a3f13030a32.tar.zst PeerTube-8afc19a6121569da054462c7cb351a3f13030a32.zip |
Add ability to choose the language
Diffstat (limited to 'server/controllers')
-rw-r--r-- | server/controllers/client.ts | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/server/controllers/client.ts b/server/controllers/client.ts index 385757fa6..dfffe5487 100644 --- a/server/controllers/client.ts +++ b/server/controllers/client.ts | |||
@@ -7,8 +7,14 @@ import { ACCEPT_HEADERS, CONFIG, EMBED_SIZE, OPENGRAPH_AND_OEMBED_COMMENT, STATI | |||
7 | import { asyncMiddleware } from '../middlewares' | 7 | import { asyncMiddleware } from '../middlewares' |
8 | import { VideoModel } from '../models/video/video' | 8 | import { VideoModel } from '../models/video/video' |
9 | import { VideoPrivacy } from '../../shared/models/videos' | 9 | import { VideoPrivacy } from '../../shared/models/videos' |
10 | import { buildFileLocale, getCompleteLocale, getDefaultLocale, is18nLocale } from '../../shared/models' | 10 | import { |
11 | import { LOCALE_FILES } from '../../shared/models/i18n/i18n' | 11 | buildFileLocale, |
12 | getCompleteLocale, | ||
13 | getDefaultLocale, | ||
14 | is18nLocale, | ||
15 | LOCALE_FILES, | ||
16 | POSSIBLE_LOCALES | ||
17 | } from '../../shared/models/i18n/i18n' | ||
12 | 18 | ||
13 | const clientsRouter = express.Router() | 19 | const clientsRouter = express.Router() |
14 | 20 | ||
@@ -22,7 +28,8 @@ clientsRouter.use('/videos/watch/:id', | |||
22 | asyncMiddleware(generateWatchHtmlPage) | 28 | asyncMiddleware(generateWatchHtmlPage) |
23 | ) | 29 | ) |
24 | 30 | ||
25 | clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => { | 31 | clientsRouter.use('' + |
32 | '/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
26 | res.sendFile(embedPath) | 33 | res.sendFile(embedPath) |
27 | }) | 34 | }) |
28 | 35 | ||
@@ -63,7 +70,7 @@ clientsRouter.use('/client/*', (req: express.Request, res: express.Response, nex | |||
63 | // Try to provide the right language index.html | 70 | // Try to provide the right language index.html |
64 | clientsRouter.use('/(:language)?', function (req, res) { | 71 | clientsRouter.use('/(:language)?', function (req, res) { |
65 | if (req.accepts(ACCEPT_HEADERS) === 'html') { | 72 | if (req.accepts(ACCEPT_HEADERS) === 'html') { |
66 | return res.sendFile(getIndexPath(req, req.params.language)) | 73 | return res.sendFile(getIndexPath(req, res, req.params.language)) |
67 | } | 74 | } |
68 | 75 | ||
69 | return res.status(404).end() | 76 | return res.status(404).end() |
@@ -77,16 +84,24 @@ export { | |||
77 | 84 | ||
78 | // --------------------------------------------------------------------------- | 85 | // --------------------------------------------------------------------------- |
79 | 86 | ||
80 | function getIndexPath (req: express.Request, paramLang?: string) { | 87 | function getIndexPath (req: express.Request, res: express.Response, paramLang?: string) { |
81 | let lang: string | 88 | let lang: string |
82 | 89 | ||
83 | // Check param lang validity | 90 | // Check param lang validity |
84 | if (paramLang && is18nLocale(paramLang)) { | 91 | if (paramLang && is18nLocale(paramLang)) { |
85 | lang = paramLang | 92 | lang = paramLang |
93 | |||
94 | // Save locale in cookies | ||
95 | res.cookie('clientLanguage', lang, { | ||
96 | secure: CONFIG.WEBSERVER.SCHEME === 'https', | ||
97 | sameSite: true, | ||
98 | maxAge: 1000 * 3600 * 24 * 90 // 3 months | ||
99 | }) | ||
100 | |||
101 | } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) { | ||
102 | lang = req.cookies.clientLanguage | ||
86 | } else { | 103 | } else { |
87 | // lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale() | 104 | lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale() |
88 | // Disable auto language for now | ||
89 | lang = getDefaultLocale() | ||
90 | } | 105 | } |
91 | 106 | ||
92 | return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html') | 107 | return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html') |
@@ -181,18 +196,18 @@ async function generateWatchHtmlPage (req: express.Request, res: express.Respons | |||
181 | } else if (validator.isInt(videoId)) { | 196 | } else if (validator.isInt(videoId)) { |
182 | videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(+videoId) | 197 | videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(+videoId) |
183 | } else { | 198 | } else { |
184 | return res.sendFile(getIndexPath(req)) | 199 | return res.sendFile(getIndexPath(req, res)) |
185 | } | 200 | } |
186 | 201 | ||
187 | let [ file, video ] = await Promise.all([ | 202 | let [ file, video ] = await Promise.all([ |
188 | readFileBufferPromise(getIndexPath(req)), | 203 | readFileBufferPromise(getIndexPath(req, res)), |
189 | videoPromise | 204 | videoPromise |
190 | ]) | 205 | ]) |
191 | 206 | ||
192 | const html = file.toString() | 207 | const html = file.toString() |
193 | 208 | ||
194 | // Let Angular application handle errors | 209 | // Let Angular application handle errors |
195 | if (!video || video.privacy === VideoPrivacy.PRIVATE) return res.sendFile(getIndexPath(req)) | 210 | if (!video || video.privacy === VideoPrivacy.PRIVATE) return res.sendFile(getIndexPath(req, res)) |
196 | 211 | ||
197 | const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video) | 212 | const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video) |
198 | res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags) | 213 | res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags) |