看這里谷歌翻譯我得到以下代碼。
<meta name="google-translate-customization" content="9f841e7780177523-3214ceb76f765f38-gc38c6fe6f9d06436-c"></meta>
<div id="google_translate_element">
</div>
<script type="text/javascript">
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');
}
</script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
但是看這里html-5-tutorial,在右上方,我看到下面的代碼:
<div class="translate">
<div id="google_translate_element">
<div dir="ltr" class="skiptranslate goog-te-gadget">
<div id=":0.targetLanguage">
<select class="goog-te-combo">
<option value="">Select Language</option>
<option value="af">Afrikaans</option>
<option value="sq">Albanian</option>
<option value="ar">Arabic</option>
<option value="hy">Armenian</option>
<option value="az">Azerbaijani</option>
<option value="eu">Basque</option>
<option value="be">Belarusian</option>
<option value="bn">Bengali</option>
<option value="bg">Bulgarian</option>
<option value="ca">Catalan</option>
<option value="zh-CN">Chinese (Simplified)</option>
<option value="zh-TW">Chinese (Traditional)</option>
<option value="hr">Croatian</option>
<option value="cs">Czech</option>
<option value="da">Danish</option>
<option value="nl">Dutch</option>
<option value="eo">Esperanto</option>
<option value="et">Estonian</option>
<option value="tl">Filipino</option>
<option value="fi">Finnish</option>
<option value="fr">French</option>
<option value="gl">Galician</option>
<option value="ka">Georgian</option>
<option value="de">German</option>
<option value="el">Greek</option>
<option value="gu">Gujarati</option>
<option value="ht">Haitian Creole</option>
<option value="iw">Hebrew</option>
<option value="hi">Hindi</option>
<option value="hu">Hungarian</option>
<option value="is">Icelandic</option>
<option value="id">Indonesian</option>
<option value="ga">Irish</option>
<option value="it">Italian</option>
<option value="ja">Japanese</option>
<option value="kn">Kannada</option>
<option value="ko">Korean</option>
<option value="la">Latin</option>
<option value="lv">Latvian</option>
<option value="lt">Lithuanian</option>
<option value="mk">Macedonian</option>
<option value="ms">Malay</option>
<option value="mt">Maltese</option>
<option value="no">Norwegian</option>
<option value="fa">Persian</option>
<option value="pl">Polish</option>
<option value="pt">Portuguese</option>
<option value="ro">Romanian</option>
<option value="ru">Russian</option>
<option value="sr">Serbian</option>
<option value="sk">Slovak</option>
<option value="sl">Slovenian</option>
<option value="es">Spanish</option>
<option value="sw">Swahili</option>
<option value="sv">Swedish</option>
<option value="ta">Tamil</option>
<option value="te">Telugu</option>
<option value="th">Thai</option>
<option value="tr">Turkish</option>
<option value="uk">Ukrainian</option>
<option value="ur">Urdu</option>
<option value="vi">Vietnamese</option>
<option value="cy">Welsh</option>
<option value="yi">Yiddish</option>
</select>
</div>
Powered by
<span style="white-space: nowrap;">
<a class="goog-logo-link" href="http://translate.google.com" target="_blank">
<img style="padding-right: 3px;" src="http://www.google.com/images/logos/google_logo_41.png" width="37" height="13">
Translate
</a>
</span>
</div>
</div>
<script>
function googleTranslateElementInit() {
new google.translate.TranslateElement({
pageLanguage: 'en'
}, 'google_translate_element');
}
</script>
</div>
前者只能在實(shí)時網(wǎng)站上工作,不能在本地計(jì)算機(jī)上工作,因此我不能使用CSS來定位和設(shè)計(jì)它。它也不包含任何選擇和選項(xiàng)標(biāo)記。
后者包含選擇和選項(xiàng)標(biāo)記,但如果我復(fù)制并粘貼整個代碼,它在一個活動的網(wǎng)站上不起作用。
我想我的問題是如何使用后期標(biāo)記實(shí)現(xiàn)谷歌翻譯?
這里是應(yīng)該工作的標(biāo)記,本地和遠(yuǎn)程都是從https://www.w3schools.com/howto/howto_google_translate.asp:復(fù)制的
<div id="google_translate_element"></div>
<script>
function googleTranslateElementInit() {
new google.translate.TranslateElement(
{pageLanguage: 'en'},
'google_translate_element'
);
}
</script>
<script src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
為了讓谷歌翻譯對手機(jī)友好,去掉布局部分,布局:Google . translate . translate element . inline layout . simple
<div id="google_translate_element">
</div>
<script type="text/javascript">
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'en'}, 'google_translate_element');
}
</script>
<script type="text/javascript" src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
它在我的網(wǎng)站上工作,并且是移動友好的。https://living his word . org/articles/pages/whoiscernandisourworld . PHP
2022年,這個遺留代碼在mycase中工作,Github不需要任何Google API密鑰
我修改了這些家伙的代碼gtranslate.io。 一個例子,它如何在我的網(wǎng)站https://pogonyalo.com在所有現(xiàn)代瀏覽器的工作。
更新:如果你想選擇應(yīng)該翻譯什么- 只是添加class = & quotnotranslate & quot不應(yīng)該被翻譯的元素。
密碼
<div id="google_translate_element"></div>
<script type="text/javascript">
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'hi', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');
}
</script>
<script type="text/javascript" src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
實(shí)現(xiàn)Google translate html代碼非常容易。在你的項(xiàng)目上使用這段代碼,希望對你有所幫助。
<div id="google_translate_element"></div>
<script>
function googleTranslateElementInit() {
new google.translate.TranslateElement({
pageLanguage: 'en'
}, 'google_translate_element');
}
</script>
<script src="http://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
//首先用translate-google.js和下面給出的內(nèi)容創(chuàng)建一個新的js文件。 //你可以根據(jù)自己的需求更新css。
// translate-google.js
(function () {
var gtConstEvalStartTime = new Date();
function d(b) {
var a = document.getElementsByTagName("head")[0];
a || (a = document.body.parentNode.appendChild(document.createElement("head")));
a.appendChild(b)
}
function _loadJs(b) {
// console.log(b);
var a = document.createElement("script");
a.type = "text/javascript";
a.charset = "UTF-8";
a.src = b;
d(a)
}
function _loadCss(b) {
var a = document.createElement("link");
a.type = "text/css";
a.rel = "stylesheet";
a.charset = "UTF-8";
a.href = b;
d(a)
}
function _isNS(b) {
b = b.split(".");
for (var a = window, c = 0; c < b.length; ++c)
if (!(a = a[b[c]])) return !1;
return !0
}
function _setupNS(b) {
b = b.split(".");
for (var a = window, c = 0; c < b.length; ++c) a.hasOwnProperty ? a.hasOwnProperty(b[c]) ? a = a[b[c]] : a = a[b[c]] = {} : a = a[b[c]] || (a[b[c]] = {});
return a
}
window.addEventListener && "undefined" == typeof document.readyState && window.addEventListener("DOMContentLoaded", function () {
document.readyState = "complete"
}, !1);
if (_isNS('google.translate.Element')) {
return
}(function () {
var c = _setupNS('google.translate._const');
c._cest = gtConstEvalStartTime;
gtConstEvalStartTime = undefined;
c._cl = 'en';
c._cuc = 'googleTranslateElementInit2';
c._cac = '';
c._cam = '';
c._ctkk = eval('((function(){var a\x3d3002255536;var b\x3d-2533142796;return 425386+\x27.\x27+(a+b)})())');
var h = 'translate.googleapis.com';
var s = (true ? 'https' : window.location.protocol == 'https:' ? 'https' : 'http') + '://';
var b = s + h;
c._pah = h;
c._pas = s;
c._pbi = b + '/translate_static/img/te_bk.gif';
c._pci = b + '/translate_static/img/te_ctrl3.gif';
c._pli = b + '/translate_static/img/loading.gif';
c._plla = h + '/translate_a/l';
c._pmi = b + '/translate_static/img/mini_google.png';
c._ps = b + '/translate_static/css/translateelement.css';
c._puh = 'translate.google.com';
_loadCss(c._ps);
_loadJs(b + '/translate_static/js/element/main.js');
})();
})();
//然后將該文件包含在html文件中。
{{-- Google Language Translator START --}}
<style>
.google-translate {
display: inline-block;
vertical-align: top;
padding-top: 15px;
}
.goog-logo-link {
display: none !important;
}
.goog-te-gadget {
color: transparent !important;
}
#google_translate_element {
display: none;
}
.goog-te-banner-frame.skiptranslate {
display: none !important;
}
body {
top: 0px !important;
}
</style>
<script src="{{asset('js/translate-google.js')}}"></script>
<script type="text/javascript">
function googleTranslateElementInit2(){
new google.translate.TranslateElement({
pageLanguage:'en',
includedLanguages: 'en,es',
// https://ctrlq.org/code/19899-google-translate-languages
// includedLanguages: 'en,it,la,fr',
// layout: google.translate.TranslateElement.InlineLayout.SIMPLE,
autoDisplay:true
},'google_translate_element2');
var a = document.querySelector("#google_translate_element select");
// console.log(a);
if(a){
a.selectedIndex=1;
a.dispatchEvent(new Event('change'));
}
}
</script>
<ul class="navbar-nav my-lg-0 m-r-10">
<li>
<div class="google-translate">
<div id="google_translate_element2"></div>
</div>
</li>
{ {-Google Language Translator ENDS-} }
使用:
c._ctkk=eval('((function(){var a\x3d2143197373;var b\x3d-58933561;return 408631+\x27.\x27+(a+b)})())');
<script type="text/javascript">
(function(){
var d="text/javascript",e="text/css",f="stylesheet",g="script",h="link",k="head",l="complete",m="UTF-8",n=".";
function p(b){
var a=document.getElementsByTagName(k)[0];
a||(a=document.body.parentNode.appendChild(document.createElement(k)));
a.appendChild(b)}
function _loadJs(b){
var a=document.createElement(g);
a.type=d;
a.charset=m;
a.src=b;
p(a)}
function _loadCss(b){
var a=document.createElement(h);
a.type=e;
a.rel=f;
a.charset=m;
a.href=b;
p(a)}
function _isNS(b){
b=b.split(n);
for(var a=window,c=0;c<b.length;++c)
if(!(a=a[b[c]])) return ! 1;
return ! 0}
function _setupNS(b){
b=b.split(n);
for(var a=window,c=0;c<b.length;++c)
a.hasOwnProperty?a.hasOwnProperty(b[c])?a=a[b[c]]:a=a[b[c]]={}:a=a[b[c]]||(a[b[c]]={});
return a}
window.addEventListener&&"undefined"==typeof document.readyState&&window.addEventListener("DOMContentLoaded",function(){document.readyState=l},!1);
if (_isNS('google.translate.Element')){return}
(function(){
var c=_setupNS('google.translate._const');
c._cl='en';
c._cuc='googleTranslateElementInit1';
c._cac='';
c._cam='';
c._ctkk=eval('((function(){var a\x3d2143197373;var b\x3d-58933561;return 408631+\x27.\x27+(a+b)})())');
var h='translate.googleapis.com';
var s=(true?'https':window.location.protocol=='https:'?'https':'http')+'://';
var b=s+h;
c._pah=h;
c._pas=s;
c._pbi=b+'/translate_static/img/te_bk.gif';
c._pci=b+'/translate_static/img/te_ctrl3.gif';
c._pli=b+'/translate_static/img/loading.gif';
c._plla=h+'/translate_a/l';
c._pmi=b+'/translate_static/img/mini_google.png';
c._ps=b+'/translate_static/css/translateelement.css';
c._puh='translate.google.com';
_loadCss(c._ps);
_loadJs(b+'/translate_static/js/element/main.js');
})();
})();
</script>