script.js 1.3 KB

123456789101112131415161718192021222324252627282930
  1. // Manipulation d'attributs data
  2. // Cela pourrait être récupéré d'un fichier JSON externe.
  3. var translations = {
  4. "fr": {
  5. "the_title": "Questions essentielles",
  6. "the_subtitle": "Des questions que tout le monde se pose",
  7. "the_1st_paragraph": "Le 1er paragraphe explique des choses vitales à la compréhension de l'Univers.",
  8. "the_2nd_paragraph": "Le 2nd paragraphe explique plus modestement comment cuire des pois chiches."
  9. },
  10. "en": {
  11. "the_title": "Essential questions",
  12. "the_subtitle": "Questions everyone's asking oneself",
  13. "the_1st_paragraph": "1st paragraph explains things vital to the understanding of the Universe.",
  14. "the_2nd_paragraph": "2nd paragraphe more modestly explains how to cook chick peas."
  15. }
  16. };
  17. $('#lang-selector a').click(function(e) {
  18. e.preventDefault();
  19. // Supprimer le # du début. Notez qu'ici on utise .attr() car on ne veut
  20. // pas l'URL absolue qui aurait été calculée avec .prop().
  21. var lang = $(this).attr('href').substr(1);
  22. // Démontre une autre fonction: .each()
  23. $('[data-translate]').each(function(index, elem) {
  24. var jqElem = $(this);
  25. var translateKey = jqElem.data('translate');
  26. jqElem.html( translations[lang][translateKey] );
  27. });
  28. })