Implement "translations" AMD

This module is used to get translated messages via JSON
request in a synchronous manner. To ensure translatability
i18n messages should be initialized before any other JS code
interacted with user is run.

Fixes: https://pagure.io/freeipa/issue/7559
Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Petr Vobornik <pvoborni@redhat.com>
This commit is contained in:
Stanislav Levin 2018-06-26 12:28:28 +03:00 committed by Rob Crittenden
parent 86b57236c0
commit de58b80891
2 changed files with 77 additions and 0 deletions

View File

@ -43,6 +43,11 @@ define([
*/
url: '/ipa/ui/',
/**
* i18n messages url
*/
i18n_messages_url: '/ipa/i18n_messages',
/**
* RPC url
*/

View File

@ -0,0 +1,72 @@
//
// Copyright (C) 2018 FreeIPA Contributors see COPYING for license
//
define([
'./jquery',
'./config',
'./_base/i18n'
],
function( $, config, i18n) {
/**
*
* Retrieve translations from server
*
* @class translations
* @singleton
*
*/
var translations = {};
var retrieve = function() {
var result = false;
var jsondata = {
method: "i18n_messages",
params: [ [], { "version": window.ipa_loader.api_version } ]
};
var json_url = config.i18n_messages_url;
// test case: tests dir with crafted data stored in the local json file
if (window.location.protocol === 'file:') {
json_url = "data/" + jsondata.method + '.json';
}
var request = {
method: 'POST',
url: json_url,
data: JSON.stringify(jsondata),
dataType: "json",
contentType: 'application/json',
async: false,
processData: false,
success: success_handler,
error: error_handler
};
function error_handler(xhr, text_status, error_thrown) {
result = false;
}
function success_handler(data, text_status, xhr) {
if (!data.error) {
i18n.source = data.result.texts;
result = true;
}
}
$.ajax(request);
return result;
};
if (!retrieve()) {
throw new Error('Couldn\'t receive translations');
}
translations.update = retrieve;
return translations;
});