Welcome Guest, Not a member yet? Register   Sign In
Passing language strings to Javascript, view organization help


I would like to start over with your original question(s).

1) I need to provide language strings to my Javascript --> Okay, that should not be a problem

2) I really want to avoid defining JS or HTML in my controllers. --> Of course, it does not belong there

3) I'd prefer not to define a bunch of redundant Javascript files that are intermingled with language-specific data --> Mmm, okay there we have got a challenge.

The most simple solution for your problem would be to have different Javascript language files like this:


The content of a resource file could look like this:

var resource = {

Cancel: "Abbrechen",
Caution: "Achtung",
Confirm: "Bestätigen",
Confirmation: "Bestätigung"


When the language is German,

echo resource.Cancel

will print 'Abbrechen'. The only thing you have to do is to include the right Javascript language file based on the user's language.

There are two approaches:

1. You load the Javascript file in the view in the controller;
2. You load the Javascript file in a view in the view

I would go for the first approach, because you do not want to have any logic in your view. The decision which Javascript file to load has to be made in the controller.

The questions is of course where? It depends where you want to include your Javascript files, all in the header or all at the end of the body. Let's say you want to include the at the end of the body, your code could look like this.

$data = array(); //fill with whatever data you want
$this->load->view("header", $data);

$data = array(); //fill with whatever data you want
$data['language'] = $this->current_user->language;
$this->load->view("some_page", $data);

$data = array(); //fill with whatever data you want
$this->load->view("footer", $data);

You simple pass the user's language to the view. (No need to set it in the session.)

At the end of the view you simply have a line like this:

<script type="text/javascript" src="resources.<?php echo $language; ?>.js"></script>

followed by all the other required Javascript files:

<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.min.js"></script>

That will simply do.... If you would choose this approach, you can use these Javascript language files also for your PHP code. So all the language strings required by your application are stored in these files. (You just have to write some simple code which reads the Javascript file and places the data in a PHP array.)

BUT what if you do not want that? What if the language strings come from a database? I can think of a few solutions. One of them would be AJAX. Mmmm, seems a bit overkill for something this simple. Two more reasonable solutions would be:

1. Read the language strings from the database, pass them to the view and parse them as inline code;
2. Read the language strings from the database and place them in a Javascript resource (similar as described before);

The first approach seems the most efficient one, but actually isn't. The reading of the database has to be done with every page request (unless you store the strings in the session, which you do not want). Reading from the database costs time. Parsing the strings also costs time.  

With the second approach you simply check for the existence of the Javascript file. If it is present, just use it and otherwise create it, only once. The only thing you have to think of is to remove the Javascript files after making changes to the resource strings in the database.

Is this better than just having the language strings in Javascript files (maybe also used as PHP language resource files)? It depends on your motives for having the language strings in the database. Is there a good reason? Why would you?

Another question. Are the language strings you use in PHP different from the ones in Javascript? If there is little overlap, I would go for different resource files for PHP and Javascript. In that case you do not have to write PHP code to read the files and put the language strings in a PHP array. Just include the file in the right place based on the user's language.

You end up with double resource files. (So what?)


Of course a file like resources.de.php would look like this:

$resource = array(
'Cancel' => 'Abbrechen',
'Caution' => 'Achtung',
'Confirm' => 'Bestätigen',
'Confirmation' => 'Bestätigung');

Anyhow, try to keep things simple. My € 0.02

Messages In This Thread
RE: Passing language strings to Javascript, view organization help - by RWCH - 02-20-2015, 08:04 AM

Theme © iAndrew 2016 - Forum software by © MyBB