Skip to content

Commit

Permalink
Pass "?locale" URL query param to oVirt API
Browse files Browse the repository at this point in the history
With this change, the oVirt API returns localized error messages, so
they can be displayed to the user without additional modification.

Fixes: #110
  • Loading branch information
mareklibra committed Nov 28, 2017
1 parent a12095a commit 7795c48
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
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

0 comments on commit 7795c48

Please sign in to comment.