page.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';//严格模式
  2. function googleTranslateElementInit(){
  3. new google.translate.TranslateElement({
  4. pageLanguage: 'zh-CN',//页面源语言,防止翻译非中文内容
  5. layout:google.translate.TranslateElement.InlineLayout.SIMPLE,
  6. autoDisplay:true,
  7. },'google_translate_element');
  8. }
  9. function googleTranslateCookie(a, b) {
  10. for (var c = window.location.hostname.split("."); 2 < c.length; )
  11. c.shift();
  12. c = ";domain=" + c.join(".");
  13. null != b ? a = a + "=" + b : (b = new Date,
  14. b.setTime(b.getTime() - 1),
  15. a = a + "=none;expires=" + b.toGMTString());
  16. a += ";path=/";
  17. document.cookie = a;
  18. try {
  19. document.cookie = a + c;
  20. } catch (d) {}
  21. }
  22. $(document).ready(function(){
  23. //添加元素
  24. $('body').append('<div id="google_translate_element" style="position:fixed;bottom:10px;right:10px;z-index:2000;opacity:0.4;cursor:move!important;"></div>');
  25. //设置cookie
  26. var hasCookie=false;
  27. if (document.cookie.length>0){
  28. if(document.cookie.indexOf("googtrans=")>-1){
  29. hasCookie=true;
  30. }
  31. }
  32. if(!hasCookie){
  33. googleTranslateCookie('googtrans',null);//先删除,防止保留了旧cookie
  34. googleTranslateCookie('googtrans','/zh-CN/'+localLanguage);//设置cookie实现自动翻译
  35. }
  36. $.getScript(window.resourcesUrl+'/js/element.js?cb=googleTranslateElementInit');
  37. if($('#google_translate_element').draggable){
  38. $('#google_translate_element').draggable();//可拖拽
  39. }
  40. if(window.site_config){
  41. //跳转设置页面
  42. $('#google_translate_element').on('click','.goog-te-gadget-icon',function(){
  43. window.location.href=ulink('admin/setting/site');
  44. });
  45. }
  46. });