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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post

    Embed language strings into Javascript code

    Hi,

    I am using language files to localize my script. For example, I include lang_english.php that holds the english language strings like:

    PHP Code:
    define("_WAIT","Please Wait...");
    define("_ERRORNAME","Please enter your name"); 
    Then use the defined string in my scripts like

    PHP Code:
    echo _ERRORNAME
    As fas as PHP scripts are concerned, everything works fine so far. But, I can't localize my Javascript codes. For example form error alerts. I include my javascript files externally in my header.php, like

    Code:
    <script language="JavaScript" type="text/javascript" src="js/form.js"></script>
    and for example in form.js:

    Code:
    function validateForm() {
    
    	var myForm = document.forms["myForm"];
    
    	if (myForm.name.value == "") {
    		// This is what I'm trying to localize!
    		alert("Please enter your name");
    		myForm.name.focus();
    		return false;
    	}
    
    }
    So far, I couldn't get it to work. I tried to rename form.js to form.php and tried

    PHP Code:
    alert(<?php echo _ERRORNAME?>);
    But that didn't work either. Any advices?

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You would have to do link to a php file as the javascript source.
    PHP Code:
    <script language="JavaScript" type="text/javascript" src="js/form.php"></script> 
    Then in form.php you would do something like this
    PHP Code:
    <?php
    header
    ("Content-Type: text/javascript");
    include(
    'lang_english.php');
    $errorname _ERRORNAME;
    echo <<<EOF
    function validateForm() {

        var myForm = document.forms["myForm"];

        if (myForm.name.value == "") {
            // This is what I'm trying to localize!
            alert("$errorname");
            myForm.name.focus();
            return false;
        }

    }
    EOF;
    ?>
    Last edited by _Aerospace_Eng_; 07-27-2007 at 12:51 PM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    Hi Aerospace, that answer was fast!

    Could there be an easier way to do this? lang_english.php includes already all the strings and I have some forms which almost have 100 variables, so defining each variable again would supersize the file, I guess.

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Without using heredoc you would have to echo out each line of the js file, then you would just concatenate the constants where needed.
    PHP Code:
    <?php
    header
    ("Content-Type: text/javascript");
    include(
    'lang_english.php');
    echo 
    "alert('"._ERRORNAME."');";
    ?>
    You really should be doing the validation server side. Seriously 100 variables? Are you kidding? What the heck are you submitting?
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    You really should be doing the validation server side. Seriously 100 variables? Are you kidding? What the heck are you submitting?
    No, I am not I am already doing a validation on server side. I am doing the javascript validation to prevent the "heck" from being submitted in case there is an error

    Please check your PM


  •  

    Posting Permissions

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