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

View file

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

View file

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

View file

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

View file

@ -1,26 +1,23 @@
/* /*
* I18n * I18n
*/ */
var lang = null;
var translations; var translations;
var translations_data;
function _(string) { function _(string) {
var translated = translations.gettext(string); var translated = (translations?translations.gettext(string):string);
return (translated !== '') ? translated : string; return (translated !== '') ? translated : string;
} }
function ngettext(singular, plural, n) { 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() { $(document).ready( function() {
// Load Gettext translations // Load Gettext translations
var catalog = {}; translations = babel.Translations.load(translations_data?translations_data:{}).install();
$.getJSON('translations/'+lang+'.json', function(data) {
catalog = data;
}).always(function() {
translations = babel.Translations.load(catalog).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} {/block}
<!-- Bootstrap --> <!-- Jquery & Bootstrap -->
<script src="lib/jquery-3.4.1.min.js"></script> <script src="lib/jquery-3.4.1.min.js"></script>
<script src="lib/bootstrap4/js/bootstrap.bundle.min.js"></script> <script src="lib/bootstrap4/js/bootstrap.bundle.min.js"></script>
{foreach $js as $file} <!-- Other libs & JavaScript scripts -->
{foreach $js as $file}
<script language="javascript" src="{$file}"></script> <script language="javascript" src="{$file}"></script>
{/foreach} {/foreach}
<!-- Global variables -->
<script>
var lang = "{$lang}";
</script>
</body> </body>
</html> </html>