php - CodeIgniter: Parse dynamic language captions located in javascript -


i have javascript code needs localisation, ie.

function js_proc() {     var some_data = 'this text needs translated dynamically @ runtime'; } 

so re-wrote this:

function js_proc() {     var some_data = <?php echo $this->lang->line('some_data_id'); ?>; } 

in view, wrote js link this:

<script type="text/javascript" src="www.domain.com/codeigniter/get_js/file-1/"></script> 

which calls function get_js() in controller. idea have get_js() function read js file(s) , translate language strings...

there way many js strings translated, can't pass every string variable. ideally i'd make work through codeigniter language files.

my question is: there way parse php parts in js file & executed (=translate) them? ie.

class app extends ci_controller {     function get_js {         $content = file_get_contents($js_file);         echo parse_php($content);     }     ... } 

thanks!

depending on how big language file is, quick way allow javascript access entire language array load array global javascript array;

<script>     var globallang = <?php echo json_encode($this->lang->language); ?>; </script> 

then access in javascript this;

globallang['some_lang_key'] 

Comments

Post a Comment