@bcworkz, Thanks.
I Indeed enqueued my file.js which was integrated in footer.php.
I checked with wp_script_is()
that is was correctly enqueued. It’s good.
Theme/functions.php
add_filter('locale', 'getnewlocale');
function getnewlocale($locale) {
$gen_locale = explode('/',$_SERVER["REQUEST_URI"])[1];
if ( !is_admin() ) {
$locale = ($gen_locale == 'fr') ? 'fr_FR' : (($gen_locale == 'en') ? 'en_GB' : '' );
return $locale;
}
}
add_action("after_setup_theme", function () {
load_theme_textdomain( 'ActivUs', get_stylesheet_directory() . '/languages' );
});
function actionjs_enqueue_scripts() {
wp_enqueue_script( 'main-action', get_template_directory_uri() . '/script/action.js', array('jquery', 'wp-i18n' ), '1.0.0', true);
}
add_action( 'wp_enqueue_scripts', 'actionjs_enqueue_scripts' );
function load_js_text_domain(){
wp_set_script_translations( 'main-action', 'Activus', get_stylesheet_directory().'/languages');
//var_dump(wp_script_is( 'main-action', 'enqueued' )); OK
}
add_action( 'wp_enqueue_scripts', 'load_js_text_domain', 100 );
But I’m always stuck because my internationalized string in my file.js are still not loaded.
The translation gettext __('string',text-domain');
is recognised correctly inside file.js (with id=”main-action”) and the default language (fr) is displayed correctly, but not for en_GB.
the strings of characters to be translated are inside the on(‘click’,…) functions in file.js.
The script to read internationalized string inside file.js
Theme/script/file.js :
var localeData = $('html').prop('lang');
localeData = localeData.replace('-', '_'); //return en_GB
const { __ } = wp.i18n;
const textdomain = 'text-domain';
__( '__', 'ActivUs' );
wp.i18n.setLocaleData(localeData,'text-domain');
Files in the “Theme/languages” folder :
textdomain-en_GB-main-action.json
en_GB.mo
en_GB.po
text-domain.pot
Example of Theme/languages/en_GB.po file:
#: script/file.js:969
#: script/file.js:2094
msgid "Revenir à la page précédente"
msgstr "Go back to previous page"
Thanks for your help