Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Nov 2012
    Location
    France
    Posts
    78
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Modifying object{FieldName:value within a function?

    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.

    Code:
    new google.translate.TranslateElement({pageLanguage: 'en', includedLanguages: 'fr',layout: google.translate.TranslateElement.InlineLayout.HORIZONTAL, autoDisplay: false})
    Code:
    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)}}

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    If you want to replace 'en' with a variable called lang then you'd replace the 'en' with function() {return lang;}
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Nov 2012
    Location
    France
    Posts
    78
    Thanks
    20
    Thanked 0 Times in 0 Posts
    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.

    Can anybody else shed some light on this.

  • #4
    New Coder
    Join Date
    Nov 2012
    Location
    France
    Posts
    78
    Thanks
    20
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    If you want to replace 'en' with a variable called lang then you'd replace the 'en' with function() {return lang;}
    Tests
    Code:
    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?

  • #5
    New Coder
    Join Date
    Nov 2012
    Location
    France
    Posts
    78
    Thanks
    20
    Thanked 0 Times in 0 Posts
    I incorporated the split method:

    Code:
    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:
    Code:
    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.

    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.
    Last edited by Ace.....; 11-26-2012 at 11:07 PM.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    Nov 2012
    Location
    France
    Posts
    78
    Thanks
    20
    Thanked 0 Times in 0 Posts
    Re: The url, you can re-test with my pleasure & anticipation

    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.
    Quote Originally Posted by Old Pedant View Post
    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.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    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
    Code:
    <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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New Coder
    Join Date
    Nov 2012
    Location
    France
    Posts
    78
    Thanks
    20
    Thanked 0 Times in 0 Posts
    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:
    1. Write a doc that is vast & complex
    2. Provide a long page of undocumented links to code libraries
    3. 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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •