google 翻译下拉列表 - 以该语言本身设置语言选项标签
google translate dropdown - set the language option label in that language itself
我使用了 google 翻译元素和代码:
<div id="google_translate_element"></div>
function googleTranslateElementInit() {
new google.translate.TranslateElement({
pageLanguage: 'en'
}, 'google_translate_element');
}
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit">
这为我提供了标准语言 select 或带有英文标签的下拉菜单。
如何自定义语言标签以使用该语言本身?
我想要的结果是:
<select>
<option selected="selected" value="">select</option>
<option value="af">Afrikaans</option>
<option value="sq">shqip</option>
<option dir="ltr" value="ar">العربية</option>
<option value="be">Беларуская</option>
<option value="bg">български</option>
<option value="ca">català</option>
<option value="zh-CN">中文(简体)</option>
<option value="zh-TW">中文(繁體)</option>
<option value="hr">Hrvatska</option>
<option value="cs">česky</option>
<option value="da">Dansk</option>
<option value="nl">Nederlands</option>
<option value="et">Eesti</option>
<option value="tl">Filipino</option>
<option value="fi">suomi</option>
<option value="fr">Français</option>
<option value="gl">Galego</option>
<option value="de">Deutsch</option>
<option value="el">Ελληνικά</option>
<option dir="ltr" value="iw">עברית</option>
<option value="hi">हिन्दी</option>
<option value="hu">magyar</option>
<option value="is">Íslenska</option>
<option value="id">Indonesia</option>
<option value="ga">Gaeilge</option>
<option value="it">Italiano</option>
<option value="ja">日本語</option>
<option value="ko">한국어</option>
....
</select>
更新
我想替换 google 生成的 select
的 innerHTML
var googleDropdown = $("#google_translate_element .goog-te-combo");
var customDropdown = $("#custom-translate");
googleDropdown.innerHTML = customDropdown.innerHTML;
自定义select:
<select id="custom-translate" style="display:none;">
<option value="">Select Language</option>
<option value="af">Afrikaans</option>
<option value="sq">Albanian</option>
<option value="am">Amharic</option>
<option value="ar">Arabic (العربية)</option>
...
</select>
当我在加载所有内容时在控制台上尝试它时这有效,但是当我在文件中添加此代码时,问题是 googleTranslateElementInit 最后执行。
那么如何让我的代码在 google 翻译完成后执行?
我自己为此苦苦挣扎了很长时间。我最终不得不学习一些 JQuery 来访问 iframe 并编辑下拉列表中的文本。一旦我能够访问文本元素,我就写了一个巨大的 switch 语句来将英语版本的文本更改为它的母语。我使用 google 翻译或应用程序脚本 LanguageApp 来翻译语言名称。
$("#google_translate_element").on("click", function () {
var languages = $("iframe").contents().find(".text").get();
console.log(languages);
for (i = 0; i < languages.length; ++i) {
var languageElem = languages[i];
var Lang = languageElem.textContent;
console.log(Lang);
var nativeLang = getNativeLang(Lang);
if (!nativeLang) {
nativeLang = Lang;
}
languageElem.textContent = nativeLang;
console.log(nativeLang);
}
});
function getNativeLang(Lang) {
switch (Lang) {
case "Spanish":
return "Español";
case "Haitian Creole":
return "Kreyòl Ayisyen";
case "Portuguese":
return "Português";
case "Chinese (Simplified)":
return "中文";
case "Chinese (Traditional)":
return "中文";
case "Afrikaans":
return "Afrikaans";
case "Albanian":
return "Shqiptar";
case "Amharic":
return "አማርኛ";
case "Arabic":
return "عربى";
case "Armenian":
return "Հայերեն";
case "Azerbaijani":
return "AzəRbaycan";
case "Basque":
return "Euskal";
case "Belarusian":
return "Беларус";
case "Bengali":
return "বাংলা";
case "Bosnian":
return "Bosanski";
case "Bulgarian":
return "Български";
case "Catalan":
return "Català";
case "Cebuano":
return "Cebuano";
case "Corsican":
return "Corsu";
case "Croatian":
return "Hrvatski";
case "Czech":
return "Čeština";
case "Danish":
return "Dansk";
case "Dutch":
return "Nederlands";
case "Esperanto":
return "Esperanto";
case "Estonian":
return "Eestlane";
case "Filipino":
return "Pilipino";
case "Finnish":
return "Suomalainen";
case "French":
return "Français";
case "Frisian":
return "Frysk";
case "Galician":
return "Galego";
case "Georgian":
return "ᲥᲐᲠᲗᲣᲚᲘ";
case "German":
return "Deutsche";
case "Greek":
return "Ελληνικά";
case "Gujarati":
return "ગુજરાતી";
case "Hausa":
return "Hausa";
case "Hawaiian":
return "ʻŌlelo Hawaiʻi";
case "Hebrew":
return "עברית";
case "Hindi":
return "हिन्दी";
case "Hmong":
return "Hmong";
case "Hungarian":
return "Magyar";
case "Icelandic":
return "Íslensku";
case "Igbo":
return "Ndi Igbo";
case "Indonesian":
return "Bahasa Indonesia";
case "Irish":
return "Gaeilge";
case "Italian":
return "Italiano";
case "Japanese":
return "日本語";
case "Javanese":
return "Basa Jawa";
case "Kannada":
return "ಕನ್ನಡ";
case "Kazakh":
return "Қазақ";
case "Khmer":
return "ខ្មែរ";
case "Korean":
return "한국어";
case "Kinyarwanda":
return "Ikinyarwanda";
case "Kurdish (Kurmanji)":
return "Kurdî";
case "Kyrgyz":
return "Кыргызча";
case "Lao":
return "ລາວ";
case "Latin":
return "Latine";
case "Latvian":
return "Latvietis";
case "Lithuanian":
return "Lietuvis";
case "Luxembourgish":
return "Lëtzebuergesch";
case "Macedonian":
return "Македонски";
case "Malagasy":
return "Malagasy";
case "Malay":
return "Bahasa Melayu";
case "Malayalam":
return "മലയാളം";
case "Maltese":
return "Malti";
case "Maori":
return "Maori";
case "Marathi":
return "मराठी";
case "Mongolian":
return "Монгол";
case "Myanmar (Burmese)":
return "မြန်မာ";
case "Nepali":
return "नेपाली";
case "Norwegian":
return "Norsk";
case "Nyanja":
return "Nyanja";
case "Odia (Oriya)":
return "ଓଡିଆ";
case "Pashto":
return "پښتو";
case "Persian":
return "فارسی";
case "Polish":
return "Polskie";
case "Punjabi":
return "ਪੰਜਾਬੀ";
case "Romanian":
return "Română";
case "Russian":
return "Русский";
case "Samoan":
return "Sāmoa";
case "Serbian":
return "Српски";
case "Sesotho":
return "Sesotho";
case "Shona":
return "Shona";
case "Scots Gaelic":
return "Gàidhlig";
case "Sindhi":
return "سنڌي";
case "Sinhala":
return "සිංහල";
case "Slovak":
return "Slovenský";
case "Slovenian":
return "Slovenščina";
case "Somali":
return "Soomaali";
case "Sundanese":
return "Urang Sunda";
case "Swahili":
return "Kiswahili";
case "Swedish":
return "Svenska";
case "Tagalog":
return "Tagalog";
case "Tajik":
return "Точик";
case "Tamil":
return "தமிழ்";
case "Telugu":
return "తెలుగు";
case "Thai":
return "ไทย";
case "Turkish":
return "Türk";
case "Turkmen":
return "türkmençe";
case "Ukrainian":
return "Українська";
case "Urdu":
return "اردو";
case "Uyghur":
return "ئۇيغۇر تىلى";
case "Uzbek":
return "O'Zbek";
case "Vietnamese":
return "TiếNg ViệT";
case "Welsh":
return "Cymraeg";
case "Xhosa":
return "Isixhosa";
case "Yiddish":
return "יידיש";
case "Yoruba":
return "Yoruba";
case "Zulu":
return "Isizulu";
}
}
@margaret 感谢您的回复。
但我最终使用了一个在元素加载后设置标签的函数。
调用下拉列表的 onclick 函数:
<div id="google_translate_element" onclick="foo();">
function foo(){
$("option[value='ar']").text("عربى (Arabic)");
$("option[value='fr']").text("Français (French)");
//.... so on
}
我使用了 google 翻译元素和代码:
<div id="google_translate_element"></div>
function googleTranslateElementInit() {
new google.translate.TranslateElement({
pageLanguage: 'en'
}, 'google_translate_element');
}
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit">
这为我提供了标准语言 select 或带有英文标签的下拉菜单。
如何自定义语言标签以使用该语言本身? 我想要的结果是:
<select>
<option selected="selected" value="">select</option>
<option value="af">Afrikaans</option>
<option value="sq">shqip</option>
<option dir="ltr" value="ar">العربية</option>
<option value="be">Беларуская</option>
<option value="bg">български</option>
<option value="ca">català</option>
<option value="zh-CN">中文(简体)</option>
<option value="zh-TW">中文(繁體)</option>
<option value="hr">Hrvatska</option>
<option value="cs">česky</option>
<option value="da">Dansk</option>
<option value="nl">Nederlands</option>
<option value="et">Eesti</option>
<option value="tl">Filipino</option>
<option value="fi">suomi</option>
<option value="fr">Français</option>
<option value="gl">Galego</option>
<option value="de">Deutsch</option>
<option value="el">Ελληνικά</option>
<option dir="ltr" value="iw">עברית</option>
<option value="hi">हिन्दी</option>
<option value="hu">magyar</option>
<option value="is">Íslenska</option>
<option value="id">Indonesia</option>
<option value="ga">Gaeilge</option>
<option value="it">Italiano</option>
<option value="ja">日本語</option>
<option value="ko">한국어</option>
....
</select>
更新 我想替换 google 生成的 select
的 innerHTMLvar googleDropdown = $("#google_translate_element .goog-te-combo");
var customDropdown = $("#custom-translate");
googleDropdown.innerHTML = customDropdown.innerHTML;
自定义select:
<select id="custom-translate" style="display:none;">
<option value="">Select Language</option>
<option value="af">Afrikaans</option>
<option value="sq">Albanian</option>
<option value="am">Amharic</option>
<option value="ar">Arabic (العربية)</option>
...
</select>
当我在加载所有内容时在控制台上尝试它时这有效,但是当我在文件中添加此代码时,问题是 googleTranslateElementInit 最后执行。
那么如何让我的代码在 google 翻译完成后执行?
我自己为此苦苦挣扎了很长时间。我最终不得不学习一些 JQuery 来访问 iframe 并编辑下拉列表中的文本。一旦我能够访问文本元素,我就写了一个巨大的 switch 语句来将英语版本的文本更改为它的母语。我使用 google 翻译或应用程序脚本 LanguageApp 来翻译语言名称。
$("#google_translate_element").on("click", function () {
var languages = $("iframe").contents().find(".text").get();
console.log(languages);
for (i = 0; i < languages.length; ++i) {
var languageElem = languages[i];
var Lang = languageElem.textContent;
console.log(Lang);
var nativeLang = getNativeLang(Lang);
if (!nativeLang) {
nativeLang = Lang;
}
languageElem.textContent = nativeLang;
console.log(nativeLang);
}
});
function getNativeLang(Lang) {
switch (Lang) {
case "Spanish":
return "Español";
case "Haitian Creole":
return "Kreyòl Ayisyen";
case "Portuguese":
return "Português";
case "Chinese (Simplified)":
return "中文";
case "Chinese (Traditional)":
return "中文";
case "Afrikaans":
return "Afrikaans";
case "Albanian":
return "Shqiptar";
case "Amharic":
return "አማርኛ";
case "Arabic":
return "عربى";
case "Armenian":
return "Հայերեն";
case "Azerbaijani":
return "AzəRbaycan";
case "Basque":
return "Euskal";
case "Belarusian":
return "Беларус";
case "Bengali":
return "বাংলা";
case "Bosnian":
return "Bosanski";
case "Bulgarian":
return "Български";
case "Catalan":
return "Català";
case "Cebuano":
return "Cebuano";
case "Corsican":
return "Corsu";
case "Croatian":
return "Hrvatski";
case "Czech":
return "Čeština";
case "Danish":
return "Dansk";
case "Dutch":
return "Nederlands";
case "Esperanto":
return "Esperanto";
case "Estonian":
return "Eestlane";
case "Filipino":
return "Pilipino";
case "Finnish":
return "Suomalainen";
case "French":
return "Français";
case "Frisian":
return "Frysk";
case "Galician":
return "Galego";
case "Georgian":
return "ᲥᲐᲠᲗᲣᲚᲘ";
case "German":
return "Deutsche";
case "Greek":
return "Ελληνικά";
case "Gujarati":
return "ગુજરાતી";
case "Hausa":
return "Hausa";
case "Hawaiian":
return "ʻŌlelo Hawaiʻi";
case "Hebrew":
return "עברית";
case "Hindi":
return "हिन्दी";
case "Hmong":
return "Hmong";
case "Hungarian":
return "Magyar";
case "Icelandic":
return "Íslensku";
case "Igbo":
return "Ndi Igbo";
case "Indonesian":
return "Bahasa Indonesia";
case "Irish":
return "Gaeilge";
case "Italian":
return "Italiano";
case "Japanese":
return "日本語";
case "Javanese":
return "Basa Jawa";
case "Kannada":
return "ಕನ್ನಡ";
case "Kazakh":
return "Қазақ";
case "Khmer":
return "ខ្មែរ";
case "Korean":
return "한국어";
case "Kinyarwanda":
return "Ikinyarwanda";
case "Kurdish (Kurmanji)":
return "Kurdî";
case "Kyrgyz":
return "Кыргызча";
case "Lao":
return "ລາວ";
case "Latin":
return "Latine";
case "Latvian":
return "Latvietis";
case "Lithuanian":
return "Lietuvis";
case "Luxembourgish":
return "Lëtzebuergesch";
case "Macedonian":
return "Македонски";
case "Malagasy":
return "Malagasy";
case "Malay":
return "Bahasa Melayu";
case "Malayalam":
return "മലയാളം";
case "Maltese":
return "Malti";
case "Maori":
return "Maori";
case "Marathi":
return "मराठी";
case "Mongolian":
return "Монгол";
case "Myanmar (Burmese)":
return "မြန်မာ";
case "Nepali":
return "नेपाली";
case "Norwegian":
return "Norsk";
case "Nyanja":
return "Nyanja";
case "Odia (Oriya)":
return "ଓଡିଆ";
case "Pashto":
return "پښتو";
case "Persian":
return "فارسی";
case "Polish":
return "Polskie";
case "Punjabi":
return "ਪੰਜਾਬੀ";
case "Romanian":
return "Română";
case "Russian":
return "Русский";
case "Samoan":
return "Sāmoa";
case "Serbian":
return "Српски";
case "Sesotho":
return "Sesotho";
case "Shona":
return "Shona";
case "Scots Gaelic":
return "Gàidhlig";
case "Sindhi":
return "سنڌي";
case "Sinhala":
return "සිංහල";
case "Slovak":
return "Slovenský";
case "Slovenian":
return "Slovenščina";
case "Somali":
return "Soomaali";
case "Sundanese":
return "Urang Sunda";
case "Swahili":
return "Kiswahili";
case "Swedish":
return "Svenska";
case "Tagalog":
return "Tagalog";
case "Tajik":
return "Точик";
case "Tamil":
return "தமிழ்";
case "Telugu":
return "తెలుగు";
case "Thai":
return "ไทย";
case "Turkish":
return "Türk";
case "Turkmen":
return "türkmençe";
case "Ukrainian":
return "Українська";
case "Urdu":
return "اردو";
case "Uyghur":
return "ئۇيغۇر تىلى";
case "Uzbek":
return "O'Zbek";
case "Vietnamese":
return "TiếNg ViệT";
case "Welsh":
return "Cymraeg";
case "Xhosa":
return "Isixhosa";
case "Yiddish":
return "יידיש";
case "Yoruba":
return "Yoruba";
case "Zulu":
return "Isizulu";
}
}
@margaret 感谢您的回复。
但我最终使用了一个在元素加载后设置标签的函数。 调用下拉列表的 onclick 函数:
<div id="google_translate_element" onclick="foo();">
function foo(){
$("option[value='ar']").text("عربى (Arabic)");
$("option[value='fr']").text("Français (French)");
//.... so on
}