Google Translate api JavaScript
Bonjour,
Je suis entrain de faire la traduction de mon site. Donc j'ai essayé de suivre un tutoriel qui montre comment créer une une API de google translate en JS. Alors j'ai créé mon API-key et j'ai ajouté dans mon code :
- Code en JS :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <script src="https://www.google.com/jsapi?key=API_key_ICI"></script>
<script>
// Set the original/default language
var lang = "fr";
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);
}
};
// 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");
// 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);
});
});
</script> |
- Code en HTML :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
//menu:
<div id="languages">
<p>
<a href="?lang=en" rel="en">English</a> / <a href="?lang=es" rel="es">Spanish</a> / <a href="?lang=it" rel="it">Italian</a> /
<a href="?lang=fr" rel="fr">French</a>
</p>
</div>
//le blocs qui sera traduit aprés
<div id="languageBlock">
{% block body %}.... {% endblock %}
</div> |
Le problème lorsque je choisis ma langue, par exemple "en", la page ne se traduit pas. Elle reste toujours en "fr".
Merci pour vos aides. :)