SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Bilingual Javascript External File

    Hello All, after i read some of the books of sitepoint (GREAT BOOKS) i try to get some of my exisiting code to use some of the standar that the book recomend, for example all the css in a seperate file and all the javascript in a external file too, but here is my problem, i have a PHP File, that generate the HTML with some JavaScript on it, becaus i generate the page useing PHP i have some code like this:

    Code:
    <script language="javascript" type="text/javascript">
    function submitbutton(pressbutton)
    {
    	var form = document.adminForm;
    	if (pressbutton == 'cancelaragencia') {
    		submitform( pressbutton );
    		return;
    	}
    
    	// do field validation
    	if (document.getElementById('nombre_agencia').value == ""){
    		alert( "<?php echo JText::_( 'El Nombre de Agencia es requerido', true ); ?>" );
    	} else {
    		submitform( pressbutton );
    	}
    }
    </script>
    as you can see i use and internal JText function using php,
    Code:
    alert( "<?php echo JText::_( 'El Nombre de Agencia es requerido', true ); ?>" );
    to translate the text depending the lenguaje seleccion of the site, so if the user select English the text is translated, before i generate the HTML code and show the page.


    i can make diferent .js file for each lenguaje and remove the php code and put the text for each lenguaje, but i all ready have a file with all the text that i have to translate for each page, i like to now if some body can give me direction or what to do, to have a bilingual javascript file,

    thanks for any help or advise,

    note: i am rocky on javascritp

  2. #2
    #titanic {float:none} silver trophy
    molona's Avatar
    Join Date
    Feb 2005
    Location
    from Madrid to Heaven
    Posts
    8,271
    Mentioned
    252 Post(s)
    Tagged
    1 Thread(s)
    First, "El nombre de la Agencia es requerido" sounds terrible. Change it to "El nombre de la Agencia es obligatorio"

    Second, I am not sure why you have a php echo inside a javascript alert. It makes much more sense to me if you get rid of the php and simply write
    Code:
    alert('El nombre de la Agencia es obligatorio')
    The javascript function alert already displays text, albeit by opening an alert window, therefore you don't need a php function to display it. And since you are not requesting any variable that you don't have already, I don't see the point of it.

  3. #3
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi molona, you right it sound terrible, jejejee

    ok about the php inside the java, i use that code to translate the alert text, depending of the selection of the lenguaje at the site, remember that code it inside a PHP file, and generate dynamic the HTML whit the Javascript so in the alert

    'El Nombre de Agencia es requerido', is actual a variable, if not translation exist show this massege but if a translation exist show 'The Agency names is required' automaticly.

    thanks for the reply

  4. #4
    #titanic {float:none} silver trophy
    molona's Avatar
    Join Date
    Feb 2005
    Location
    from Madrid to Heaven
    Posts
    8,271
    Mentioned
    252 Post(s)
    Tagged
    1 Thread(s)
    Well, it looks like everything but a variable

    I thought the variable was nombre_agencia.

    I would not use that, though. I would pass the value of the php variable to Javascript

    Code:
    <script type="text/javascript">
    var servervar= <php? echo $severvar;?>
    </script>
    And then use javascript to write the proper message according to this value.

  5. #5
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply molona, so in my main php file where i generate the java and html i add in the header the code whit my varialbes:

    Code:
    <script type="text/javascript">
    var servervar= <php? echo $severvar;?>
    </script>
    so i can used that variable on my external .js file?

    i will try this thanks

  6. #6
    #titanic {float:none} silver trophy
    molona's Avatar
    Join Date
    Feb 2005
    Location
    from Madrid to Heaven
    Posts
    8,271
    Mentioned
    252 Post(s)
    Tagged
    1 Thread(s)
    Be careful with your wording... remember that java is another programming language and people can get confused.

    What I am saying that you can pass the value of the php variable to javascript. It doesn't necessarily means that you need the <SCRIPT> tags. If all your javascript is in another file, it may not be necessary at all to use the <SCRIPT> tags.

    You'll need to keep a variable in your php where you will keep the language value. Then, when the page is processed and the javascript file loads, the php variable will be in memory because the server side is always processed before the client side. If your javascript file has a line

    Code:
    var myVar=<php? echo $phpVar ?>
    the value can be used by javascript without further complication.

  7. #7
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok thanks for let me now, because the idea is to remove the <SCRIPT> tags from my page and add external .js file, let me try and let you now

    thanks

  8. #8
    #titanic {float:none} silver trophy
    molona's Avatar
    Join Date
    Feb 2005
    Location
    from Madrid to Heaven
    Posts
    8,271
    Mentioned
    252 Post(s)
    Tagged
    1 Thread(s)
    Good luck

  9. #9
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have no luck

    i fund this on other forum and i think is what you recomend to me,

    Code:
    JavaScript cannot actively read variables from the server, but when a page loads, PHP can pass variables to JavaScript by writing to the page the JavaScript is on.
    
    
    <?php
    
        $username = "Snib";
    
    ?>
    ...
    <script language="javascript" type="text/javascript">
        var sStr = "My name is <?php echo $username ?>.";
    
        document.write(sStr);
    </script>
    ...
    
    
        This code will output this text:
    
    
        ...
        My name is Snib.
        ...
    
    
    
    By editing the JavaScript on the page, PHP can set JavaScript variables.
    but my goal is to have and external file so is not posible to read php variables using javascript becuase i use some like this to call the js file in the <head> area:

    Code:
    <script src="/js/registro_simple.js" type="text/javascript">
    i will have to stick whit the <SCRIPT> tags in my code.

    please let me now if you have any other sugestion.

    thanks

  10. #10
    #titanic {float:none} silver trophy
    molona's Avatar
    Join Date
    Feb 2005
    Location
    from Madrid to Heaven
    Posts
    8,271
    Mentioned
    252 Post(s)
    Tagged
    1 Thread(s)
    I assumed that it would work even in an external file because the php is processed first and after that the javascript is loaded and processed... hmmm need to think about this.

  11. #11
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what i have to do, (dindt like to have java on the page but is the only way):

    - i add a <SCRIPT> tag on may main page only with my variables example (JTEXT is a function to convert and translate the text that i use in joomla):

    Code:
    <script type="text/javascript">
    	//definimos variables para traduccion para el archivo externo
    	var txt_nombre_artistico = "<?php echo JText::_( 'El Nombre Artistico es Requerido',true );?>.";
    </script>
    Then in my external file .js i have some like this:

    Code:
      alert ( txt_nombre_artistico );
    and work, but like i say my idea is to not have any javascript on main page.


  12. #12
    #titanic {float:none} silver trophy
    molona's Avatar
    Join Date
    Feb 2005
    Location
    from Madrid to Heaven
    Posts
    8,271
    Mentioned
    252 Post(s)
    Tagged
    1 Thread(s)
    I think that cookies could be an option or maybe gathering information about the browser (I mean the language that it is used) and define the language from that information


Bookmarks

Posting Permissions

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