Skip to content

Commit

Permalink
feat: Add support for X-Forwarded-Host (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
darcoder authored and paulgv committed May 30, 2018
1 parent cbee47d commit 514ad63
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,27 @@ export const getLocaleFromRoute = (route = {}, routesNameSeparator = '', locales
return null
}

/**
* Get x-forwarded-host
* @return {String} x-forwarded-host
*/
export const getForwarded = () => (
process.browser ? window.location.href.split('/')[2] : (req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'] : req.headers.host) // eslint-disable-line
)

/**
* Get hostname
* @return {String} Hostname
*/
export const getHostname = () => (
process.browser ? window.location.href.split('/')[2] : req.headers.host // eslint-disable-line
)

/**
* Get locale code that corresponds to current hostname
* @return {String} Locade code found if any
*/
export const getLocaleDomain = () => {
const hostname = getHostname()
const hostname = app.i18n.forwardedHost ? getForwarded() : getHostname()
if (hostname) {
const localeDomain = app.i18n.locales.find(l => l[LOCALE_DOMAIN_KEY] === hostname) // eslint-disable-line
if (localeDomain) {
Expand Down
2 changes: 2 additions & 0 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { makeRoutes } from './helpers/routes'
import {
getLocaleCodes,
getLocaleFromRoute,
getForwarded,
getHostname,
getLocaleDomain,
syncVuex
Expand Down Expand Up @@ -45,6 +46,7 @@ export default function (userOptions) {
LOCALE_FILE_KEY,
getLocaleCodes,
getLocaleFromRoute,
getForwarded,
getHostname,
getLocaleDomain,
syncVuex
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default async ({ app, route, store, req }) => {
const getLocaleCodes = <%= options.getLocaleCodes %>
const getLocaleFromRoute = <%= options.getLocaleFromRoute %>
const getHostname = <%= options.getHostname %>
const getForwarded = <%= options.getForwarded %>
const getLocaleDomain = <%= options.getLocaleDomain %>
const syncVuex = <%= options.syncVuex %>

Expand Down Expand Up @@ -51,6 +52,7 @@ export default async ({ app, route, store, req }) => {
app.i18n.locales = <%= JSON.stringify(options.locales) %>
app.i18n.defaultLocale = '<%= options.defaultLocale %>'
app.i18n.differentDomains = <%= options.differentDomains %>
app.i18n.forwardedHost = <%= options.forwardedHost %>
app.i18n.routesNameSeparator = '<%= options.routesNameSeparator %>'
app.i18n.beforeLanguageSwitch = <%= options.beforeLanguageSwitch %>
app.i18n.onLanguageSwitched = <%= options.onLanguageSwitched %>
Expand Down

0 comments on commit 514ad63

Please sign in to comment.