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; ?>;
8
  • 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? Commented Mar 26, 2013 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 <script> tags are), you are going to want to make those pages PHP and echo or whatever you need to do to the scripts. Commented Mar 26, 2013 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. Commented Mar 26, 2013 at 16:10
  • 1
  • @Bergi You can't run php code in a .js file Commented Mar 26, 2013 at 16:51

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I didn't think javascript support associative arrays so will LANG['value_1'] work in javascript?
It's a json object so you access the members like so: LANG.value_1
See my edit in the original post. I added more info.
Looking at your updated code: I think the issue is with the double script tags. lang.php generates output enclosed in <script></script>. This then gets interpreted as javascript by the browser and I suspect an error occurs when the <script> is encountered since it's not valid javascript.
You are correct, after getting rid of the <script> it works fine.
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>

3 Comments

See my edit in the original post. I added more info.
remove the <script> tag in the lang.php. The <script> tag in the different file is required. But it is not required in lang.php and that is causing the problem.You lang.php should be <?php session_cache_limiter('nocache'); session_start(); require_once ($_SESSION['language'].'.php'); $js_out = json_encode($LANG); ?> var LANG = <?php echo $js_out; ?>; alert(LANG.value_1);
You are correct, removing the <script> did the trick.
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

2 Comments

Works for me, with <script type="text/javasacript">
See my edit in the original post. I added more info.
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.

2 Comments

Can't run php code in a .js file
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.
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>

2 Comments

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.
See my edit in the original post. I added more info.
0

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

1 Comment

Thanks - Exactly the problem. Works now !!!!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.