Support » Plugins » Hacks » translation in functions.php

  • Resolved Insomnia88

    (@insomnia88)


    Hi, I already asked this question in a plugin forum but it seems that the problem is more of wordpress nature.

    You can re-read it here: https://wordpress.org/support/topic/no-functionality-in-functionsphp?replies=3#post-6748729

    ——————————–

    I am trying to use _e() or __() for text translations inside the functions.php. For example for the wp_localize_script(). But I am not getting any translated strings. wp_localize_script() returns undefined in my linked script and custom functions return only the source text, not the translated one. When I hardcode strings there, they will be returned correctly.

    wp_enqueue_script( 'my-script', get_template_directory_uri() . '/js/script.js', array( 'jquery' ), null, true );
    wp_localize_script('my-script', 'translate', array(
                'not_required'  => __('not required', 'bla')
    ));

    What am I doing wrong?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Are you able to post the code you are using in order to retrieve the value of translate.not_required in your JavaScript file?

    Better yet, is there an error in the console, and if so what it is?

    Thread Starter Insomnia88

    (@insomnia88)

    There is no error. As I said, the function itself is working. Every other function in my functions.php as well. When I use a hardcoded string like ‘test’, it is correctly outputted but not when using __e() or __() functions.

    I get an ‘undefined’ variable when I log them via console because javascript (compared to php) doesn’t seem to output the source text instead.

    So to put it short. The translation function is not working at any place in the functions.php. In header, footer, templates they are but not in functions.php.

    The circumstance that the example code snippets at the codex are placed in functions.php tell me that this might be a bug but I kinda doubt that I found a bug here …

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    If you are getting an undefined variable it sounds like the JavaScript object isn’t being created. When wp_localize_script runs it creates a global JavaScript object. In the header it looks a little like:

    /* <![CDATA[ */
    var demoObj = {"auto":"","alert":"I am some text"};
    /* ]]> */

    The code I used was:

    // create the array to pass wp_localize_script
    $array = array();
    $array['auto'] = get_theme_mod( 'user-setting', false );
    $sarray['alert'] = __( 'I am some text', 'demo' );
    
    // finally create JS object
    wp_localize_script( 'myscript-js', 'demoObj', $array );

    In myscript.js I would use:

    alert( demoObj.alert );

    Which is why I was asking if you are able to post the JS code.

    Thread Starter Insomnia88

    (@insomnia88)

    Yeah, sure but due to the circumstances that

    1. no php echo is working with the translations in the functions.php and 2. the javascript is working when I place it in other *php files like header.php or templates

    are showing me that this has nothing to do with javascript. That’s what I am trying to tell you 😉

    I want it to work in the functions.php like it should. And not only the javascript. We can say “forget about the javascript part” because that’s the smallest problem 🙂

    Thread Starter Insomnia88

    (@insomnia88)

    UPDATE

    Ok, I solved the problem myself. This was no failure by wordpress. I switched databases in my custom functions and noticed that the switch back to wp database didn’t work. That was the cause for a lot of problems, including this one with the translations 🙂

    Still thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘translation in functions.php’ is closed to new replies.