...

View Full Version : Modifying object{FieldName:value within a function?



Ace.....
11-24-2012, 03:11 PM
The language code below, belonging to the function further below, contains what I believe are object fieldname values: 'en' & 'fr'.

how can these values be modified - replaced by another code?

Replacing the values with a variable so far, has not worked.


new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'fr',layout: google.translate.TranslateElement.InlineLayout.HORIZONTAL, autoDisplay: false})


var thetext;
function googleTranslateElementInit(txt){
thetext=txt;
document.getElementById('thetext').innerHTML=txt;
new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'fr',layout: google.translate.TranslateElement.InlineLayout.HORIZONTAL, autoDisplay: false});
setTimeout(checkIt,2000)}
function checkIt(){var thediv=document.getElementById('thetext')
res=thediv.innerText?thediv.innerText:thediv.textContent;if(thetext!=res){parent.frames['theframe2'].googleTranslateElementInit(thediv.textContent);}
else{setTimeout(checkIt,2000)}}

felgall
11-24-2012, 08:52 PM
If you want to replace 'en' with a variable called lang then you'd replace the 'en' with function() {return lang;}

Ace.....
11-25-2012, 03:48 PM
I can't get this to work.
I've spent a good day scouring the web for any further knowledge, but as usual, all the examples seem to be using return for anything but my scenario.

Similarly with the value..... one or two people working with the fieldname, but not with the value.

So close to a workable sys, but always another bridge to cross. :rolleyes: :D

Can anybody else shed some light on this.

Ace.....
11-26-2012, 06:14 PM
If you want to replace 'en' with a variable called lang then you'd replace the 'en' with function() {return lang;}

Tests

var lang = "'de'"; (and subsequently)
var lang = 'de';

({pageLanguage: 'en', includedLanguages: function () {return lang;})

Console report:
Uncaught TypeError: Object function () {return lang;} has no method 'split'
.....................

Is it saying that the split method should have been used?
:confused:

Ace.....
11-26-2012, 10:55 PM
I incorporated the split method:


var str="'de'";
var lang=str.split();
alert(lang);

The alert returns 'de' as desired.

Console report:
Uncaught TypeError: Object function () {return lang;} has no method 'split'.

However, it's source ref is:

http://translate.googleapis.com/translate_static/js/element/11/element_main.js

quoting line 363 as the problem.
Certainly at the end of that line and beginning next, ref is made to includedLanguages.

I again tried entering the var lang, in place of 'fr', however, this produced no errors at all, and the doc translated perfectly in its default french. :confused:

So a simple variable doesn't break the script..... it is simply ignored.

Note: replacing 'fr' with de (no quotes).
Uncaught ReferenceError: de is not defined

Anybody care to comment...... does return still hold development promise, or should another avenue be followed.

Old Pedant
11-26-2012, 11:27 PM
Can you show your live page? Give us the URL to look at?

From everything I have read, you aren't using googleTranslateElementInit correctly. The usages I have seen do *NOT* allow you to pass in an argument (as you are doing with your txt argument.

Ace.....
11-27-2012, 02:27 AM
Re: The url, you can re-test with my pleasure & anticipation (http://www.max-haut-debit.fr/test_translator/translator_page.html)

Hopefully you can see thru testing, where I've gone wrong.

At the moment, I am dubious of your stance on the passing of the argument.

you aren't using googleTranslateElementInit correctly. The usages I have seen do *NOT* allow you to pass in an argument (as you are doing with your txt argument.
The reason being, that this element appears to work v.well.
xelawho was the force in opening up the complexities of googleTranslateElementInit
It was a major bridge crossing, but obviously everything is up for debate.

From what I know:
Given the physical transformation of the two digit code 'en', 'fr','de' etc.
The result is as expected.

I believe therefore, that the problem IS focussed upon THAT transformation (thru a select derived variable).

However, I'm well prepared to be told different. :)

Old Pedant
11-27-2012, 02:49 AM
Well, maybe it is just that you are using the *NAME* googleTranslateElementInit

As I said, all usages I saw that was the name of the callback function passed to

<script type="text/javascript"
src="//translate.google.com/translate_a/element.js?callback=FUNCTIONNAMEHERE"></script>

Since you apparently don't want the page to be translated when it first appears, clearly you don't want that callback function.

Anyway, this *SHOULD* be easy. You are working too hard.

You just need to PASS IN the languages to your function!

Back later.

Ace.....
12-08-2012, 04:17 PM
Yes you are of course right.

Though, I did figure out how to do it, only it was then, well, what now?
The system is unusable for publication to the web.

However, the coding & learning project itself was a complete success...... something that couldn't have been done using google api, without first having security in place (due to their billing).

Because of the financial risks involved, I believe the correct way forward is to now look at building such a secure web site.

I've spent some time reading a large number of docs, and it seems that a set of global publishing format protocols exists.

Effectively:

Write a doc that is vast & complex
Provide a long page of undocumented links to code libraries
Write a perfectly understandable doc, that provides insufficient information

The result being that I was unable to establish a direction, whereupon I could focus my research efforts.

I'm confident that I'm not alone in experiencing this 'inability to get to grips with this area of web development'.

As a consequence I'm going to start a thread that looks at the 'security path' options available to us. ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum