Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter soimagine

    (@soimagine)

    I resolved my issue.
    the problem come from the language use by the json file instantiation. The language code used is only [ISO 639] and not both of them [ISO 639]_[ISO 3166].

    That means that you can’t distinguish between ‘en_US’ and ‘en_GB’. So it makes no sense to name .po and .json files with the locale (en_GB) if json use only ISO 639 to retrieve the language used.

    Also I didn’t find a Cli Command to update the .json file .
    The documentation define “make-json” only to create.
    If I do a “make-json on exiting .json file I’m losing all the translations.

    So script must be like that :

    "locale_data": {
            "messages": {
                "": {
                    "domain": "your-text-domain",
                    "lang": "en", // And not "en_GB", only ISO 639
                    "plural-forms": "nplurals=2; plural=(n != 1);"
                },
    Thread Starter soimagine

    (@soimagine)

    @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

Viewing 2 replies - 1 through 2 (of 2 total)