2

I have a file called english.php containing a tonne of variable values. All part of the $LANG array.

Examples:

$LANG['value_1']="abc";

$LANG['value_2']="xyz";

I then have a million different .php files that use require_once('english.php');

That is fine but I also have a lot of javascript and jquery plugins that I am using. They all have external .js files. How can I get the values of $LANG in javascript to it is usable in the .js files?

I guess I am gonna need to add code to the top of the .js to somehow reading the .php data before running the remainder of the javascript code. I just have absolutely no idea how to do that.

I have seen a few possible ideas but I don't really want to do a major rewrite of everything. Looking for a simple solution. Can anyone help this clueless novice?

======= Added more info based on comments received =======

I now have a lang.php with this code in it...

<?php
session_cache_limiter('nocache');
session_start();
require_once ($_SESSION['language'].'.php');


$js_out = json_encode($LANG);

?>
<script>
    var LANG = <?php echo $js_out; ?>;
    alert(LANG.value_1);
</script>

When I access the lang.php it successfully accesses english.php and alerts 'abc'

My problem is that this does not work when added to a different file...

<script type='text/javascript' src='lang.php'></script>
<script>
    alert(LANG.value_1);
</script>

======= Edited to add the SOLUTION =======

Thanks to the comments of the people below, I got rid of the <script> in the lang.php file and it worked.

I now have a lang.php with this code in it...

<?php
session_cache_limiter('nocache');
session_start();
require_once ($_SESSION['language'].'.php');


$js_out = json_encode($LANG);

?>

var LANG = <?php echo $js_out; ?>;
G-J
  • 1,080
  • 2
  • 16
  • 32
  • 1
    use a `json_encode` and place the data somewhere that is acecssible to your JS. Without knowing how your project is organized it is difficult to say. Why does your JS need the translation tables? – Chad Mar 26 '13 at 16:05
  • You don't get the PHP variables from JavaScript, it's the other way around. PHP tells its variables to the JavaScript. So wherever you include your scripts (probably where the ` – Matt Dodge Mar 26 '13 at 16:05
  • 1
    If there's that much data in `english.php` (I assume you're doing dynamic regional pages or some such) I'd recommend making this an ajax call to the php requesting a specific list of keys instead of dumping the entire thing out to the JavaScript. – Wolfman Joe Mar 26 '13 at 16:10
  • 1
    possible duplicate of [What is the safest way of passing arguments from server-side PHP to client-size JavaScript](http://stackoverflow.com/questions/3613186/what-is-the-safest-way-of-passing-arguments-from-server-side-php-to-client-size) or [Pass a PHP string to a Javascript variable (and escape newlines)](http://stackoverflow.com/questions/168214/pass-a-php-string-to-a-javascript-variable-and-escape-newlines) – Bergi Mar 26 '13 at 16:15
  • @Bergi You can't run php code in a .js file – G-J Mar 26 '13 at 16:51
  • @Chad See my edit in the original post. I added more info. – G-J Mar 26 '13 at 17:47
  • Ok... Thanks to the comments below. The problem with the recent edit was the ` – G-J Mar 26 '13 at 20:39
  • @G-J Glad to help... you also need to accept the answer you found most suitable in order to help others that come across this question. – KalenGi Mar 26 '13 at 21:04

6 Answers6

3

You can have the following code inside english.php:

<?php


$LANG['value_1']="abc";
$LANG['value_2']="xyz";

$js_out = json_encode($LANG);

?>
<script>
    var LANG = <?php echo $js_out; ?>;
</script>

LANG is then visible to your javascript after the page loads.

KalenGi
  • 1,766
  • 4
  • 25
  • 40
  • I didn't think javascript support associative arrays so will `LANG['value_1']` work in javascript? – G-J Mar 26 '13 at 16:39
  • It's a json object so you access the members like so: LANG.value_1 – KalenGi Mar 26 '13 at 16:59
  • See my edit in the original post. I added more info. – G-J Mar 26 '13 at 17:44
  • Looking at your updated code: I think the issue is with the double script tags. lang.php generates output enclosed in . This then gets interpreted as javascript by the browser and I suspect an error occurs when the – KalenGi Mar 26 '13 at 18:10
  • You are correct, after getting rid of the ` – G-J Mar 26 '13 at 20:41
0

you can do that with a simple script tag in the files which need to see the $LANG in javascript.

1) Create a php file which echoes the javascript representation of $LANG - lets call it lang.php it should do something like

echo 'var english ="' . $LANG['value_1']  "';";

2) include this file in your html and then u can use the variables english etc. as normal javascript variables.

<script language="javascript" src="http://whatever.com/lang.php"> </script>
Akhilesh Singh
  • 2,548
  • 1
  • 13
  • 10
  • See my edit in the original post. I added more info. – G-J Mar 26 '13 at 17:50
  • remove the – Akhilesh Singh Mar 26 '13 at 17:54
  • You are correct, removing the ` – G-J Mar 26 '13 at 20:46
0

Try:

<script ...>
var mydata = <?=json_encode($LANG)?>;
</script>

json_encode returns a string containing the JSON representation. http://php.net/manual/en/function.json-encode.php

000
  • 26,951
  • 10
  • 71
  • 101
0

I think the right thing to do is something like :

var foo = "<? echo $LANG['bar']; ?>";

The less php code you write in a js code, the better and cleaner it is.

Taytay
  • 82
  • 7
  • Can't run php code in a .js file – G-J Mar 26 '13 at 16:32
  • If you try to use php as a javascript generation engine, you need to include a php file not a real js file, something like foobar.js.php, it's the way files are handled in symfony2 framework. – Taytay Mar 26 '13 at 17:58
0

You can create a PHP file:

<?php
header('Content-Type: text/javascript');
echo 'var lang = {};';
foreach ($LANG as $key => $value) {
    echo "lang['$key'] = '" . addslashes($value) . "';";
}
?>

then link the script and you can use the lang object:

<script type='text/javascript' src='/path/to/lang.php'></script>
<script type='text/javascript'>
alert(lang.value_1);
</script>
  • Thanks @Rapidwolf - I am currently trying basically this. Put the pieces of other comments together and came up with basically what you have. Trying it now. – G-J Mar 26 '13 at 16:54
  • See my edit in the original post. I added more info. – G-J Mar 26 '13 at 17:45
0

Remove the <script></script> tags which possibly cause syntax errors when linked as a text/javascript file.