Show the tab count to close in the context menu - includes migrating strings to PluralForm-friendly ones. Increment to 1.05
authorJohnathan Nightingale <johnath@mozilla.com>
Wed, 17 Jun 2009 15:31:56 -0400
changeset 10 e62fb09006d8
parent 9 4d8c4ad7eb3b
child 11 18807249b971
push id11
push userjnightingale@mozilla.com
push dateMon, 31 Aug 2009 14:55:25 +0000
Show the tab count to close in the context menu - includes migrating strings to PluralForm-friendly ones. Increment to 1.05
content/overlay.js
install.rdf
locale/en-US/tabcloser.properties
--- a/content/overlay.js
+++ b/content/overlay.js
@@ -47,55 +47,47 @@ var tabcloser = {
     // Add the menu item
     var closeTabMenuitem = this.tabMenu.getElementsByAttribute("id",
                                                           "context_closeTab")[0];
     var newButton = document.createElement("menuitem");
     newButton.setAttribute("accesskey",
                            this.strings.getString("closeTabsForSite.accesskey"));
     newButton.setAttribute("oncommand", "tabcloser.onMenuItemCommand(event);");
     this.contextMenuItem = this.tabMenu.insertBefore(newButton, closeTabMenuitem);
+    
+    Components.utils.import("resource://gre/modules/PluralForm.jsm");
   },
 
   onMenuShowing: function (e) {
     try {
       // Might throw if current tab has no host...
       var host = gBrowser.mContextTab.linkedBrowser.currentURI.host;
     } catch (ex) {
       // ... so just hide the menu item
       this.contextMenuItem.hidden = true;
       return;
     }
 
     this.contextMenuItem.hidden = false;
+    var tabCount = this.getTabsToClose(host).length;
     var label = this.strings.getFormattedString("closeTabsForSite.label",
                                                 [host]);
+    label = PluralForm.get(tabCount, label).replace("#1", tabCount);
     this.contextMenuItem.setAttribute("label", label);
   },
 
   onMenuItemCommand: function(e) {
     var host = gBrowser.mContextTab.linkedBrowser.currentURI.host;
-
-    var tabsToClose = [];
-    var tabCount = gBrowser.mTabs.length;
-    for (var i = tabCount - 1; i >= 0; i--) {
-      var tab = gBrowser.mTabs[i];
-      var browser = gBrowser.getBrowserForTab(tab);
-      try {
-        var tabHost = browser.currentURI.host;
-      } catch (ex) {
-        // Ignore failure to get .host
-        continue;
-      }
-      if (host == tabHost) {
-        tabsToClose.push(tab);
-      }
-    }
+    var tabsToClose = this.getTabsToClose(host);
 
     var message = this.strings.getFormattedString("areYouSure.message",
-                                                  [tabsToClose.length, host]);
+                                                  [host]);
+    message = PluralForm.get(tabsToClose.length, message)
+                        .replace("#1", tabsToClose.length);
+    
     var title = this.strings.getString("areYouSure");
     var promptService = Cc["@mozilla.org/embedcomp/prompt-service;1"].
                         getService(Ci.nsIPromptService);
     
     // don't prompt if there's only one tab
     if (tabsToClose.length == 1 || promptService.confirm(window, title, message)) {
       tabsToClose.forEach(function (t) {
         // work around some strange bug in 3.0.x that causes removeTab to fail
@@ -104,10 +96,30 @@ var tabcloser = {
         if (gBrowser.mTabs.length == 1)
           closeWindow(true);
         else
           gBrowser.removeTab(t);
       });
     }
   },
 
+  getTabsToClose : function(host) {
+    var tabsToClose = [];
+    var tabCount = gBrowser.mTabs.length;
+    for (var i = tabCount - 1; i >= 0; i--) {
+      var tab = gBrowser.mTabs[i];
+      var browser = gBrowser.getBrowserForTab(tab);
+      try {
+        var tabHost = browser.currentURI.host;
+      } catch (ex) {
+        // Ignore failure to get .host
+        continue;
+      }
+      if (host == tabHost) {
+        tabsToClose.push(tab);
+      }
+    }
+    
+    return tabsToClose;
+  }
+
 };
 window.addEventListener("load", function(e) { tabcloser.onLoad(e); }, false);
--- a/install.rdf
+++ b/install.rdf
@@ -1,15 +1,15 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
  xmlns:em="http://www.mozilla.org/2004/em-rdf#">
   <Description about="urn:mozilla:install-manifest">
     <em:id>tabcloser@gavinsharp.com</em:id>
     <em:name>TabCloser</em:name>
-    <em:version>1.04</em:version>
+    <em:version>1.05</em:version>
     <em:creator>Gavin Sharp</em:creator>
     <em:description>Close all tabs for a given site.</em:description>
     <em:targetApplication>
       <Description>
         <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id> <!-- firefox -->
         <em:minVersion>3.0</em:minVersion>
         <em:maxVersion>3.6a1pre</em:maxVersion>
       </Description>
--- a/locale/en-US/tabcloser.properties
+++ b/locale/en-US/tabcloser.properties
@@ -1,5 +1,10 @@
-closeTabsForSite.label=Close all %S tabs
+# LOCALIZATION NOTE (closeTabsForSite.label, areYouSure.message):
+# Semi-colon list of plural forms.
+# See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
+# #1 number of tabs
+# example: Close all 4 tabs from mxr.mozilla.org
+closeTabsForSite.label=Close #1 tab from %1$S;Close all #1 tabs from %1$S
 closeTabsForSite.accesskey=l
 areYouSure=Are you sure?
-areYouSure.message=Are you sure you want to close all %S tabs from %S?
+areYouSure.message=Are you sure you want to close the tab from %1$S?;Are you sure you want to close all #1 tabs from %1$S?
 extensions.tabcloser.description=Close all tabs for a given site.