-3

I'M developing a site in php. I have set the language session $_SESSION['language'] = 'english' and I have included the language file according to the setted session. In php everything is fine and no issue.

The issue is I am using jquery validator plugin. And I want to return the validation errors according to the current language session. blow is a little code snidest.

code of english lang file.

` $lang = array();
$lang['0'] =  "Home";
$lang['1'] =  "Join";
$lang['2'] =  "Sign In";
$lang['3'] =  "close";
$lang['4'] =  "Email";
$lang['5'] =  "Choose Username";
$lang['6'] =  "Choose Password";
$lang['7'] =  "Are You Human?";`

for example if I need home somewhere in page I just do

echo $lang['0'];

in php its fine. but how can I accomplish this in .js

P.S I know the solution like write you javascript code in .php file please tell me is there any alternate way to do this or is there any way to import php array into javascript like jsonencode

Thanks

Warren Sergent
  • 2,542
  • 4
  • 36
  • 42
Saeed Ansari
  • 455
  • 8
  • 16

1 Answers1

0

You have to pass your php variable in js to use it in front, for example :

<script>
var lang0 = <?php echo $lang['0'] ; ?>;
</script>
Nicolas D
  • 1,182
  • 18
  • 40
  • but is it possible to use in a `.js` file? – Saeed Ansari Sep 15 '15 at 09:59
  • @SaeedAnsari: If you've configured your webserver to interpret PHP inside `.js` files, yes it is. – D4V1D Sep 15 '15 at 10:03
  • the easiest thing will be to do it in your php file between script tags ( in your view if you are in mvc). You will be able to use these variables in the js file anyway if they are in the same scope. – Nicolas D Sep 15 '15 at 10:05
  • lets say I have 10 functions in js file i don't want them all to show in source code on every page where I include the file it will present the code quality as poor. if I include file like this `` will it be a solution? – Saeed Ansari Sep 15 '15 at 10:10
  • this isnt a solution at all. the only good way is to sort well your code first, and to do what i did above. execute a php code by js require more skills ( ajax), and it seems to be too complicated as I see your answers, try to keep it simple. – Nicolas D Sep 15 '15 at 10:39