Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

wp_localize_script() not i18n compatible on frontend? WordPress

  • SOLVED

I use wp_localize_script() to localize the strings "Zoom in" and "Zoom out" on maps created with my plugin mapsmarker.com. I use the following code

...
$lmm_options = get_option( 'leafletmapsmarker_options' );
add_action('init', array(&$this, 'lmm_load_translation_files'),1);
add_action('wp_enqueue_scripts', array(&$this, 'lmm_frontend_enqueue_scripts'),2);
...
function lmm_load_translation_files() {
load_plugin_textdomain('lmm', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/');
}
...
function lmm_frontend_enqueue_scripts() {
$lmm_options = get_option( 'leafletmapsmarker_options' );
$plugin_version = get_option('leafletmapsmarker_version');
if ( is_admin() ) { $gmaps_libraries = '&libraries=places'; } else { $gmaps_libraries = ''; }
if ( defined('WPLANG') ) { $lang = substr(WPLANG, 0, 2); } else { $lang = 'en'; }
wp_enqueue_script( array ( 'jquery' ) );
//info: google maps
if ( isset($lmm_options['google_maps_api_key']) && ($lmm_options['google_maps_api_key'] != NULL) ) { $google_maps_api_key = $lmm_options['google_maps_api_key']; } else { $google_maps_api_key = ''; }
wp_enqueue_script( 'leafletmapsmarker-googlemaps-loader', 'http://www.google.com/jsapi?key='.$google_maps_api_key, array(), NULL);
//info: load leaflet.js + plugins
wp_register_script( 'leafletmapsmarker', LEAFLET_PLUGIN_URL . 'leaflet-dist/leaflet.js', array('leafletmapsmarker-googlemaps-loader'), $plugin_version);
wp_enqueue_script( 'leafletmapsmarker' );
wp_localize_script('leafletmapsmarker', 'leafletmapsmarker_L10n', array(
'lmm_zoom_in' => __( 'Zoom in', 'lmm' ),
'lmm_zoom_out' => __( 'Zoom out', 'lmm' ),
'lmm_googlemaps_language' => $lang,
'lmm_googlemaps_libraries' => $gmaps_libraries
) );
}
...


As you can see from above, I load the function lmm_load_translation_files() with priority 1 first, and only afterwards the function lmm_frontend_enqueue_scripts(). To my understanding, this should result in plugins-language-files being loaded first and changing the output to

<script type='text/javascript'>
/* <![CDATA[ */
var leafletmapsmarker_L10n = {"lmm_zoom_in":"Vergroessern","lmm_zoom_out":"Verkleinern","lmm_googlemaps_language":"en","lmm_googlemaps_libraries":""};
/* ]]> */
</script>


in German for example.

But I am wrong :-/ The output is always

<script type='text/javascript'>
/* <![CDATA[ */
var leafletmapsmarker_L10n = {"lmm_zoom_in":"Zoom in","lmm_zoom_out":"Zoom out","lmm_googlemaps_language":"en","lmm_googlemaps_libraries":""};
/* ]]> */
</script>


no matter which language is select in wp-config.php.

Does anyone know what I am missing here? Isnt load priority of the functions (1+2) enough for plugins internationalization to load before script enqueues?

Answers (3)

2012-08-04

Asad Iqbal answers:

Did you follow this steps yet rather than default localization process of wordpress itself?

http://www.mapsmarker.com/languages/

Kindly let me know.


Robert Seyfriedsberger comments:

ups - thanks for the hint, I think I new my plugin better ;-) Forgot to change the option "Where to change the default language" - now it works, thanks!

2012-08-05

Plugarized answers:

Try adding the action after the function such as;


function lmm_load_translation_files() {

load_plugin_textdomain('lmm', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/');

}

add_action('init', array(&$this, 'lmm_load_translation_files'),1);


And same for the other function, the german language must be defined as
define('WPLANG', ''de_DE); and create a folder called "languages" in the root folder also upload the german mo and po files

2012-08-05

Daryl Lozupone answers:

Robert--

In your javascript, you need to reference the the object you pass to the wp_localize_script action. I am not familiar with the Google maps JS, but hopefully this example will help:

jQuery("#google-maps").zoomintext = leafletmapmarker_L10n.lmm_zoomin;

etc...

If you need more assistance, I would be more than happy to work with you on your site to localize your JS.

--Daryl