Support from Acquia helps fund testing for Drupal Acquia logo

Comments

RobLoach’s picture

Title: Does this module handle Drupal.t() ? » JavaScript Support through Drupal.t()

No it doesn't, but that is definitely a good idea. Would it just send all the strings set in String Overrides to Drupal.locale.strings? How does Locale do it? What if there was an additional String Overrides tab for JavaScript strings?

Manuel Garcia’s picture

I opened up locale.module and found this function which looks like it's how its being done: locale_update_js_files().
I then ran a find in files for that function on the whole of drupal, and found _locale_invalidate_js() in locale.inc.

As far as how string overrides should step in I'm afraid it's passed my knowledge... perhaps we should harass someone in #drupal ?

I think having another tab for JavaScript strings would make sense, since this would probably work differently. Again, just guessing here...

Hope this helps -- thanks for caring!

Manuel Garcia’s picture

Category: support » feature
RobLoach’s picture

RobLoach’s picture

Status: Active » Needs work
jeffschuler’s picture

t() supports locale_custom_strings_en even without Locale enabled -- you can set $custom_strings in settings.php.

Shouldn't Drupal.t() access locale_custom_strings_en too?

AaronBauman’s picture

Status: Needs work » Active

Someone must have got this working by now, right?

chx’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
Status: Active » Needs review
FileSize
1.38 KB

Sue me for abusing the context.

Edit: most of this is, of course, copy-paste from locale.module.

RobLoach’s picture

Issue tags: +chxisawesome
FileSize
381 bytes
1.84 KB

Concerned about having a hash and file IO calculations every page load, what if we were to put the translations directly in Drupal.settings, and then had a stringoverrides.js load them into the Drupal translation system?

Forgive me for not having the .patch file include adding stringoverrides.js itself. I'm lazy.

RobLoach’s picture

Whoops, should probably be:

(function($) {

/**
 * String Overrides behavior to load in JavaScript-based translations.
 */
Drupal.behaviors.stringoverrides = {
  attach: function (context, settings) {
    if (settings.stringoverrides || false) {
      // Make sure the strings array is initialized.
      Drupal.locale.strings = Drupal.locale.strings || {};
      // Add the string overrides translations to the strings array.
      jQuery.extend(Drupal.locale.strings, settings.stringoverrides);
    }
  }
};

})(jQuery);

Needs an actual review though, I haven't tested this thoroughly.

RobLoach’s picture

Woot!!!

reszli’s picture

There is one small problem with the last patch,
in stringoverrides.js_.txt

// Add the string overrides translations to the Drupal.locale array.
jQuery.extend(Drupal.locale.strings[''], settings.stringoverrides);

should be:

// Add the string overrides translations to the Drupal.locale array.
jQuery.extend(Drupal.locale.strings, settings.stringoverrides);

This way it works for me.
Thanks!

Jerenus’s picture

RobLoach’s picture

Very nice, thanks a lot for the update! Could we get some additional manual testing on this to confirm that it's working? Thanks so much!

+++ b/stringoverrides.moduleundefined
@@ -13,6 +13,7 @@ function stringoverrides_help($path, $arg) {
       $output .= '<p>' . t('To replace a string, enter <strong>the complete string</strong> that is passed through the <a href="@t">t()</a> function. String Overrides cannot translate user-defined content, it can only replace strings wrapped in the t() function. To find the strings you can actually change, open up a module and look for t() function calls. Places where %, @, or ! are used means that the translation contains dynamic information (such as the node type or title in the above examples); these are not translated while the text around them is.', array('@t' => 'http://api.drupal.org/api/function/t')) . '</p>';
+      $output = '<p>' . t('Overrides with a context of "js" will be applied to JavaScript translations.') . '</p>';
       $output .= theme('item_list', array(

Small note before committing, the above should probably be $output .= '<p>' /* ... */;

Jerenus’s picture

RobLoach’s picture

Very nice, I wrapped the $language->language with an isset check, just to be safe. We'll need a manual terst to make sure this is good, then I think it'd be good to go. Thnaks!

thamas’s picture

Applied the patch from #369658-16: JavaScript Support through Drupal.t() to 7.x-1.8.

Tried to change the string Item !current of !total find in 'media_gallery/colorbox-display.js' to !current of !total but it didn't change anything. Tried it with empty context and context set to 'js'. Caches emptyed of course (also tested in other browser in private mode to avoid browser cache problems).

glynnr’s picture

Just a note to help those that need to get this working:

In the patch above, in this line : $javascript['settings']['data'][] = array('stringoverrides' => $translations['js']);
The stringoverrides with the context of 'js' are being appended.

Then this line : jQuery.extend(Drupal.locale.strings, settings.stringoverrides);
Merges them directly into Drupal.locale.strings

However Drupal.t() retrieves the strings like this : str = Drupal.locale.strings[options.context][str];
Using the key passed in at options.context.

Usually context is not passed in when using Drupal.t() and in these cases it defaults to ''.

So the quick solution here is to ensure that the stringoverrides are merged into the default, by changing
jQuery.extend(Drupal.locale.strings, settings.stringoverrides);
to
jQuery.extend(Drupal.locale.strings[''], settings.stringoverrides);

Then you should find that the stringoverrides identified by the context 'js' will start working.

Attached is the updated patch from #16

A.Kotov’s picture

Issue summary: View changes

Applied patch from #18 - see no result.

I have js file attached

$form['#attached']['js'][] = array(
   'type' => 'file',
   'data' => drupal_get_path('module','added1module') . '/js/added1Module.js',
  );

and use Drupal.t('bla-bla'); in it.

Should I do something special to make it working?

kevinquillen’s picture

The patch in #18 only works if you merge the strings into the js context, then specify the context as 'js' in the string override. And you have to add this line, adding the js context into the strings object.

(function($) {

/**
 * String Overrides behavior to load in JavaScript-based translations.
 */
Drupal.behaviors.stringoverrides = {
  attach: function (context, settings) {
    if (settings.stringoverrides || false) {
      // Make sure the JavaScript localization is available.
      Drupal.locale = Drupal.locale || {};
      Drupal.locale.strings = Drupal.locale.strings || {};
      Drupal.locale.strings[''] = Drupal.locale.strings[''] || {};
      Drupal.locale.strings['js'] = Drupal.locale.strings['js'] || {};

      // Add the string overrides translations to the Drupal.locale array.
      jQuery.extend(Drupal.locale.strings['js'], settings.stringoverrides);
    }
  }
};

})(jQuery);

AFTER this change, I was able to override Drupal.t() strings correctly with string overrides, PROVIDED that translated strings are passed in like so:

Drupal.t('string to translate', {}, {context: 'js'});
jrglasgow’s picture

Status: Needs review » Reviewed & tested by the community

patch in #18 is working great for me

The last submitted patch, 8: stringoverrides_js.patch, failed testing.

The last submitted patch, 9: stringoverrides_js_js.patch, failed testing.

The last submitted patch, 11: stringjs.patch, failed testing.

The last submitted patch, 16: 369658.patch, failed testing.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 18: 369658.patch, failed testing.

Pasqualle’s picture

#18 works for me

sukh.singh’s picture

#18 works for me partially, to make it working you will have to do 2 things, which are as follows.

1. Under admin/config/regional/stringoverrides, add js under the context column textfield besides the string which is to be changed.
2. When I added #18 patch, the stringoverrides.js file copied to the root of the drupal folder. To fix this I have added js folder and moved stringoverrides.js under it in stringoverrides module. I have created a refined patch #30 for the same.

Echofive’s picture

New patch to remove an "alert" call... "alert(90);"...