Skip to content

How to include and use language files into your project

Lucas Kovács edited this page Aug 19, 2020 · 8 revisions

How to include a language file

We covered in this article How to create or edit a Language file, but what if we just created one and we want to include/add it to our project, controller or page? Just follow these steps:

  1. Open any controller that you have created
  2. Be sure that your controller extends from the parent class Controller
  3. Load the language file calling parent::loadLang and providing a name as a parameter. See the example below.
class MyClass extends Controller
{
    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();

        // load Language
        parent::loadLang('my_language_file');
    }
}

Notice that you won't need to add _lang.php, that will be done for you. Just be sure that your file name matches the file that you created under application/languages. In this case my_language_file_lang.php

You can load a single language file or multiple passing an array.

parent::loadLang(['language_file_one', 'language_file_two']);

How to use a language file

Our last step is to use the just created and loaded file. There are a few ways that you can do it.

Get all language lines

$this->langs->language; // store it into a variable to use it

Get a single language line (recommended)

$this->langs->line('language_line');

Get a single language line (alternative)

$this->langs->language['language_line'];

Get nested language lines

$this->langs->language['language_line']['language_line_second_block']['language_line_third_block'];

This document is valid as of: 30 October 2019
Valid as of: v3.1.0 Beta 3
Last update: 19 August 2020