How to translate JS plugins dynamically with Codeigniter?

5

I'm developing a WebApp using Codeigniter and need to have multiple languages, in quiet PHP, my problem is how to organize the translations of Javascript plugins.

Example, I have in the application plugins like DataTables and Datepicker and both have translations coming from the Bower folder, but how do I pull dynamically using my PHP variable that changes the language on the system?

On many systems I noticed that there are global variables in JS that are placed in head with translations. Is there a technique to work with this?

    
asked by anonymous 27.07.2017 / 21:39

1 answer

2
  

On many systems I noticed that there are global variables in JS that are   placed in the head with the translations. There is a technique for working   with that?

Make it clear: This is not a tutorial here . You should already have a sense of the framework , and I'll just exemplify passing a variable from $_SESSION to JavaScript using the native CodeIgniter tools. I also did not worry too much about security, but I do not think there is a gross flaw in this code.

The goal is for you to notice how to assign session data from PHP to a JavaScript variable, which, if I understand correctly, answers your question. That is, we will manipulate the value of the variable JavaScript as it is changed in PHP .

So, use sessions , hooks and helpers . The hook will set the default session value for the variable. The helper will retrieve this value for JavaScript and print to document HTML .

  • Enable session library usage:

I like autoload : enter application/config/autoload.php and enter session into the corresponding array: $autoload['libraries'] = array('session') .

  • Defining available languages:

This data can logically be loaded from your bank. In the example I will set array $config['langs'] to serve as a reference. Enter application/config/config.php and enter this: $config['langs'] = ['pt-br','en','es']; .

So these are the only assumptions available for the set_lang_var() method that defines the language.

  • Enable and create a hook:

Enable hooks by setting $config['enable_hooks'] = TRUE; to application/config/config.php ;

Within application/config/hooks.php enter the following:

$hook['post_controller_constructor'][] = array(
    'function' => 'set_lang',
    'filename' => 'set_lang.php',
    'filepath' => 'hooks'
);

Now create the hook application/hooks/set_lang.php and enter the following:

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

function set_lang() {

    $ci = & get_instance();
    //$ci->session->unset_userdata('lang');
    if(NULL == $ci->session->userdata('lang')){
        //Se nao houver idioma na session, define o idioma padrao
        $userdata = ['lang'=>'pt-br'];
        $ci->session->set_userdata($userdata);
    }
    //Caso contrario, o metodo teste/set_lang_var() pode ser usado para mudar o idioma
}

From this point on PHP already knows what the default language is. It will now be necessary to have the system pass data from $_SESSION of PHP to a JavaScript readable variable.

  • Enabling and creating a helper:

To use helpers you should load them. I like using autoload : within application/config/autoload.php add the string helper to the appropriate array: $autoload['helper'] = array('string') ;

It's a good practice not to change the original framework scripts, so

12.08.2017 / 01:34