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

Pass "?locale" URL query param to oVirt API #131

Merged
merged 1 commit into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 13 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import $ from 'jquery'

import { setLogDebug } from './helpers'
import { setLogDebug, getURLQueryParameterByName } from './helpers'

const CONFIG_URL = '/ovirt-engine/web-ui/ovirt-web-ui.config'

Expand All @@ -12,9 +12,15 @@ const AppConfiguration = {

consoleClientResourcesURL: 'https://www.ovirt.org/documentation/admin-guide/virt/console-client-resources/',
cockpitPort: '9090',

queryParams: { // from URL
locale: null,
},
}

export function readConfiguration () {
parseQueryParams()

return new Promise((resolve, reject) => {
$.ajax({
url: CONFIG_URL,
Expand All @@ -33,4 +39,10 @@ export function readConfiguration () {
})
}

function parseQueryParams () {
// TODO: align this with intl/index.js:getLocaleFromUrl()
AppConfiguration.queryParams.locale = getURLQueryParameterByName('locale')
console.log('parseQueryParams, provided locale: ', AppConfiguration.queryParams.locale)
}

export default AppConfiguration
17 changes: 17 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,20 @@ export function hrefWithoutHistory (handler) {
handler(e)
}
}

export function getURLQueryParameterByName (name) {
const url = window.location.href
name = name.replace(/[\[\]]/g, '\\$&')
const regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)')
const results = regex.exec(url)

if (!results) {
return null
}
if (!results[2]) {
return ''
}

const uriComponent = results[2].replace(/\+/g, ' ')
return decodeURIComponent(uriComponent)
}
3 changes: 3 additions & 0 deletions src/ovirtapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ OvirtApi = {
logDebug(`_httpGet start: url="${url}"`)
const headers = Object.assign({
'Authorization': `Bearer ${OvirtApi._getLoginToken()}`,
'Accept-Language': AppConfiguration.queryParams.locale, // can be: undefined, empty or string
}, custHeaders)
logDebug(`_httpGet: url="${url}", headers="${JSON.stringify(headers)}"`)

Expand All @@ -50,6 +51,7 @@ OvirtApi = {
'Accept': 'application/json',
'Content-Type': contentType,
'Authorization': `Bearer ${OvirtApi._getLoginToken()}`,
'Accept-Language': AppConfiguration.queryParams.locale,
'Filter': 'true',
},
data: input,
Expand All @@ -66,6 +68,7 @@ OvirtApi = {
'Accept': 'application/json',
'Content-Type': contentType,
'Authorization': `Bearer ${OvirtApi._getLoginToken()}`,
'Accept-Language': AppConfiguration.queryParams.locale,
},
data: input,
}).then((data: Object): Promise<Object> => Promise.resolve(data))
Expand Down