views:

623

answers:

2

I'd like to have wordpress to do I18N for my javascript. My plan is to have javascript code in php file. For example, one sample.js.php file as below:

function foo()
{
   alert(<?php _e('do something'); ?>);
}

The sample.js.php file is referred as javascript.

<script type='text/javascript'>url-to-myplugin/sample.js.php</script>

However, it seems __() and _e() does't work as they are not defined. How to make the _e() and __() work in my case?

Thanks


I found the answer. Below code will do the work.

<?php
    define('WP_USE_THEMES', false);
    require('./wp-blog-header.php');
?>
A: 

Have you installed gettext extenssion?

Alexey
wordpress should have gettext extension.
Morgan Cheng
+1  A: 

Just include WP configuration file into sample.js.php script:

<?php require_once 'your-path-to/wp-config.php'; ?>

function foo()
{
   alert(<?php _e('do something'); ?>);
}
Oleksandr Bernatskyi
require wp-config.php make it display garbage character. I found the answer. wp-blog-header.php is good for use.
Morgan Cheng
Yep, wp-blog-header.php might also work. I'm usually loading wp-config.php, because some of my plug-in files require database connection. As to problem with wp-config.php, probably it has an ending space, an empty line, or any other character right after the closing php-tag. It can be in any other file WP includes during the load though. I'm usually deleting those symbols, and actually they're already removed in the latest version of WP (2.8.4 at the moment), so didn't see garbage characters during test.
Oleksandr Bernatskyi