[Greasemonkey] extension localization
chris feldmann
cfeldmann at gmail.com
Tue Dec 20 00:29:58 EST 2005
In the process of working on a modeless alert system for install
success notification, I added localization sort of as an exercise to
familiarize myself with greasemonkey. After an inquiry on this
list[1], I cleaned it up and tested it and, well, am contributing it,
for what it's worth. I tried to follow naming conventions as I found
them for vars and ids, using my own entity and property conventions
from other extensions. Of course, that's all easy to tweak or change
outright, so let me know. Since gm still uses chrome/chromeFiles/, I
put locale/ under chromeFiles/ alongside content/.
As for the notification thingy, I found a neat way to do it, but ran
into cross-platform compatibility problems I'm still sorting out. I
still don't like clicking on that alert.
So ...anyone interested in a patch?
Chris
[1] http://www.mozdev.org/pipermail/greasemonkey/2005-December/007151.html
[2] the attachment is from running $diff -ur src local >> gm-localize.diff
where `local` is the modified src dir.
-------------- next part --------------
diff -ru src/chrome/chromeFiles/content/browser.js local/chrome/chromeFiles/content/browser.js
--- src/chrome/chromeFiles/content/browser.js 2005-12-18 23:14:11.000000000 -0500
+++ local/chrome/chromeFiles/content/browser.js 2005-12-19 23:52:51.725029496 -0500
@@ -48,7 +48,8 @@
this.statusPopup = document.getElementById("gm-status-popup");
this.statusEnabledItem = document.getElementById("gm-status-enabled-item");
this.toolsMenu = document.getElementById("menu_ToolsPopup");
-
+ this.gmBundle = document.getElementById("gm-menu-bundle");
+
// seamonkey compat
if (!this.toolsMenu) {
this.toolsMenu = document.getElementById("taskPopup");
@@ -61,7 +62,7 @@
// hook various events
GM_listen(this.appContent, "DOMContentLoaded", GM_hitch(this, "contentLoad"));
GM_listen(this.contextMenu, "popupshowing", GM_hitch(this, "contextMenuShowing"));
- GM_listen(this.toolsMenu, "popupshowing", GM_hitch(this, "toolsMenuShowing"));
+ GM_listen(this.toolsMenu, "popupshowing", GM_hitch(this, "toolsMenuShowing"));
// listen for clicks on the install bar
Components.classes["@mozilla.org/observer-service;1"]
@@ -154,6 +155,12 @@
GM_listen(unsafeWin, "pagehide", GM_hitch(this, "contentUnload"));
}
+ GM_BrowserUI.greetz = new Array;
+ for (i = 0; i < 6; i++){
+ GM_BrowserUI.greetz.push(this.gmBundle.getString('greetz.' + i));
+ }
+
+
if (GM_getEnabled() && href.match(/\.user\.js($|\?)/i)) {
// find the browser the user script is loading in
for (var i = 0, browser; browser = this.tabBrowser.browsers[i]; i++) {
@@ -164,9 +171,8 @@
this.tabBrowser.showMessage(
browser,
"chrome://greasemonkey/content/status_on.gif",
- greeting + " This is a Greasemonkey User Script. " +
- "Click Install to start using it.",
- "Install",
+ greeting + " " + this.gmBundle.getString('greeting.msg'),
+ this.gmBundle.getString('greeting.btn'),
null /* default doc shell */,
"install-userscript",
null /* no popuup */,
@@ -353,25 +359,25 @@
}
function GM_showPopup(aEvent) {
- var config = new Config(getScriptFile("config.xml"));
- config.load();
- var popup = aEvent.target;
- var url = getBrowser().contentWindow.document.location.href;
+ var config = new Config(getScriptFile("config.xml"));
+ config.load();
+ var popup = aEvent.target;
+ var url = getBrowser().contentWindow.document.location.href;
// set the enabled/disabled state
GM_BrowserUI.statusEnabledItem.setAttribute("checked", GM_getEnabled());
- // remove all the scripts from the list
+ // remove all the scripts from the list
for (var i = popup.childNodes.length - 1; i >= 0; i--) {
if (popup.childNodes[i].hasAttribute("value")) {
popup.removeChild(popup.childNodes[i]);
}
- }
+ }
var foundScript = false;
- // build the new list of scripts
- for (var i = 0, script = null; script = config.scripts[i]; i++) {
+ // build the new list of scripts
+ for (var i = 0, script = null; script = config.scripts[i]; i++) {
incloop: for (var j = 0; j < script.includes.length; j++) {
var pattern = convert2RegExp(script.includes[j]);
if (pattern.test(url)) {
@@ -395,18 +401,18 @@
break incloop;
}
}
- }
+ }
document.getElementById("gm-status-no-scripts").collapsed = foundInjectedScript;
}
function GM_popupClicked(aEvent) {
- var config = new Config(getScriptFile("config.xml"));
- config.load();
- var scriptNum=aEvent.target.value;
- if (!config.scripts[scriptNum]) return;
- config.scripts[scriptNum].enabled=!config.scripts[scriptNum].enabled;
- config.save();
+ var config = new Config(getScriptFile("config.xml"));
+ config.load();
+ var scriptNum=aEvent.target.value;
+ if (!config.scripts[scriptNum]) return;
+ config.scripts[scriptNum].enabled=!config.scripts[scriptNum].enabled;
+ config.save();
}
/**
@@ -415,12 +421,14 @@
* the mozilla preference that backs it directly.
*/
GM_BrowserUI.refreshStatus = function() {
- if (GM_getEnabled()) {
+
+
+ if (GM_getEnabled()) {
this.statusImage.src = "chrome://greasemonkey/content/status_on.gif";
- this.statusImage.tooltipText = "Greasemonkey is enabled";
+ this.statusImage.tooltipText = this.gmBundle.getString('tooltip.enabled');
} else {
this.statusImage.src = "chrome://greasemonkey/content/status_off.gif";
- this.statusImage.tooltipText = "Greasemonkey is disabled";
+ this.statusImage.tooltipText = this.gmBundle.getString('tooltip.disabled');
}
}
@@ -491,15 +499,6 @@
this.statusLabel.collapsed = true;
}
-GM_BrowserUI.greetz = [
- "Huzzah!",
- "Toodles...",
- "Howdy!",
- "Sup...",
- "Greetings, fellow traveler.",
- "G'Day!"
- ];
-
// necessary for webProgressListener implementation
GM_BrowserUI.onProgressChange = function(webProgress,b,c,d,e,f){}
GM_BrowserUI.onStateChange = function(a,b,c,d){}
diff -ru src/chrome/chromeFiles/content/browser.xul local/chrome/chromeFiles/content/browser.xul
--- src/chrome/chromeFiles/content/browser.xul 2005-12-18 23:14:11.000000000 -0500
+++ local/chrome/chromeFiles/content/browser.xul 2005-12-19 23:52:15.426547704 -0500
@@ -1,10 +1,9 @@
<?xml version="1.0"?>
-
+<!DOCTYPE overlay SYSTEM "chrome://greasemonkey/locale/greasemonkey.dtd">
<!-- Note: Contains Firefox-specific overlay -->
<overlay id="greasemonkey-browser-overlay" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
-
<script type="application/x-javascript" src="chrome://greasemonkey/content/utils.js" />
<script type="application/x-javascript" src="chrome://greasemonkey/content/prefmanager.js" />
<script type="application/x-javascript" src="chrome://greasemonkey/content/convert2RegExp.js" />
@@ -19,7 +18,7 @@
<script type="application/x-javascript" src="chrome://greasemonkey/content/accelimation.js" />
<popup id="contentAreaContextMenu">
- <menuitem id="install-userscript" label="Install User Script..." accesskey="S" insertbefore="context-openlink" oncommand="installContextItemClicked(event)"/>
+ <menuitem id="install-userscript" label="&popup.install;" accesskey="S" insertbefore="context-openlink" oncommand="installContextItemClicked(event)"/>
<menuseparator id="install-userscript-sep" insertbefore="context-openlink" />
</popup>
@@ -27,12 +26,12 @@
change for the Seamonkey copy below -->
<menupopup id="menu_ToolsPopup">
<menuseparator id="userscript-tools-sep" insertafter="menu_preferences" />
- <menuitem accesskey="U" label="Manage User Scripts..." oncommand="manageMenuItemClicked(event)"/>
- <menuitem id="userscript-tools-new" accesskey="N" label="New User Script" oncommand="GM_BrowserUI.newUserScript()"/>
- <menuitem id="userscript-tools-install" accesskey="S" label="Install This User Script..." oncommand="installMenuItemClicked(event)"/>
+ <menuitem accesskey="U" label="&menuitem.manage;" oncommand="manageMenuItemClicked(event)"/>
+ <menuitem id="userscript-tools-new" accesskey="N" label="&menuitem.new;" oncommand="GM_BrowserUI.newUserScript()"/>
+ <menuitem id="userscript-tools-install" accesskey="S" label="&menuitem.install;" oncommand="installMenuItemClicked(event)"/>
<menuseparator />
<menu id="userscript-commands-sb" accesskey="r"
- label="User Script Commands" disabled="false"
+ label="&menu.userCommands;" disabled="false"
onpopupshowing='event.preventBubble()'>
<menupopup/>
</menu>
@@ -40,15 +39,16 @@
<!-- Seamonkey ID is a bit different. Keep this the same as above -->
<menupopup id="taskPopup">
- <menuitem accesskey="U" label="Manage User Scripts..." oncommand="manageMenuItemClicked(event)"/>
+ <menuitem accesskey="U" label="&menuitem.manage;" oncommand="manageMenuItemClicked(event)"/>
<menuseparator id="userscript-tools-sep" insertafter="menu_preferences" />
- <menuitem id="userscript-tools-new" accesskey="N" label="New User Script" oncommand="GM_BrowserUI.newUserScript()"/>
- <menuitem id="userscript-tools-install" accesskey="S" label="Install This User Script..." oncommand="installMenuItemClicked(event)"/>
+ <menuitem id="userscript-tools-new" accesskey="N" label="&menuitem.new;" oncommand="GM_BrowserUI.newUserScript()"/>
+ <menuitem id="userscript-tools-install" accesskey="S" label="&menuitem.install;" oncommand="installMenuItemClicked(event)"/>
</menupopup>
<statusbar id="status-bar">
<statusbarpanel id="gm-status" insertafter="livemark-button">
- <label id="gm-status-label" collapsed="true" crop="end" style="width:0px" />
+ <stringbundle id="gm-menu-bundle" src="chrome://greasemonkey/locale/gm-alerts.properties"/>
+ <label id="gm-status-label" collapsed="true" crop="end" style="width:0px" />
<image id="gm-status-image" width="16" height="16" onclick="if (!event.button) GM_setEnabled(!GM_getEnabled())" context='gm-status-popup'/>
<popup id='gm-status-popup' position='before_end'
onpopupshowing='GM_showPopup(event); event.preventBubble();'
@@ -56,10 +56,10 @@
<menuitem accesskey="U" label="Manage User Scripts..."
oncommand="manageMenuItemClicked(event)"/>
<menuseparator/>
- <menuitem id="gm-status-no-scripts" label="No scripts installed!"
+ <menuitem id="gm-status-no-scripts" label="&menuitem.noScripts;"
style="color:grey"/>
<menuseparator id="gm-status-no-scripts-sep"/>
- <menuitem id="gm-status-enabled-item" accesskey="E" label="Enabled"
+ <menuitem id="gm-status-enabled-item" accesskey="E" label="&menuitem.enabled;"
type="checkbox" oncommand="GM_setEnabled(!GM_getEnabled())"/>
</popup>
</statusbarpanel>
diff -ru src/chrome/chromeFiles/content/manage.xul local/chrome/chromeFiles/content/manage.xul
--- src/chrome/chromeFiles/content/manage.xul 2005-12-18 23:14:11.000000000 -0500
+++ local/chrome/chromeFiles/content/manage.xul 2005-12-19 23:56:12.919443296 -0500
@@ -2,7 +2,7 @@
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<?xul-overlay href="chrome://greasemonkey/content/pages-overlay.xul"?>
-
+<!DOCTYPE dialog SYSTEM "chrome://greasemonkey/locale/greasemonkey.dtd">
<dialog
id="manage-window"
@@ -13,6 +13,8 @@
style="max-width:650px;"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
+ <stringbundle id="gm-windows-bundle" src="chrome://greasemonkey/locale/gm-alerts.properties"/>
+
<script type="application/x-javascript" src="chrome://greasemonkey/content/utils.js" />
<script type="application/x-javascript" src="chrome://greasemonkey/content/prefmanager.js" />
<script type="application/x-javascript" src="chrome://greasemonkey/content/config.js" />
@@ -175,15 +177,15 @@
<row>
<hbox>
<hbox>
- <checkbox id="chkEnabled" label="Enabled" checked="true" />
- <button flex="1" id="btnEdit" label="Edit" />
+ <checkbox id="chkEnabled" label="&manage.chkEnabled.label;" checked="true" />
+ <button flex="1" id="btnEdit" label="&manage.btnEdit.label;" />
</hbox>
</hbox>
<hbox>
<hbox>
- <button flex="1" id="btnUninstall" label="Uninstall" />
+ <button flex="1" id="btnUninstall" label="&manage.btnUninstall.label;" />
<checkbox id="chkUninstallPrefs"
- label="Also uninstall associated preferences" checked="false" />
+ label="&manage.chkUninstall.label;" checked="false" />
</hbox>
</hbox>
</row>
diff -ru src/chrome/chromeFiles/content/pages-overlay.xul local/chrome/chromeFiles/content/pages-overlay.xul
--- src/chrome/chromeFiles/content/pages-overlay.xul 2005-12-18 23:14:11.000000000 -0500
+++ local/chrome/chromeFiles/content/pages-overlay.xul 2005-12-19 23:55:58.070700648 -0500
@@ -1,14 +1,15 @@
<?xml version="1.0"?>
-
+<!DOCTYPE overlay SYSTEM "chrome://greasemonkey/locale/greasemonkey.dtd">
<overlay id="pages-overlay" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
-
- <script type="application/x-javascript">
- <![CDATA[
+
+ <script type="application/x-javascript">
+ <![CDATA[
function PagesControl(ctlPages) {
var includesBox = new PagesBox(document.getElementById("grpIncluded"));
var excludesBox = new PagesBox(document.getElementById("grpExcluded"));
+ var gmBundle = document.getElementById("gm-windows-bundle");
this.populate = function(script) {
includesBox.populate(script.includes);
@@ -61,7 +62,7 @@
}
function promptForNewPage(ev) {
- var val = gmPrompt("Enter a new URL below. You can specify multiple pages using the wildcard (*) character.", "http://foo.com/*", "Add Page");
+ var val = gmPrompt(gmBundle.getString("promptForNewPage.msg"), gmBundle.getString("promptForNewPage.defVal"));
if (val && val != "") {
addPage(val);
self.pages.push(val);
@@ -70,8 +71,8 @@
}
function promptForEdit(ev) {
- var val = gmPrompt("Modify the URL of the page below. You can specify multiple pages using the wildcard (*) character.",
- self.listbox.selectedItem.label, "Edit Page");
+ var val = gmPrompt(gmBundle.getString("promptForEdit.msg"),
+ self.listbox.selectedItem.label, gmBundle.getString("promptForEdit.title"));
if (val && val != "") {
self.listbox.selectedItem.label = val;
@@ -101,31 +102,29 @@
}
]]>
</script>
-
- <vbox id="pages-control">
- <groupbox id="grpIncluded" orient="vertical">
- <caption label="Included pages" />
- <hbox style="margin-bottom:.5em;">
- <listbox style="max-height: 8em; overflow: auto" flex="1" />
- <vbox>
- <button label="Add..." />
- <button label="Edit..." disabled="true" />
- <button label="Remove" disabled="true" />
- </vbox>
- </hbox>
- </groupbox>
-
- <groupbox id="grpExcluded" orient="vertical">
- <caption label="Excluded pages" />
- <hbox style="margin-bottom:.5em">
- <listbox style="max-height: 8em; overflow: auto" flex="1" />
- <vbox>
- <button label="Add..." />
- <button label="Edit..." disabled="true" />
- <button label="Remove" disabled="true" />
- </vbox>
- </hbox>
- </groupbox>
+ <vbox id="pages-control">
+ <groupbox id="grpIncluded" orient="vertical">
+ <caption label="&manage.grpIncluded.label;" />
+ <hbox style="margin-bottom:.5em;">
+ <listbox style="max-height: 8em; overflow: auto" flex="1" />
+ <vbox>
+ <button label="&manage.button.add;" />
+ <button label="&manage.button.edit;" disabled="true" />
+ <button label="&manage.button.remove;" disabled="true" />
+ </vbox>
+ </hbox>
+ </groupbox>
+
+ <groupbox id="grpExcluded" orient="vertical">
+ <caption label="&manage.grpExcluded.label;" />
+ <hbox style="margin-bottom:.5em">
+ <listbox style="max-height: 8em; overflow: auto" flex="1" />
+ <vbox>
+ <button label="&manage.button.add;" />
+ <button label="&manage.button.edit;" disabled="true" />
+ <button label="&manage.button.remove;" disabled="true" />
+ </vbox>
+ </hbox>
+ </groupbox>
</vbox>
-
</overlay>
diff -ru src/chrome/chromeFiles/content/scriptdownloader.js local/chrome/chromeFiles/content/scriptdownloader.js
--- src/chrome/chromeFiles/content/scriptdownloader.js 2005-12-18 23:14:11.000000000 -0500
+++ local/chrome/chromeFiles/content/scriptdownloader.js 2005-12-19 23:41:42.401782096 -0500
@@ -3,7 +3,7 @@
function ScriptDownloader() {}
ScriptDownloader.prototype.installFromURL = function(url) {
- GM_BrowserUI.showStatus("Downloading user script...");
+ GM_BrowserUI.showStatus(GM_BrowserUI.gmBundle.getString('alert.fromURI'));
this.xhr = new XMLHttpRequest();
this.url = url;
@@ -20,7 +20,7 @@
}
ScriptDownloader.prototype.installFromURLFailure = function(e) {
- alert("Could not download user script\n\n" + e.toString());
+ alert(GM_BrowserUI.gmBundle.getString('alert.fromURI.failure') + "\n\n" + e.toString());
GM_BrowserUI.hideStatus();
}
@@ -119,14 +119,14 @@
config.scripts.push(script);
config.save();
GM_BrowserUI.hideStatus();
- alert(script.filename + " installed successfully.");
+ alert(script.filename + " " + GM_BrowserUI.gmBundle.getString('alert.success'));
}
catch (e) {
config.scripts = oldScripts;
throw e;
}
} catch (e2) {
- alert("Error installing user script:\n\n" + (e2 ? e2 : ""));
+ alert(GM_BrowserUI.gmBundle.getString('alert.failure') + "\n\n" + (e2 ? e2 : ""));
GM_BrowserUI.hideStatus();
}
}
\ No newline at end of file
Only in local/chrome/chromeFiles: locale
diff -ru src/chrome.manifest local/chrome.manifest
--- src/chrome.manifest 2005-12-18 23:14:11.000000000 -0500
+++ local/chrome.manifest 2005-12-18 23:31:56.000000000 -0500
@@ -1,2 +1,3 @@
content greasemonkey chrome/chromeFiles/content/
overlay chrome://browser/content/browser.xul chrome://greasemonkey/content/browser.xul
+locale greasemonkey en-US chrome/chromeFiles/locale/en-US/
More information about the Greasemonkey
mailing list