From 006c4db832d90804ca75fd14ba2e710da8ecedea Mon Sep 17 00:00:00 2001
From: Owen Leonard <oleonard@myacpl.org>
Date: Thu, 2 May 2019 16:26:55 +0000
Subject: [PATCH] Bug 22804: OPAC Overdrive JavaScript contains untranslatable
 strings

This patch moves untranslatable strings out of overdrive.js and into
opac-bottom.inc where other strings are defined for use in JavaScript
files.

To test you must have OverDrive integration set up, including
OverDriveCirculation. Test the various Overdrive integration features:

 - Placing holds
 - Cancelling holds
 - Checking out
 - Checking in

These features should work as expected.
---
 .../bootstrap/en/includes/opac-bottom.inc          | 11 ++++-
 koha-tmpl/opac-tmpl/bootstrap/js/overdrive.js      | 47 ++++++++++++----------
 2 files changed, 35 insertions(+), 23 deletions(-)

diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-bottom.inc b/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-bottom.inc
index f51f990..f9028d8 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-bottom.inc
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-bottom.inc
@@ -134,7 +134,7 @@ $.widget.bridge('uitooltip', $.ui.tooltip);
     var MSG_CHECKOUTS = _("Checkouts");
     var MSG_NO_CHECKOUTS = _("No checkouts");
     var MSG_CHECK_OUT = _("Check out");
-    var MSG_CHECK_OUT_CONFIRM = _("Are you sure you want to checkout this item?");
+    var MSG_CHECK_OUT_CONFIRM = _("Are you sure you want to check out this item?");
     var MSG_CHECKED_OUT_UNTIL = _("Checked out until %s");
     var MSG_CHECK_IN = _("Check in");
     var MSG_CHECK_IN_CONFIRM = _("Are you sure you want to return this item?");
@@ -176,6 +176,15 @@ $.widget.bridge('uitooltip', $.ui.tooltip);
         var MSG_MULTI_ADD_TAG_FAILED = _("Unable to add one or more tags.");
         var MSG_NO_TAG_SPECIFIED = _("No tag was specified.");
     [% END %]
