From 6bcfc444bd93d99a1734ed8f4ce09da915abf957 Mon Sep 17 00:00:00 2001
From: Jerenus <jun.zheng@insready.com>
Date: Sat, 19 Jan 2013 14:20:56 +0800
Subject: [PATCH] Issue #369658 by Jerenus: JavaScript Support through
 Drupal.t().

---
 stringoverrides.js     |   20 ++++++++++++++++++++
 stringoverrides.module |   17 +++++++++++++++++
 2 files changed, 37 insertions(+)
 create mode 100644 stringoverrides.js

diff --git a/stringoverrides.js b/stringoverrides.js
new file mode 100644
index 0000000..5233d7b
--- /dev/null
+++ b/stringoverrides.js
@@ -0,0 +1,20 @@
+(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[''] || {};
+
+      // Add the string overrides translations to the Drupal.locale array.
+      jQuery.extend(Drupal.locale.strings, settings.stringoverrides);
+    }
+  }
+};
+
+})(jQuery);
diff --git a/stringoverrides.module b/stringoverrides.module
index d613150..3cc9e88 100644
--- a/stringoverrides.module
+++ b/stringoverrides.module
@@ -13,6 +13,7 @@ function stringoverrides_help($path, $arg) {
     case 'admin/help#stringoverrides':
       $output = '<p>' . t('The <strong>String Overrides</strong> module provides a quick and easy way of replacing text.') . '</p>';
       $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(
         'title' => t('Examples'),
         'items' => array(
@@ -98,3 +99,19 @@ function stringoverrides_theme() {
     ),
   );
 }
+
+/**
+ * Implements hook_js_alter().
+ */
+function stringoverrides_js_alter(&$javascript) {
+  global $language;
+  $translations = variable_get('locale_custom_strings_' . $language->language);
+  if (isset($translations['js'])) {
+    // Add stringoverrides.js to the page, as well as the overrides into the
+    // JavaScript settings.
+    $file = drupal_get_path('module', 'stringoverrides') . '/stringoverrides.js';
+    $javascript[$file] = drupal_js_defaults($file);
+    $javascript['settings']['data'][] = array('stringoverrides' => $translations['js']);
+  }
+}
+
-- 
1.7.10.4

