Integrate Google Translate with jquery

Source: Internet
Author: User

Easily integrate Google Translate into your web App with jquery.

1. [Code][javascript] Code
?
1<script src= "Scripts/translatorscript.js" type= "Text/javascript" ></script>
?
2 [Code]loadscript, setting the path
?
? 1LoadScript (ScriptPath + "jquery-blockui.js");
2LoadScript (ScriptPath + "jquery.translate-1.4.7.min.js");
3LoadScript (ScriptPath + "jquery.cookie.js");

? 3.Code [JavaScript] Code
01function Getscriptspath () {
Scripts var = document.getelementsbytagname ("script");
The var regex =/(. *\/) translatorscript/i;
for (var i = 0; i < scripts.length; i++) {
var currentscriptsrc = scripts[i].src;
if (Currentscriptsrc.match (regex))
Currentscriptsrc.match (regex) [1];
08}
09
return null;
11}
4.Code]loadtranslator

01function Loadtranslator () {
02
$.translate (function () {
04
try {
$ (' #translate-bar '). HTML ("");
07}
The catch (e) {
09}
10
One-to-one var selectedlanguage = $.cookie (' selectedlanguage '); Get previously translated language
12
if (selectedlanguage) {
if (selectedlanguage! = ' en ')
Translateto (Selectedlanguage);
16}
17
function Translateto (selectedlanguage) {
$ (' body '). Translate (' 中文版 ', Selectedlanguage, {
Not: '. Jq-translate-ui ',
Fromoriginal:true,
Start:function () {
$ (' #jq-translate-ui '). Val (selectedlanguage);
$.blockui.defaults.applyplatformopacityrules = false;
$.blockui (
26 {
Message: ' Language translation in Progress, please Wait ... ',
CSS:
29 {
border: ' None ',
padding: ' 10px ',
BackgroundColor: ' #000 ',
'-webkit-border-radius ': ' 9px ',
'-moz-border-radius ': ' 9px ',
Opacity:. 9,
Color: ' #fff '
37},
Overlaycss: {backgroundcolor: ' #000 ', opacity:0.6, '-moz-opacity ': ' 0.6 ', Width: ' 100% ', Height: ' 100% '}
39});
40},
Complete:function () {$.unblockui ({css: {cursor: ' Default '}});
42});
43}
+//Languages is loaded into the selection box
$.translate.ui ({CSS3 effects
Tags: ["select", "option"],
//a function that filters the languages:
-Filter: $.translate.istranslatable,//this can be an array of languages/language codes too
//a function that returns the text to display based on the language code:
50            Label: $.translate.tonativelanguage | |http://www.huiyi8.com/css3/?
51                function (langcode, lang) {return $.translate.capitalize (LAN g); },
52           //whether to include the UNKNOWN: ' Along with the languages:
53  ;           Includeunknown:false
54           }). Change (function () {
55            $.cookie (' Selectedlanguage ', $ (this). Val (), {path: '/'});
56            Translateto ($ (this). Val ());
57            return true;
58       }). AppendTo (' #translate-bar ');
59   });
60}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.