Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add internationalization support #78

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
8df076e
feat: add internationalization support
mistakia Feb 17, 2024
8676de9
feat: added `home` to locales json
mistakia Feb 20, 2024
9a9407f
fix: use async instead of `fs.existsSync`
mistakia Feb 20, 2024
56c8714
fix: remove unnecessary react fragments
mistakia Feb 20, 2024
ad26736
feat: cleanup and add missing `en` localization, added `es` localizat…
mistakia Feb 20, 2024
63f1e8c
feat: add `fr` locale
mistakia Feb 20, 2024
d769d7a
feat: add `it` locale
mistakia Feb 20, 2024
41b05bf
feat: add `de` localization
mistakia Feb 20, 2024
325fc51
feat: add `nl` localization
mistakia Feb 20, 2024
fa02272
feat: add `ru` localization
mistakia Feb 21, 2024
94d99b1
fix: change locale select `renderValue` prop
mistakia Feb 21, 2024
8aae7a0
feat: organize docs by locale
mistakia Feb 21, 2024
4c9dcd5
feat: add `ar` localization
mistakia Feb 21, 2024
9c42f69
feat: add `fa` localization
mistakia Feb 21, 2024
2f1394f
feat: add `hi` localization
mistakia Feb 21, 2024
42411d9
feat: add `ja` localization
mistakia Feb 21, 2024
acc0ebf
feat: add `ko` localization
mistakia Feb 22, 2024
227b33f
feat: add `pl` localization
mistakia Feb 22, 2024
04fdee9
feat: add `pt` localization
mistakia Feb 22, 2024
566bf4a
feat: add `tr` localization
mistakia Feb 23, 2024
a0c56e3
feat: add `vi` localization
mistakia Feb 23, 2024
a755d97
feat: add `zh` localization
mistakia Feb 23, 2024
b0dee87
feat: add `no` localization
mistakia Feb 23, 2024
a3c0d28
chore: update yarn.lock
mistakia Apr 4, 2024
c09dc82
fix: locale race condition on doc page load
mistakia Apr 4, 2024
dceeb4e
feat: detect browser locale preference
mistakia Apr 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions api/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import serveStatic from 'serve-static'
import cors from 'cors'
import favicon from 'express-favicon'
import robots from 'express-robots-txt'
import { slowDown } from 'express-slow-down'

import * as config from '#config'
import * as routes from '#api/routes/index.mjs'
Expand Down Expand Up @@ -70,6 +71,9 @@ api.use((req, res, next) => {
const resourcesPath = path.join(__dirname, '..', 'resources')
api.use('/resources', serveStatic(resourcesPath))

const localesPath = path.join(__dirname, '..', 'locales')
api.use('/locales', serveStatic(localesPath))

const dataPath = path.join(__dirname, '..', 'data')
api.use('/data', serveStatic(dataPath))

Expand All @@ -95,9 +99,52 @@ api.use('/api/representatives', routes.representatives)
api.use('/api/weight', routes.weight)

const docsPath = path.join(__dirname, '..', 'docs')
api.use('/api/docs', serveStatic(docsPath))
api.get('/api/docs/*', (req, res) => {
res.status(404).send('Not found')

const speedLimiter = slowDown({
windowMs: 10 * 60 * 1000, // 10 minutes
delayAfter: 50, // allow 50 requests per 10 minutes, then...
delayMs: 500, // begin adding 500ms of delay per request above 50:
maxDelayMs: 20000 // maximum delay of 20 seconds
})
mistakia marked this conversation as resolved.
Show resolved Hide resolved

api.use('/api/docs', speedLimiter, serveStatic(path.join(docsPath, 'en')))
api.use('/api/docs/en', speedLimiter, serveStatic(path.join(docsPath, 'en')))
api.get('/api/docs/:locale/*', speedLimiter, async (req, res) => {
const { locale } = req.params
const doc_id = req.params[0] // Capture the rest of the path as doc_id
const localized_doc_path = path.join(docsPath, locale, `${doc_id}.md`)
const default_doc_path = path.join(docsPath, 'en', `${doc_id}.md`)

// check if paths are under the docs directory
if (
!localized_doc_path.startsWith(docsPath) ||
!default_doc_path.startsWith(docsPath)
) {
return res.status(403).send('Forbidden')
}

try {
if (
await fs.promises
.access(localized_doc_path, fs.constants.F_OK)
.then(() => true)
.catch(() => false)
) {
return res.sendFile(localized_doc_path)
Fixed Show fixed Hide fixed
} else if (
await fs.promises
.access(default_doc_path, fs.constants.F_OK)
.then(() => true)
.catch(() => false)
) {
return res.redirect(`/api/docs/en/${doc_id}`)
} else {
return res.status(404).send('Document not found')
}
} catch (error) {
console.error(error)
return res.status(500).send('Internal Server Error')
}
mistakia marked this conversation as resolved.
Show resolved Hide resolved
})

api.use('/api/*', (err, req, res, next) => {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
387 changes: 387 additions & 0 deletions locales/ar.json

Large diffs are not rendered by default.

Loading