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?
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 …
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.
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 🙂
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