2011-05-11 4 views
0

google.translate의 설정을 쿠키에 저장할 수 있습니까? 예를 들어 내 언어를 '영어'에서 '스페인어'로 설정하고 내 사이트의 다른 페이지 (예 : About)로 이동하면 언어가 '스페인어'로 유지됩니다. 이 방법을 구현하는 방법에 대한 도움이 필요합니다. 가능하다면 제대로 구현하는 방법을 모르겠습니다.Google에서 쿠키로 언어 설정 저장

Here (http://jsbin.com/esiga3) 현재 작업중인 코드 "이 언어에 설정된 쿠키가 있는지 확인하고 필요하지 않으면 생성합니다 쿠키는 언어를 설정합니다.

을 좀 자바 스크립트 나 쿠키를 사용하여 일시적으로 사용자 측에서 번역 API를 구글의 언어 설정을 설정하여 그 수를 생각한다.

당신을 감사합니다!

답변

1

을 몇 가지 개선 나쁜 번역이나 영어 문제를 피하기 위해 - http://jsfiddle.net/F248G/3/

0 도움을 주셔서 감사합니다 순전히 - 물론
// Set the original/default language 
var lang = "en"; 
var currentClass = "currentLang"; 

// Load the language lib 
google.load("language", 1); 

// When the DOM is ready.... 
window.addEvent("domready", function() { 
    // Retrieve the DIV to be translated. 
    var translateDiv = document.id("languageBlock"); 
    // Define a function to switch from the currentlanguage to another 
    var callback = function(result) { 
     if (result.translation) { 
      translateDiv.set("html", result.translation); 
     } 
    }; 

    // is language set? if so, auto translate 
    (function() { 
     // to avoid "lost in translation" on stacking up, i.e. 
     // translate from english to spanish, then from translated spanish back to english or others 
     // with errors, always use english as base language. 

     if (!translateDiv.retrieve("orig_en")) { 
      translateDiv.store("orig_en", translateDiv.get("html")); 
     } 

     // check cookie and if so, translate and set new base language 
     var toLang = Cookie.read("googleLang"); 
     if (toLang && toLang != lang) { 
      google.language.translate(translateDiv.retrieve("orig_en"), lang, toLang, callback); 
      lang = toLang; 
     } 
    })(); 

    // Add a click listener to update the DIV 
    $$("#languages a").addEvent("click", function(e) { 
     // Stop the event 
     if (e) e.stop(); 
     // Get the "to" language 
     var toLang = this.get("rel"); 

     if (toLang === lang) 
      return; 

     // Set the translation into motion 
     google.language.translate(translateDiv.get("html"), lang, toLang, callback); 
     // Set the new language 
     lang = toLang; 
     // Add class to current 
     this.getSiblings().removeClass(currentClass); 
     this.addClass(currentClass); 
     // ... and add here the code to save the last choice 
     Cookie.write("googleLang", toLang, { 
      path: "/" 
     }); 
    }); 
}); 

, 당신은 단지 http://mootools.net/docs/core/Utilities/Cookie

+0

헤이 디미타르 볼 수 있었다. – Pennf0lio

+0

나는 그것을 올바르게하고있다 (http://jsbin.com/ovage5)? – Pennf0lio

+0

번호. 업데이트 된 답변. 분명히 조각보다 더 많은 것을 필요로합니다 ... tbh 필자는이 코드 스 니펫을 직접 다 언어 작업의 기반으로 사용할 수 있다고 생각하기 때문에이 코드를 다시 작성합니다. –