diff --git a/stringoverrides.module b/stringoverrides.module index d613150..dd141e0 100644 --- a/stringoverrides.module +++ b/stringoverrides.module @@ -98,3 +98,36 @@ function stringoverrides_theme() { ), ); } + +function stringoverrides_js_alter(&$javascript) { + global $language; + $translations = variable_get('locale_custom_strings_' . $language->language); + if (empty($translations['js'])) { + return; + } + $translations = $translations['js']; + + // Construct the JavaScript file, if there are translations. + $data = "Drupal.locale = { "; + $data .= "'strings': " . drupal_json_encode($translations) . " };"; + $data_hash = drupal_hash_base64($data); + // Construct the filepath where JS translation files are stored. + // There is (on purpose) no front end to edit that variable. + $dir = 'public://' . variable_get('locale_js_directory', 'languages'); + + // Only create a new file if the content has changed or the original file got + // lost. + $dest = $dir . '/' . $language->language . '_' . $data_hash . '.js'; + if (!file_exists($dest)) { + // Ensure that the directory exists and is writable, if possible. + file_prepare_directory($dir, FILE_CREATE_DIRECTORY); + // Save the file. + if (file_unmanaged_save_data($data, $dest)) { + } + else { + } + } + if (file_exists($dest)) { + $javascript[$dest] = drupal_js_defaults($dest); + } +}