Fix JS translations loading method to make translations available earlier

This commit is contained in:
Benjamin Renard 2022-04-24 20:17:48 +02:00
parent 3a61299005
commit 0219714d4a
18 changed files with 44 additions and 53 deletions

View file

@ -270,28 +270,28 @@ function cli_compile_messages($command_args) {
logging('DEBUG', _("Lang alias symlink found: %s -> %s"), $lang, $real_lang_dir);
// Create JSON catalog symlink (if not exists)
$json_link = "$root_dir_path/public_html/translations/$lang.json";
$link_target = "$real_lang_dir.json";
if (!file_exists($json_link)) {
if (symlink($link_target, $json_link)) {
$js_link = "$root_dir_path/public_html/translations/$lang.js";
$link_target = "$real_lang_dir.js";
if (!file_exists($js_link)) {
if (symlink($link_target, $js_link)) {
logging('INFO', _("JSON catalog symlink for %s -> %s created (%s)"),
$lang, $real_lang_dir, $json_link);
$lang, $real_lang_dir, $js_link);
}
else {
logging('ERROR', _("Fail to create JSON catalog symlink for %s -> %s (%s)"),
$lang, $real_lang_dir, $json_link);
$lang, $real_lang_dir, $js_link);
$error = True;
}
}
elseif (readlink($json_link) == $link_target) {
elseif (readlink($js_link) == $link_target) {
logging('DEBUG', _("JSON catalog symlink for %s -> %s already exist (%s)"),
$lang, $real_lang_dir, $json_link);
$lang, $real_lang_dir, $js_link);
}
else {
logging(
'WARNING',
_("JSON catalog file for %s already exist, but it's not a symlink to %s (%s)"),
$lang, $real_lang_dir, $json_link
$lang, $real_lang_dir, $js_link
);
$error = True;
}
@ -334,19 +334,19 @@ function cli_compile_messages($command_args) {
// Compile messages from PO file to JSON catalog file
$json_catalog = po2json($lang, $po_file);
$json_file = "$root_dir_path/public_html/translations/$lang.json";
if(!$fd = fopen($json_file, 'w')) {
$js_file = "$root_dir_path/public_html/translations/$lang.js";
if(!$fd = fopen($js_file, 'w')) {
logging('ERROR', _("Fail to open %s JSON catalog file in write mode (%s)."),
$lang, $json_file);
$lang, $js_file);
$error = True;
}
elseif (fwrite($fd, $json_catalog) === false) {
elseif (fwrite($fd, sprintf("translations_data = %s;", $json_catalog)) === false) {
logging('ERROR', _("Fail to write %s JSON catalog in file (%s)."),
$lang, $json_file);
$lang, $js_file);
$error = True;
}
else {
logging('INFO', _("%s JSON catalog writed (%s)."), $lang, $json_file);
logging('INFO', _("%s JSON catalog writed (%s)."), $lang, $js_file);
}
}
closedir($dh);

View file

@ -140,10 +140,9 @@ function init_translation() {
logging('TRACE', "Test: "._('Hello world !'));
// JS translation file
$json_translation_file = "translations/$lang.json";
if (php_sapi_name() != "cli" && is_file("$root_dir_path/public_html/$json_translation_file")) {
add_js_file("lib/babel.js");
add_js_file("js/translation.js");
$js_translation_file = "translations/$lang.js";
if (php_sapi_name() != "cli" && is_file("$root_dir_path/public_html/$js_translation_file")) {
add_js_file(array("lib/babel.js", "js/translation.js", $js_translation_file));
$smarty->assign('lang', $lang);
}
}

View file

@ -1,7 +1,7 @@
msgid ""
msgstr ""
"POT-Creation-Date: 2022-04-24 19:09+0200\n"
"PO-Revision-Date: 2022-04-24 19:09+0200\n"
"POT-Creation-Date: 2022-04-24 20:13+0200\n"
"PO-Revision-Date: 2022-04-24 20:13+0200\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"

View file

@ -1,7 +1,7 @@
msgid ""
msgstr ""
"POT-Creation-Date: 2022-04-24 19:09+0200\n"
"PO-Revision-Date: 2022-04-24 19:09+0200\n"
"POT-Creation-Date: 2022-04-24 20:13+0200\n"
"PO-Revision-Date: 2022-04-24 20:13+0200\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

View file

@ -1,26 +1,23 @@
/*
* I18n
*/
var lang = null;
var translations;
var translations_data;
function _(string) {
var translated = translations.gettext(string);
var translated = (translations?translations.gettext(string):string);
return (translated !== '') ? translated : string;
}
function ngettext(singular, plural, n) {
return translations.ngettext(singular, plural, n);
return (
translations?
translations.ngettext(singular, plural, n):
(n > 1?plural:singular)
);
}
$(document).ready( function() {
// Load Gettext translations
var catalog = {};
$.getJSON('translations/'+lang+'.json', function(data) {
catalog = data;
}).always(function() {
translations = babel.Translations.load(catalog).install();
});
translations = babel.Translations.load(translations_data?translations_data:{}).install();
});

View file

@ -0,0 +1 @@
fr_FR.UTF8.js

View file

@ -1 +0,0 @@
fr_FR.UTF8.json

View file

@ -0,0 +1 @@
fr_FR.UTF8.js

View file

@ -1 +0,0 @@
fr_FR.UTF8.json

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1 @@
fr_FR.UTF8.js

View file

@ -1 +0,0 @@
fr_FR.UTF8.json

View file

@ -0,0 +1 @@
fr_FR.UTF8.js

View file

@ -1 +0,0 @@
fr_FR.UTF8.json

View file

@ -0,0 +1 @@
fr_FR.UTF8.js

View file

@ -1 +0,0 @@
fr_FR.UTF8.json

View file

@ -88,19 +88,14 @@
{/block}
<!-- Bootstrap -->
<!-- Jquery & Bootstrap -->
<script src="lib/jquery-3.4.1.min.js"></script>
<script src="lib/bootstrap4/js/bootstrap.bundle.min.js"></script>
<!-- Other libs & JavaScript scripts -->
{foreach $js as $file}
<script language="javascript" src="{$file}"></script>
{/foreach}
<!-- Global variables -->
<script>
var lang = "{$lang}";
</script>
</body>
</html>