+    [% IF ( Koha.Preference('OverDriveClientKey') && Koha.Preference('OverDriveClientSecret') ) %]
+        var MSG_OVERDRIVE_LOGIN = _("Log in to your OverDrive account");
+        var MSG_OVERDRIVE_LINK = _( "OverDrive account page" );
+        var MSG_OVERDRIVE_LOGOUT = _("Log out from your OverDrive account");
+        var MSG_OVERDRIVE_CHECKEDOUT_UNTIL = _( "Checked out until: " );
+        var MSG_OVERDRIVE_ACCESS_ONLINE = _("Access online");
+        var MSG_OVERDRIVE_DOWNLOAD_AS = _( "Download as: " );
+        var MSG_OVERDRIVE_CANNOT_CHECKOUT = _("Item cannot be checked out. There are no available formats");
+    [% END %]
     [% IF ( OPACAmazonCoverImages || SyndeticsCoverImages ) %]
         $(window).load(function() {
             verify_images();
diff --git a/koha-tmpl/opac-tmpl/bootstrap/js/overdrive.js b/koha-tmpl/opac-tmpl/bootstrap/js/overdrive.js
index 9070dc3..3722163 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/js/overdrive.js
+++ b/koha-tmpl/opac-tmpl/bootstrap/js/overdrive.js
@@ -1,3 +1,5 @@
+/* global MSG_OVERDRIVE_LOGIN MSG_OVERDRIVE_LINK MSG_OVERDRIVE_LOGOUT MSG_OVERDRIVE_CHECKEDOUT_UNTIL MSG_DOWNLOAD MSG_OVERDRIVE_ACCESS_ONLINE MSG_OVERDRIVE_DOWNLOAD_AS MSG_CHECK_IN MSG_CHECK_OUT MSG_CHECKOUTS MSG_HOLDS MSG_ON_HOLD MSG_PLACE_HOLD MSG_CANCEL_HOLD MSG_OVERDRIVE_CANNOT_CHECKOUT MSG_CANCEL_HOLD_CONFIRM MSG_CHECK_IN_CONFIRM MSG_CHECK_OUT_CONFIRM OD_password_required */
+
 if ( typeof KOHA == "undefined" || !KOHA ) {
     var KOHA = {};
 }
@@ -74,7 +76,7 @@ KOHA.OverDriveCirculation = new function() {
             if( OD_password_required ) { $("#overdrive-login").modal('show'); }
             else { login(""); }
         })
-        .text(_("Login to OverDrive account"));
+        .text( MSG_OVERDRIVE_LOGIN );
 
     var login_div = $('<div class="overdrive-login">').append(login_link);
 
@@ -87,6 +89,7 @@ KOHA.OverDriveCirculation = new function() {
     var checkout_popup = null;
     $( document ).ready(function() {
         var p = window.opener;
+        var cb;
         if (p) {
             try { cb = p.refresh_overdrive_account_details;}
             catch(err){ return; } //Catch error if opener is not accessible
@@ -113,7 +116,7 @@ KOHA.OverDriveCirculation = new function() {
         }
 
         var overdrive_link = $('<a href="https://www.overdrive.com/account/" target="overdrive-account" class="overdrive-link" style="float:right">')
-            .text("OverDrive Account Page");
+            .text( MSG_OVERDRIVE_LINK );
         $(container).append(overdrive_link);
 
         var logout_link = $('<a href="#logout" class="overdrive-logout" style="float:left">')
@@ -123,12 +126,12 @@ KOHA.OverDriveCirculation = new function() {
                 logout(function(data) {
                     display_account(container, data);
                 });
-            }).text(_("Logout from OverDrive account"));
+            }).text( MSG_OVERDRIVE_LOGOUT );
         $(container).append(logout_link);
         $(container).append('<br style="clear:both;"/>');
 
         if (data.checkouts) {
-            var checkouts_div = $('<div class="overdrive-div">').html('<h3>' + _("Checkouts") + '</h3>');
+            var checkouts_div = $('<div class="overdrive-div">').html('<h3>' + MSG_CHECKOUTS + '</h3>');
             var checkouts_list = $('<ul class="overdrive-list">');
             data.checkouts.items.forEach(function(item) {
                 item_line(checkouts_list, item);
@@ -138,7 +141,7 @@ KOHA.OverDriveCirculation = new function() {
         }
 
         if (data.holds) {
-            var holds_div = $('<div class="overdrive-div">').html('<h3>' + _("Holds") + '</h3>');
+            var holds_div = $('<div class="overdrive-div">').html('<h3>' + MSG_HOLDS + '</h3>');
             var holds_list = $('<ul class="overdrive-list">');
             data.holds.items.forEach(function(item) {
                 item_line(holds_list, item);
@@ -209,7 +212,7 @@ KOHA.OverDriveCirculation = new function() {
                 if( $("#overdrive-results-page").length > 0 ){
                     location.reload();
                 } else {
-                KOHA.OverDriveCirculation.display_account_details( $("#opac-user-overdrive") );
+                    KOHA.OverDriveCirculation.display_account_details( $("#opac-user-overdrive") );
                 }
             }
         });
@@ -271,13 +274,13 @@ KOHA.OverDriveCirculation = new function() {
             if (item) {
                 var expires = new Date(item.expires);
                 $('<span class="overdrive-item-status">')
-                    .text(_("Checked out until") + " " + expires.toLocaleString())
+                    .text( MSG_OVERDRIVE_CHECKEDOUT_UNTIL  + " " + expires.toLocaleString())
                     .appendTo(el);
                 $(el).append(" ");
 
                 if (item.format) {
                     var download = $('<a href="#">').appendTo(el);
-                    decorate_button(download, _("Download") + " " + item.format);
+                    decorate_button(download,  MSG_DOWNLOAD  + " " + item.format);
                     svc_ajax('get', {action: "download-url", id: id, format: item.format}, function(data) {
                         download.attr("href", data.action);
                     });
@@ -291,7 +294,7 @@ KOHA.OverDriveCirculation = new function() {
 
                         if (item.formats[f]) {
                             var access = $('<a target="_blank">').appendTo(el);
-                            decorate_button(access, _("Access online") + " " + f);
+                            decorate_button(access,  MSG_OVERDRIVE_ACCESS_ONLINE + " " + f);
                             svc_ajax('get', {action: "download-url", id: id, format: f}, function(data) {
                                 access.attr("href", data.action);
                             });
@@ -302,7 +305,7 @@ KOHA.OverDriveCirculation = new function() {
                         }
                     }
                     if (lockable_formats.length > 0 && checkout_popup) {
-                        $(el).append( ajax_button(_("Download as"), function() {
+                        $(el).append( ajax_button( MSG_OVERDRIVE_DOWNLOAD_AS, function() {
                             checkout_format(el, id, lockable_formats, copies_available);
                         }) ).append(" ");
                     }
@@ -310,8 +313,8 @@ KOHA.OverDriveCirculation = new function() {
 
                 if (item.format) return item;
 
-                $(el).append( ajax_button(_("Check in"), function() {
-                    if( confirm(_("Are you sure you want to return this item?")) ) {
+                $(el).append( ajax_button( MSG_CHECK_IN, function() {
+                    if( confirm( MSG_CHECK_IN_CONFIRM ) ) {
                         item_action({action: "return", id: id}, el, copies_available + 1);
                     }
                 }) );
@@ -322,14 +325,14 @@ KOHA.OverDriveCirculation = new function() {
             item = item_is_on_hold(id);
             if (item) {
                 $('<span class="overdrive-status">')
-                    .text(_("On hold"))
+                    .text( MSG_ON_HOLD )
                     .appendTo(el);
                 $(el).append(" ");
             }
 
             if(copies_available && checkout_popup) {
-                $(el).append( ajax_button(_("Check out"), function() {
-                    if( confirm(_("Are you sure you want to checkout this item?")) ) {
+                $(el).append( ajax_button( MSG_CHECK_OUT , function() {
+                    if( confirm( MSG_CHECK_OUT_CONFIRM ) ) {
                         svc_ajax('post', {action: "checkout", id: id}, function(data) {
                             if (data.checkouts) {
                                 details.checkouts = data.checkouts;
@@ -358,14 +361,14 @@ KOHA.OverDriveCirculation = new function() {
                 }) );
             }
             else if (!item) {
-                $(el).append( ajax_button(_("Place hold"), function() {
+                $(el).append( ajax_button( MSG_PLACE_HOLD, function() {
                     item_action({action: "place-hold", id: id}, el, copies_available);
                 }) );
             }
 
             if (item) {
-                $(el).append( ajax_button(_("Cancel"), function() {
-                    if( confirm(_("Are you sure you want to cancel this hold?")) ) {
+                $(el).append( ajax_button( MSG_CANCEL_HOLD, function() {
+                    if( confirm( MSG_CANCEL_HOLD_CONFIRM ) ) {
                         item_action({action: "remove-hold", id: id}, el, copies_available);
                     }
                 }) );
@@ -393,7 +396,7 @@ KOHA.OverDriveCirculation = new function() {
 
     function checkout_format(el, id, formats, copies_available) {
         if (formats.length == 0) {
-            alert(_("Item cannot be checked out - no available formats"));
+            alert( MSG_OVERDRIVE_CANNOT_CHECKOUT );
             return false;
         }
 
@@ -423,12 +426,12 @@ KOHA.OverDriveCirculation = new function() {
             }
             callback(data);
         });
-    }
+    };
 
     this.display_account_details = function( el ) {
         window.refresh_overdrive_account_details = function () {
             KOHA.OverDriveCirculation.display_account_details( el );
-        }
+        };
         $(el).empty().append(error_div);
         load_account_details(function(data) {
             display_account(el, data);
@@ -449,4 +452,4 @@ KOHA.OverDriveCirculation = new function() {
             .append(actions)
             .appendTo(el);
     };
-}
+};
-- 
2.1.4