From 7795c48b77e4d69301b11f86fc9c1fe5019c6bd1 Mon Sep 17 00:00:00 2001 From: Marek Libra Date: Mon, 10 Apr 2017 14:06:30 +0200 Subject: [PATCH] Pass "?locale" URL query param to oVirt API With this change, the oVirt API returns localized error messages, so they can be displayed to the user without additional modification. Fixes: https://github.com/oVirt/ovirt-web-ui/issues/110 --- src/config.js | 14 +++++++++++++- src/helpers.js | 17 +++++++++++++++++ src/ovirtapi.js | 3 +++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/config.js b/src/config.js index 82cdd45ec..e56967166 100644 --- a/src/config.js +++ b/src/config.js @@ -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' @@ -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, @@ -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 diff --git a/src/helpers.js b/src/helpers.js index de633fc0c..aecf6e1d2 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -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) +} diff --git a/src/ovirtapi.js b/src/ovirtapi.js index 6363bf532..75d1b4643 100644 --- a/src/ovirtapi.js +++ b/src/ovirtapi.js @@ -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)}"`) @@ -50,6 +51,7 @@ OvirtApi = { 'Accept': 'application/json', 'Content-Type': contentType, 'Authorization': `Bearer ${OvirtApi._getLoginToken()}`, + 'Accept-Language': AppConfiguration.queryParams.locale, 'Filter': 'true', }, data: input, @@ -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 => Promise.resolve(data))