i18n_translator.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. var prefixLocaleStrings = "editThis_"
  2. function translate(messageID, args) {
  3. return chrome.i18n.getMessage(prefixLocaleStrings+messageID, args);
  4. }
  5. function localizePage() {
  6. //translate a page into the users language
  7. $("[i18n]:not(.i18n-replaced)").each(function() {
  8. //Append text to element content
  9. $(this).html($(this).html() + translate($(this).attr("i18n"), $(this).attr("i18n_argument")));
  10. $(this).addClass("i18n-replaced");
  11. });
  12. $("[i18n_value]:not(.i18n-replaced)").each(function() {
  13. $(this).val(translate($(this).attr("i18n_value")));
  14. $(this).addClass("i18n-replaced");
  15. });
  16. $("[i18n_title]:not(.i18n-replaced)").each(function() {
  17. $(this).attr("title", translate($(this).attr("i18n_title")));
  18. $(this).addClass("i18n-replaced");
  19. });
  20. $("[i18n_placeholder]:not(.i18n-replaced)").each(function() {
  21. $(this).attr("placeholder", translate($(this).attr("i18n_placeholder")) + $(this).attr("placeholder"));
  22. $(this).addClass("i18n-replaced");
  23. });
  24. $("[i18n_alt]:not(.i18n-replaced)").each(function() {
  25. $(this).attr("alt", translate($(this).attr("i18n_alt")));
  26. $(this).addClass("i18n-replaced");
  27. });
  28. $("[i18n_replacement_el]:not(.i18n-replaced)").each(function() {
  29. // Replace a dummy <a/> inside of localized text with a real element.
  30. // Give the real element the same text as the dummy link.
  31. var dummy_link = $("a", this);
  32. var text = dummy_link.text();
  33. var real_el = $("#" + $(this).attr("i18n_replacement_el"));
  34. real_el.text(text).val(text).replaceAll(dummy_link);
  35. // If localizePage is run again, don't let the [i18n] code above
  36. // clobber our work
  37. $(this).addClass("i18n-replaced");
  38. });
  39. }
  40. jQuery(document).ready(function(){
  41. localizePage();
  42. });