Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
knowledgebase-front
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Applications
Knowledge Base
knowledgebase-front
Commits
ed1d183d
Commit
ed1d183d
authored
5 years ago
by
Alice Poggioli
Browse files
Options
Downloads
Patches
Plain Diff
We can set the language given by the navigator's user.
parent
8f5143f7
No related branches found
No related tags found
1 merge request
!104
Feature/internationalization
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
server.js
+38
-3
38 additions, 3 deletions
server.js
with
38 additions
and
3 deletions
server.js
+
38
−
3
View file @
ed1d183d
const
port
=
900
0
;
const
port
=
900
1
;
const
browserSyncPort
=
3000
;
const
distPath
=
'
www
'
;
const
fs
=
require
(
'
fs
'
)
const
path
=
require
(
'
path
'
)
// express server
const
{
join
}
=
require
(
'
path
'
);
const
express
=
require
(
'
express
'
);
const
app
=
express
();
const
default_lang
=
'
en
'
function
parseLanguageData
(
stringData
)
{
var
arrayData
=
stringData
.
split
(
'
,
'
).
map
(
element
=>
element
.
split
(
/
[
-;
]
/
).
shift
()
);
arrayData
.
push
(
default_lang
)
return
arrayData
}
function
getAvailableLanguage
()
{
const
langs
=
fs
.
readdirSync
(
distPath
)
.
filter
(
f
=>
path
.
basename
(
f
,
'
.yml
'
).
match
(
/^index-
[
a-z
]{2}\.
html/
))
.
map
(
f
=>
f
.
match
(
/^index-
([
a-z
]{2})\.
html/
)[
1
])
return
langs
}
function
matchLanguage
(
availableLanguages
,
userLanguages
)
{
for
(
let
index
=
0
;
index
<
userLanguages
.
length
;
index
++
)
{
const
userLanguage
=
userLanguages
[
index
];
for
(
let
index
=
0
;
index
<
availableLanguages
.
length
;
index
++
)
{
const
availableLanguage
=
availableLanguages
[
index
];
if
(
userLanguage
===
availableLanguage
)
{
return
userLanguage
}
}
}
}
app
.
use
(
express
.
static
(
distPath
))
// .use('/src', express.static(join(__dirname, 'src')))
.
get
(
/^
[^
.
]
*$/
,
(
req
,
rep
)
=>
rep
.
sendFile
(
join
(
__dirname
,
distPath
,
'
/index-fr.html
'
))
.
get
(
/^
[^
.
]
*$/
,
(
req
,
rep
)
=>
{
var
langRequired
=
matchLanguage
(
getAvailableLanguage
(),
parseLanguageData
(
req
.
headers
[
"
accept-language
"
]))
console
.
log
(
'
langRequired :
'
,
langRequired
)
rep
.
sendFile
(
join
(
__dirname
,
distPath
,
`/index-
${
langRequired
}
.html`
))
}
)
.
listen
(
port
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment