mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
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:
parent
86b57236c0
commit
de58b80891
@ -43,6 +43,11 @@ define([
|
||||
*/
|
||||
url: '/ipa/ui/',
|
||||
|
||||
/**
|
||||
* i18n messages url
|
||||
*/
|
||||
i18n_messages_url: '/ipa/i18n_messages',
|
||||
|
||||
/**
|
||||
* RPC url
|
||||
*/
|
||||
|
72
install/ui/src/freeipa/translations.js
Normal file
72
install/ui/src/freeipa/translations.js
Normal 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;
|
||||
});
|
Loading…
Reference in New Issue
Block a user