SitePoint Sponsor |
|
User Tag List
Results 1 to 12 of 12
-
Oct 9, 2008, 21:49 #1
- 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>
Code:alert( "<?php echo JText::_( 'El Nombre de Agencia es requerido', true ); ?>" );
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
-
Oct 10, 2008, 02:08 #2
- 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')
Last edited by molona; Oct 10, 2008 at 09:57.
-
Oct 10, 2008, 09:32 #3
- 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
-
Oct 10, 2008, 10:03 #4
- 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>
-
Oct 10, 2008, 10:25 #5
- 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>
i will try this thanks
-
Oct 10, 2008, 11:46 #6
- 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 ?>
-
Oct 10, 2008, 12:26 #7
- 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
-
Oct 10, 2008, 13:05 #8
- Join Date
- Feb 2005
- Location
- from Madrid to Heaven
- Posts
- 8,271
- Mentioned
- 252 Post(s)
- Tagged
- 1 Thread(s)
Good luck
-
Oct 13, 2008, 12:40 #9
- 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.
Code:<script src="/js/registro_simple.js" type="text/javascript">
please let me now if you have any other sugestion.
thanks
-
Oct 13, 2008, 13:16 #10
- 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.
-
Oct 13, 2008, 13:25 #11
- 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>
Code:alert ( txt_nombre_artistico );
-
Oct 15, 2008, 07:48 #12
- 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