Skip to content

Commit

Permalink
Added ability to test country codes and URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Cray committed May 11, 2011
1 parent 00a13a2 commit 979da87
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
11 changes: 11 additions & 0 deletions LICENSE
@@ -0,0 +1,11 @@
Copyright (C) 2010 Brian Cray

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
4 changes: 4 additions & 0 deletions README
@@ -0,0 +1,4 @@
Some things to know:
1. If you are testing offline, it won't work. But you can set variable test_url to test against a public URL
2. You can set variable test_cc to force a country code for testing
3. If you have portions of your text that shouldn't be translated (such as source code) add "notranslate" as a class name
10 changes: 10 additions & 0 deletions translate.html
Expand Up @@ -12,6 +12,16 @@
}
</style>

<script type="text/javascript">
// if you want to test the translator
// samples: fr (france), de (germany), cn (china), il (israel), mx (mexico)
// leave blank or remove for auto translation
var test_cc = 'fr';
// test on a public URL
// leave blank or remove for auto translation
var test_url = 'http://briancray.com';
</script>

<script type="text/javascript" src="translator.js"></script>

</head>
Expand Down
9 changes: 6 additions & 3 deletions translator.js
@@ -1,17 +1,20 @@
(function () {
var loaded = false;

test_cc = test_cc || false;
test_url = test_url || false;

var inject_jsapi = function () {
var h = document.getElementsByTagName('head')[0];
var s = document.createElement('script');
s.type = 'text/javascript';
s.onreadystatechange = function () {
if (this.readyState === 'loaded' || this.readyState === 'complete') {
translate(google.loader.ClientLocation && google.loader.ClientLocation.address.country_code, 'en');
translate(test_cc || (google.loader.ClientLocation && google.loader.ClientLocation.address.country_code), 'en');
}
};
s.onload = function () {
translate(google.loader.ClientLocation && google.loader.ClientLocation.address.country_code, 'en');
translate(test_cc || (google.loader.ClientLocation && google.loader.ClientLocation.address.country_code), 'en');
};
s.src = 'http://www.google.com/jsapi';
h.appendChild(s);
Expand Down Expand Up @@ -70,7 +73,7 @@
};
cc in cc2lang && (function (uri) {
document.location.href = 'http://translate.google.com/translate?langpair=' + lang + '|' + cc2lang[cc] + '&u=' + uri;
})(document.location.href);
})(test_url || document.location.href);
}
};

Expand Down

0 comments on commit 979da87

Please sign in to comment.