Ticket #3911 (closed defect (bug): fixed)

Opened 2 years ago

Last modified 2 years ago

Javascript should not be localized by via PHP

Reported by: mdawaffe Assigned to: mdawaffe
Priority: high Milestone: 2.2
Component: Optimization Version: 2.1.2
Severity: normal Keywords: has-patch 2nd-opinion
Cc:

Description (Last modified by foolswisdom)

Example: list-manipulation-js.php has to load all of WordPress in order to localize just a two strings.

We can extend the script loader class to include a JS snippet that overwrites the default strings.

#BB605

Attachments

3911.diff (15.7 kB) - added by mdawaffe on 03/05/07 23:20:09.

Change History

03/05/07 23:20:09 changed by mdawaffe

  • attachment 3911.diff added.

03/05/07 23:22:11 changed by mdawaffe

  • keywords set to has-patch 2nd-opinion.
  • owner changed from anonymous to mdawaffe.
  • status changed from new to assigned.

3911.diff To apply

  1. svn mv wp-includes/js/list-manipulation-js.php wp-includes/js/list-manipulation.js

Features

  1. wp_localize_script( script_name, JS object_name, array( JS val => localized string ) See listman example in new script-loader.php

03/05/07 23:27:01 changed by ryan

Nice

03/05/07 23:35:28 changed by majelbstoat

+1 for this. At the moment I have to resort to defining variables in admin_head() and wp_head() which is ugly to say the least.

What object will the passed variables be attached to? (window, or a some other?) It seems to be magically attached to tempobj somehow...?

And in your patch, I didn't quite see what

 - if ( this.theList.childNodes.length % 2 )
 + if ( ( this.theList.childNodes.length + this.altOffset ) % 2 )

did, or whether it was part of the changes?

03/05/07 23:49:49 changed by mdawaffe

majelbstoat,

The variables get put into an object you specify. The patch on this ticket tells the script loader to add them to an object called "listManL10n":

wp_localize_script( 'listman', 'listManL10n', array( 
   'jumpText' => __('Jump to new item'), 
   'delText' => __('Are you sure you want to delete this %s?') 
) );

(in the patch the above is actually accomplished by a line that looks like $this->localize( ... );)

Script loader then generates the following output in the page's HEAD.

<script type='text/javascript'>
/* <![CDATA[ */
	listManL10n = {
		jumpText: "Jump to new item",
		delText: "Are you sure you want to delete this %s?"
	}
/* ]]> */
</script>

It's the responsibility of the JavaScript? to then go find it. listMan was tweaked to do just that with the following lines (included in the patch).

if ( 'undefined' != typeof listManL10n ) 
   Object.extend(listMan.prototype, listManL10n);

As to your other question:

Oops, forgot about the offset stuff. I think it fixes a coloring bug that sometimes shows up when AJAX adding new items to a list with alternating colors, but that's been sitting around locally on my machine for a while....

03/05/07 23:59:41 changed by majelbstoat

Sweet, I didn't spot the listManL10n object. That's a nice system and will make localisation a lot easier (kind of a special interest of mine).

03/06/07 00:34:38 changed by foolswisdom

  • description changed.

03/06/07 00:39:49 changed by ryan

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [4968]) JS localization from mdawaffe. fixes #3911