add language to external js files

Started by rtrtr, August 26, 2012, 01:12:49 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ShadoW69

#2
you could use something like this
json_lang.php
Code (php) Select

require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'include'.DIRECTORY_SEPARATOR.'bittorrent.php');

header("Content-Type:application/json; charset=utf-8");
#header("Cache-Control: private, no-cache, no-store, must-revalidate");
#header("Pragma: no-cache");
header("Content-Ecoding: gzip");

### JUST KEEP ADDING THE LANGUAGE FILES YOU WANT TO DISPLAY OR DO A SWITCH BASED ON REQUEST
$lang = array_merge(
load_language('global'),
load_language('index'),
load_language('browse'),
load_language('login')
);

echo json_encode( $lang );

json_lang.js
Code (javascript) Select
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type='text/javascript'>
$(document).ready(function(){
$.get('json_lang.php',function(lang){
//console.log(lang);

//do some stuff
//using lang ex:
alert(lang.index_donations+' or '+lang['index_donations']+', thank you!');
});
});
</script>



LE: if you want to get the languages specific for each page, lets say your on index and you have loaded your global and index language files or your on browse.php and loaded browse and global, than you should use something like this
NOTE: this goes in template.php in the head tag, before the start of script tags
Code (javascript) Select
<script type='text/javascript'>window.lang=" . json_encode( $lang ) . ";console.log(lang);</script>

rtrtr

looking for:
"add language to external js files"

THX for help.