1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Google translate code

Discussion in 'JavaScript' started by dramiditis, Jun 20, 2010.

  1. #1
    Hi,
    I'm using /www.google.com/jsapi/ for translating div id like this:

    google.load("language", "1");

    function initialize() {
    google.language.translate("Hello world", "en", "es", function(result) {
    if (!result.error) {
    var container = document.getElementById("translation");
    container.innerHTML = result.translation;
    }
    });
    }
    google.setOnLoadCallback(initialize);


    But, I would like to translate all div elements in page (all text on html page) with one function. Does anyone has idea how can I do that?
     
    Last edited: Jun 20, 2010
    dramiditis, Jun 20, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Try this:

    
    google.load("language", "1");
    
    function initialize() {
        all_div_elements = document.getElementsByTagName("div");
        for(var i=0; i<all_div_elements.length; i++){
            google.language.translate(all_div_elements[i].innerHTML, "en", "es", function(result) {
                if (!result.error) {
                    all_div_elements[i].innerHTML = result.translation;
                }
            });
        }
    }
    google.setOnLoadCallback(initialize);
    
    Code (markup):
     
    s_ruben, Jun 20, 2010 IP