LANGUAGE API - return ' where it should be '

7 views
Skip to first unread message

yml

unread,
Apr 16, 2008, 8:06:43 AM4/16/08
to Google AJAX API
Hello,

I am having some difficulty to understand why the API to google
Language returns ' instead of this when I translate the following
sentence from english to french :
"""
If this is checked, only logged-in users will be able to view the
page.
"""
The translation is :
"""
Si cette case est cochée, seuls utilisateurs enregistrés seront en
mesure d'afficher la page.
"""
Here it is the JS line to reproduce the issue :
"""
google.language.translate("If this is checked, only logged-in users
will be able to view the page.","en","fr", function(result)
{console.log(result.translation)})
"""

Thank you for your help
--yml
Message has been deleted

Ben Lisbakken

unread,
Apr 16, 2008, 2:53:49 PM4/16/08
to Google-AJAX...@googlegroups.com
Hey yml --

If you print out d'afficher in HTML it will actually be viewed by users as d'afficher.  The reason why we do this is because the quote can do things you don't want in your scripts, so instead of writing characters like this we write their HTML code equivalent.  Checkout this table to see more HTML codes (http://www.ascii.cl/htmlcodes.htm).

-Ben

yml

unread,
Apr 16, 2008, 6:43:42 PM4/16/08
to Google AJAX API
Ben,
This is true most of the time but I putting this string directly in a
textfield so ' is not transformed in '. Is there a way to fix this
on my end.

Thank you
--yml

On Apr 16, 8:53 pm, "Ben Lisbakken" <lisba...@google.com> wrote:
> Hey yml --
> If you print out d&#39;afficher in HTML it will actually be viewed by users
> as d'afficher. The reason why we do this is because the quote can do things
> you don't want in your scripts, so instead of writing characters like this
> we write their HTML code equivalent. Checkout this table to see more HTML
> codes (http://www.ascii.cl/htmlcodes.htm).
>
> -Ben
>

Ben Lisbakken

unread,
Apr 16, 2008, 7:03:44 PM4/16/08
to Google-AJAX...@googlegroups.com
I made a quick test (http://www.lisbakken.com/test.html).  Do you see two single quotes or one?

-Ben

yml

unread,
Apr 17, 2008, 7:35:11 AM4/17/08
to Google AJAX API
Hello Ben,
I see 2 quotes, and I understand that when a page is rendered &#39; is
transform into '. However this is not true if you use the language API
to populate a : <textarea rows="1" name="m_10"></textarea>

This is the function I use to populate the textarea :

""""
function rosetta_suggest(o) {
var i, orig = null, dest = null, tds =
o.parentNode.parentNode.getElementsByTagName('td');
for (var i=0; i < tds.length; i++) {
var td = tds[i];
if (td && 'original' == td.className) {
orig = td;
} else if (td && 'translation' == td.className) {
dest = td;
}
}
if (orig && dest) {
o.innerHTM='...';
console.log(orig.innerHTML);
google.language.translate(orig.innerHTML, "en", "fr",
function(result) {
if (!result.error) {
o.parentNode.removeChild(o);
console.log(result.translation);
dest.getElementsByTagName('textarea').item(0).value
= result.translation;
}
});
}
return false;
"""

Thank you for your help.

--yml


On Apr 17, 1:03 am, "Ben Lisbakken" <lisba...@google.com> wrote:
> I made a quick test (http://www.lisbakken.com/test.html). Do you see two
> single quotes or one?
> -Ben
>

jgeerdes [AJAX APIs "Guru"]

unread,
Apr 17, 2008, 7:41:57 AM4/17/08
to Google AJAX API
Try inserting this directly before you assign the textarea value:

result.translation.replace(/&#39;/,"'");

Jeremy R. Geerdes
Effective website design & development
Des Moines, IA

For more information or a project quote:
http://jgeerdes.home.mchsi.com
jgee...@mchsi.com

If you're in the Des Moines, IA, area, check out Debra Heights
Wesleyan Church!

yml

unread,
Apr 17, 2008, 12:40:11 PM4/17/08
to Google AJAX API
Thank you this did the trick here comes the next question does the
single quote (&#39;) is the only character that need to be replaced.
If no do you know where I can find the list ?

Thank you very much for your great help
--yml


On Apr 17, 1:41 pm, "jgeerdes [AJAX APIs \"Guru\"]"
<jgeer...@mchsi.com> wrote:
> Try inserting this directly before you assign the textarea value:
>
> result.translation.replace(/&#39;/,"'");
>
> Jeremy R. Geerdes
> Effective website design & development
> Des Moines, IA
>
> For more information or a project quote:http://jgeerdes.home.mchsi.com
> jgeer...@mchsi.com

jgeerdes [AJAX APIs "Guru"]

unread,
Apr 17, 2008, 1:06:03 PM4/17/08
to Google AJAX API
Don't know about a list, but there was a post the other day about
double quotes ( " ) being replaced with &quot;, for the same reason.
So you'll need to do something similar there.

Jeremy R. Geerdes
Effective website design & development
Des Moines, IA

For more information or a project quote:
http://jgeerdes.home.mchsi.com
jgee...@mchsi.com

Justin McConnell

unread,
Apr 18, 2008, 8:33:26 PM4/18/08
to Google AJAX API


On Apr 16, 3:43 pm, yml <yann.ma...@gmail.com> wrote:
> Ben,
> This is true most of the time but I putting this string directly in a
> textfield so &#39; is not transformed in '. Is there a way to fix this
> on my end.

Since &#39; and others you might be concerned with are numeric
character entities we can use the String.fromCharCode function to
convert them.

result.translation = result.translation.replace(/\&\#(.*?)\;/g,
function() {return (String.fromCharCode(arguments[1]))})

Justin McConnell

unread,
Apr 18, 2008, 8:37:41 PM4/18/08
to Google AJAX API
On Apr 16, 3:43 pm, yml <yann.ma...@gmail.com> wrote:
> Ben,
> This is true most of the time but I putting this string directly in a
> textfield so &#39; is not transformed in '. Is there a way to fix this
> on my end.

Not sure if the last message went through.

We can convert between numeric character references and actual
characters using String.fromCharCode. Just parse out the &# and the ;
and convert.

result.translation = result.translation.replace(/\&\#(.*?)\;/g,
function() {return (String.fromCharCode(arguments[1]))})

>
Reply all
Reply to author
Forward
0 new messages