Jump to content

Problem with escape characters


binarylime

Recommended Posts

Hi,

 

I am sending in an array of sentences to google language translation API. The sentences return and are displayed on screen.

 

Every now and then, & # 3 9 appears (without spaces). As far as I know this is the escape character for the apostrophe?

 

I've tried unescape(string); - but it doesn't appear to work.

 

The code is at http://development.asclarke.com/modliatranslator.html

 

Any idea?

 

Cheers

Link to comment
Share on other sites

No good unfortunately. It's just JavaScript - no PHP.

 

Heres the code:

 

 


  <script type="text/javascript">
    google.load("language", "1");
    window.onload = function()
    {
    	//Store original content to global variable
    	original = document.getElementById("translation").firstChild.nodeValue;
    }
    
    //Clear content from the existing element
    function clearElement(translation) 
    {
    	//While element has children, remove them
	while(translation.firstChild) translation.removeChild(translation.firstChild);
}

//Do the translation
    function initialize(value) 
    {
    	
    	//Get language to translate to
    	var translateto = value;
    	
    	//Declare array to store chunks of text
    	var myArray = new Array();
    	
    	//Split page text at every full stop into array elements
    	myArray = original.split('.');

    	//Call function to remove existing text from page
    	clearElement(document.getElementById("translation"));
    	    	
    	/*
	//Test function. Prints out elements of array before translation to verify split function

	for (var i=0; i<myArray.length; i++)
    	{
    		document.getElementById("translation").appendChild(document.createTextNode("["+i+"]"));
    		document.getElementById("translation").appendChild(document.createTextNode(myArray[i]));
    		document.getElementById("translation").appendChild(document.createTextNode(". "));
    	}
    	*/
    	
    	var i=0;
    	
    	//AJAX is asynchronous. Sentences sent in parallel for transaltion but may not return in 
    	//right order, therefore wrap incrementation of array counter in recursive function
    	
    	//Important. This varibale must be pre defined so that doTranslation can add to it
    	translated = "";
    
    	function doTranslation()
    	{ 
    		if(myArray[i])
    		{ 
    			google.language.translate(myArray[i], "en", translateto, function(result) 
    			{ 
    				if (!result.error) 
    				{
    					//concatinate translated text into string
    					translated = translated + result.translation + ". ";
        				//document.getElementById("translation").appendChild(document.createTextNode("["+i+"] "));
        				//document.getElementById("translation").appendChild(document.createTextNode(result.translation));
        				//document.getElementById("translation").appendChild(document.createTextNode(". "));
       					i++;
         				doTranslation();
        			}
        		}); 
        		
    		}
    		else
    		{
    			//When translation is complete, call display function
    			display();
    		}
    	}
    	
    	//Display "Please wait" message
    	document.getElementById("translation").appendChild(document.createTextNode("Please wait; translating text..."));
    	
    	//Call function to perform translation
    	doTranslation();
    	
    	//Display translated text function
    	function display()
    	{
    		translated.replace(translated, "&#39;","\x27")
    		document.getElementById("translation").appendChild(document.createTextNode(translated));
    	}
     }
    
    </script>


 

 

Hope that helps explain what i'm after.

 

The translation works and prints the sentences to the screen. But when translating to French for example, there are lots of apostrophise in French words. But for some reason, rather than putting in apostrophise, it shows the html escape character for apostrophe.

 

I've tried string.replace("&#39", "'") and unescape(string) but no luck...

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.